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 directorymy-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
Createworkflow.ts next to src:
workflow.ts
forEachFileruns the callback once for each file that matchessrc/**/*.js.parse(file)gives you the file’s syntax tree.findAllfinds everyvardeclaration, andfile.writesaves the new text.- The callback returns how many declarations it changed.
result.outputscollects those numbers;result.changes.writtenlists the files that changed. step(...)runs the work and saves its result, so a resumed run doesn’t do it again.- What
mainreturns is the run’s output.
3. Preview with a dry run
git status shows no changes.
4. Apply it
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.