Skip to main content
You’ll take a codemod package made with the 1.x CLI, run it on 2.0 unchanged, convert it to workflow.ts, and check that the result and its tests are the same. You need Node.js 22.18 or later and Git.

1. The 1.x package

This package is what codemod init --project-type ast-grep-js makes on 1.x, shortened: a workflow.yaml with one js-ast-grep step, a transform that turns var into const, and a fixture test.
workflow.yaml
scripts/codemod.ts
codemod.yaml points at the workflow with path: workflow.yaml, and tests/fixtures/ holds an input.js and an expected.js. Put the package in a Git repository, and next to it a small project to run it on:
app/src/index.ts

2. Run it on 2.0 as it is

The CLI translated workflow.yaml to TypeScript in memory and ran that. The package didn’t change. Registry packages run the same way: npx codemod@next @org/my-1x-package. The compatibility path supports legacy metrics and workflow state without rewriting the source. Undo the change: (cd app && git checkout -- .).

3. Preview the conversion

In the package directory:
It shows every change it would make as a diff, and writes nothing:
The node became a step, the js-ast-grep step became forEachFile with the same files, and the transform is imported and called for each file. Your transform doesn’t change.

4. Convert

The upgrade checks every package before it writes anything: if one can’t be converted, nothing is written. The hint names the CLI version you ran, so the package gets 2.0 and not the latest tag, which is 1.x. git status shows what it did: workflow.ts added, workflow.yaml removed (keep it with --keep-yaml), and codemod.yaml now says path: workflow.ts.

5. Check it

Run it on the project again. The diff is the same as in step 2:
The transform’s fixture test runs as before:
Commit the conversion. From here you can edit workflow.ts like any workflow: add steps, return a report, or test the whole workflow.
1.x’s codemod init writes npx codemod@latest jssg test in package.json, and @latest is the 1.x CLI. Change it to codemod jssg test, and add codemod as a dev dependency, so the tests run with the CLI the package uses.

Transforms that read files

1.x transforms often read the target with fs, directly or through a helper library. An upgraded workflow’s transforms keep what they had on 1.x, inside the target: fs reads files, writes them (replacing a file that is there) and deletes them with unlinkSync. The writes and deletes are part of the step’s diff. That allowance comes from a line of the generated file:
Keep that line among the comments the file starts with while the transforms still use fs; a license comment above it is fine. A workflow.ts without it, such as one you write yourself, refuses fs. Moving the transforms to codemod/files is optional; once they no longer import fs, you can delete the line.

What upgrade can’t convert for you

upgrade lists anything it can’t translate exactly, and leaves a // codemod upgrade: comment where it was. The common ones:
  • Transforms that use codemod:metrics: return the counts instead. See Upgrade from 1.x.
  • Transforms that call getState or setState: pass values between steps instead.
  • runtime: docker or runtime: podman: 2.0 refuses the workflow before running any steps. Keep running it with 1.x, or explicitly invoke the container from a shell step when you port it.
  • Transforms that import child_process: an upgraded workflow’s transforms can’t start processes. upgrade refuses the package (see below); move the commands to a shell step first.
upgrade also refuses, and writes nothing, when:
  • A workflow file is a symbolic link. Upgrade the file it points to, and point codemod.yaml at that file. A transform that is a symbolic link is refused too: put the file itself in its place.
  • A transform, or a file of the package it imports, imports child_process. Its calls would fail only when they run, after workflow.yaml is gone, so the package stays on workflow.yaml.
  • A file it would change is one its steps don’t select, such as a transform a .gitignore ignores.
  • Two workflows would become the same .ts file, or names that differ only in letter case (Build.yaml and build.yaml), which would overwrite each other on macOS and Windows.
  • A support file it would add, such as __codemod_v1/metrics.ts, already exists with other contents.
  • A transform would not load in 2.0. Before writing, upgrade loads every transform the new workflow.ts imports, as the workflow’s steps will. When one fails, it names the transform and the import that stopped it, for example scripts/codemod.ts does not load in 2.0: it imports 'node:net', which 2.0 workflows don't provide, and the package stays on workflow.yaml. A transform that imports a package that isn’t installed can’t be checked: upgrade says so and converts the package anyway. Install the package’s dependencies first to have it checked.
It lists every package it can’t upgrade, not only the first, and exits with code 2. See Migrate from workflow.yaml for how every YAML feature maps to TypeScript.