Skip to main content
You’ll write a workflow that replaces var with let in every JavaScript file of a project, check what it would change with a dry run, then apply it. You need Node.js 22.18 or later and Git.

1. Make a project to change

Create a directory my-app with two files, and commit them. The CLI won’t write to a repository with uncommitted changes, so you can always undo a run with Git.
src/cart.js
src/user.js

2. Write the workflow

Create workflow.ts next to src:
workflow.ts
  • forEachFile runs the callback once for each file that matches src/**/*.js.
  • parse(file) gives you the file’s syntax tree. findAll finds every var declaration, and file.write saves the new text.
  • The callback returns how many declarations it changed. result.outputs collects those numbers; result.changes.written lists the files that changed.
  • step(...) runs the work and saves its result, so a resumed run doesn’t do it again.
  • What main returns is the run’s output.

3. Preview with a dry run

A dry run does everything except write. It ends with the diff it would apply:
git status shows no changes.

4. Apply it

Each run is saved under its id. npx codemod@next workflow list shows it, and workflow status --id <id> shows its steps. Commit and run it again: it finds nothing left to change and prints Replaced 0 declarations in 0 files.

Next

Approvals and CI

Ask before a step, and run the same workflow unattended.

Build a complete workflow

The longer tutorial: ast-grep rules, a shell step, and tests.