Skip to main content
Build a workflow.ts that replaces deprecated util.isArray() and util.isBuffer() calls, removes the imports left unused, and formats the changed files. Then test it. You need Node.js 22.18 or later, npm, and Git.

Set up a test project

Create a directory named my-app. Run all commands from that directory. Start with these two files:
src/check.ts
src/format.ts

1. Apply ast-grep rules

Simple find-and-replace edits are ast-grep rules. Create rules/util-is.yaml with one rule per call. --- separates them:
rules/util-is.yaml
Create workflow.ts at the root. Import the rules, and give them to forEachFile with the files to run on. step runs it and returns the result:
workflow.ts
You can also write rules inline: astGrep({ id, language, rule, fix }), or an array of them. Commit the starting files so the target is clean, then try a dry run. It runs every step but writes nothing:
It prints Replaced 3 calls in 2 files and the diff it would apply, and git status shows no changes. Now run it for real:
Two calls change to Array.isArray and one to Buffer.isBuffer. The imports are still there for now.

2. Edit files with a callback

Rules can’t tell whether util is still used, so removing the import takes code. Give forEachFile a callback in place of the rules. It runs once per file and gets a file with its path and text. parse from codemod/ast-grep gives you the syntax tree, and file.write saves the new text. Replace workflow.ts with this:
workflow.ts
containing skips files that don’t mention node:util. Whatever the callback returns is collected into cleaned.outputs, in path order. Returning nothing collects nothing. Return any JSON value: a path, a count, an object. To try it, restore the original files with git restore src. The CLI refuses to run on a dirty target, so commit your workflow first:
It prints Replaced 3 calls in 2 files and Removed the util import from 2 files. git diff -- src shows both imports gone, with blank lines left where they were. The next step cleans those up.
A dry run writes nothing, but each step sees what the earlier ones would have changed, so a dry run reports the same counts as a real run. It ends by printing the diff it would apply.

Move the callback to its own file

A longer callback reads better in its own file. Move it to scripts/remove-util-import.ts as the default export. FileCallback<string> types it: it returns a string for each file it changes.
scripts/remove-util-import.ts
Then import it and pass it to forEachFile. The result is the same. The next section shows the full workflow.

3. Run a shell command

shell runs a command. Its step returns { stdout, stderr, exitCode }. Write the command as a tagged template, and each value becomes one quoted argument; an array becomes one argument per item. Here, Prettier formats the files the rules changed and lists the ones it rewrote:
A command that exits with an error fails the step, unless you pass { allowFailure: true } and check exitCode yourself. A dry run skips shell steps, so stdout can be missing there. In an interactive run, the CLI asks before running each command; an unattended run (--no-interactive, CI) of your own workflow runs them, and one of a registry package only with --approve.

4. Add a decision and an approval

Replace workflow.ts with the complete workflow. It stops early when there’s nothing to replace, uses the callback from its file, and asks before formatting:
workflow.ts
The workflow body is plain TypeScript: if, loops, and variables decide what runs next. Step results are plain JSON, so you pass them straight to the next step. Restore the files, commit, then run without prompts:
The run prints Replaced 3 calls in 2 files and Removed the util import from 2 files, then pauses at Format 2 changed files with Prettier? and prints the command to continue. Approve with the run ID from the output:
The workflow runs again from the top, but finished steps reuse their saved results (↺ ... (done earlier)). Prettier lists both files and the run prints Prettier reformatted 2 files. git diff -- src now shows clean files with no imports. Commit the changes and run the workflow again. It stops early with No util.isArray or util.isBuffer calls found.

5. Test the workflow

codemod/testing runs the whole workflow on fixture files and compares what it leaves. Add a package.json and install codemod, and keep node_modules out of Git:
package.json
A fixture is a directory with an input and an expected tree. Copy the two files from Set up a test project into tests/fixtures/util-is/input/src/, unchanged. Then create the test:
tests/workflow.test.ts
runWorkflow copies the input to a temporary directory and runs the workflow there, approvals given. Shell steps run for real in that copy. The second test passes files inline instead of a fixture. There’s no expected tree yet, so let the first run write it, then check it:
format.ts should read:
From now on, npm test fails with a diff when the workflow’s output changes. See Testing workflows for more.

Next steps

Workflow reference

See the tasks, options, and run controls.

Test your workflow

Fixtures, testing one step, and mocks.