Quick Start
1
Initialize a new codemod project
2
Run and test your codemod
3
Publish your codemod
4
Run the published codemod
Advanced Concepts
- Workflows: Orchestrate complex, multi-step codemod processes.
- JS ast-grep (jssg): Run codemods written in JavaScript/TypeScript to transform code in any language using the high-performance ast-grep engine.
CLI Command Reference
Codemod CLI is accessible using thenpx codemod
command. The following commands and options are available:
codemod workflow
Manage and execute workflow YAMLs.
workflow run
Run a workflow.
Local workflows vs. Registry packages:
- Use
npx codemod workflow run -w <path>
for local workflow files and directories - Use
npx codemod <package-name>
to run packages directly from the Codemod Registry
Path to workflow file or directory.
Examples
Examples
Running a local workflow:Running a workflow from the registry:Explore available packages on Codemod Registry.
workflow resume
Resume a paused workflow.
Workflow run ID.
Task ID to trigger (can be specified multiple times).
Trigger all awaiting tasks.
workflow validate
Validate a workflow file.
Path to workflow file.
Check | Ensures |
---|---|
Schema validation | YAML matches the workflow spec |
Unique IDs | Node & template IDs are unique |
Dependency validation | Every depends_on exists |
Cyclic dependency detection | DAG has no cycles |
Template references | All template: IDs exist |
Matrix validation | from_state matches schema |
State schema validation | state.schema is valid |
Variable syntax | ${{β¦}} uses params , env , state |
Why validate?Validation catches issues before execution, saving time and preventing runtime errors.
Validation vs. Logical Correctness
Validation vs. Logical Correctness
The
workflow validate
command ensures your YAML is syntactically correct and follows the schema, but it cannot verify:- Logical correctness: Whether your workflow does what you intend
- Runtime behavior: How your workflow behaves with real data
- Dependencies: Whether external files/scripts exist
- State consistency: Whether state updates are logically sound
workflow status
Show workflow run status.
Workflow run ID.
workflow list
List workflow runs.
Number of workflow runs to show. (default: 10)
workflow cancel
Cancel a workflow run.
Workflow run ID.
codemod jssg
JS ast-grep (jssg) is a toolkit for running JavaScript/TypeScript codemods using the high-performance ast-grep engine. It enables fast, large-scale code transformations with a familiar API and robust language support.
codemod jssg
lets you run ast-grep codemods directly from the CLI, without needing to define a workflow. Itβs built for speed and simplicity, making ast-grep codemods a first-class experience.
When should I use JS ast-grep (jssg)?
- When you want to quickly run or test an ast-grep codemod on your codebase.
- For more complex transformations that require granular AST access and manipulation than a YAML rule can provide. Read more about when to define workflows.
1
Write your codemod
Create a JS/TS file that exports your codemod logic.
2
Run your codemod
3
Test your codemod
Organize your tests as follows:Then run:
jssg run
Run a JS ast-grep (jssg) codemod.
Path to the JS ast-grep (jssg) codemod file (JS/TS).
Directory to apply the codemod to.
Target language (e.g.,
javascript
, typescript
, python
, java
, cpp
, php
, kotlin
, etc.).Comma-separated list of file extensions to process.
Do not respect
.gitignore
files.Include hidden files and directories in the scan.
Maximum number of concurrent threads to use.
Perform a dry-run to see the changes without applying them.
jssg test
Test a JS ast-grep(jssg) codemod using before/after fixtures.
Path to the JS ast-grep (jssg) codemod file, which is a JS/TS file.
Target language (e.g.,
javascript
, typescript
, python
, java
, cpp
, php
, kotlin
, etc.).The directory containing your tests (default:
"tests"
).A pattern to run only tests whose names match the filter.
The output format for test results. Can be
console
, json
, or terse
.Show detailed output, including diffs for failed tests.
The number of context lines to show in diffs (default: 3).
Ignore whitespace differences when comparing test outputs.
Test timeout in seconds (default: 30).
Maximum number of concurrent threads to use for running tests.
Run tests sequentially instead of in parallel.
Stop the test run on the first failure.
Create or update the
expected
files with the output of the codemod. (-u
is a shorthand for --update-snapshots
)A comma-separated list of test patterns that are expected to fail.
Enable watch mode to automatically re-run tests when files change.
When should I define a workflow instead?
When should I define a workflow instead?
- When you need to chain multiple codemods or scripts.
- When you want manual review, approval steps, or CI/CD integration.
- When you want to use engines other than ast-grep (e.g., jscodeshift, YAML, or custom scripts).
Why ast-grep?
Why ast-grep?
ast-grep is extremely fast and robust for syntax-aware code transformations. We made it first-class in the CLI for the most common use case, but you can still use any engine via workflows.jssg replicates the ast-grep NAPI, but with a few key differences:
- Itβs built into the CLI, so you can run it directly without needing to install it separately.
- Itβs built for speed and simplicity, making ast-grep codemods a first-class experience.
codemod init
Initialize a new workflow project.
Project directory name.
Project name (defaults to directory name).
Project type:
ast-grep-js
.More package types (advanced)
More package types (advanced)
shell
: Shell script-based codemodsast-grep-yaml
: YAML-based ast-grep codemods
Target language.
Project description.
Author name and email.
License.
Make package private.
Overwrite existing files.
Use defaults without prompts.
codemod login
Login to a registry.
Authenticate using an API key. Skips the browser login & is ideal for CI.
Registry URL.
Organization or user scope for publishing.
Need a key? Generate one in the Codemod app here ->.
codemod logout
Logout from a registry.
Registry URL to logout from.
Logout from all registries.
codemod whoami
Show current authentication status.
Registry URL to check.
Show detailed information including token scopes.
codemod publish
Publish a workflow to a registry.
Path to codemod directory.
Explicit version override.
Target registry URL.
Tag for the release.
Access level (
public
, private
).Validate and pack without uploading.
Publishing from CI or on behalf of an organization? Install the Codemod GitHub App on the target repos.
Publishing from CI (w/ GitHub App)
Publishing from CI (w/ GitHub App)
Use this method when your organization has installed the Codemod GitHub App. The app injects
CODEMOD_TOKEN
automaticallyβno separate login step needed.Publishing from CI (w/ API key)
Publishing from CI (w/ API key)
Use this flow when the GitHub App isnβt installed. Requires login
--api-key
; works for publishing new versions of existing codemods (the first publish must be interactive).Example action using a Codemod API key:
Examples
Examples
codemod unpublish
Remove a package or selected version from the registry.
Package name (e.g.,
@org/my-codemod
or my-codemod
).Specific semver to unpublish. Requires confirmation.
Unpublish all versions (irreversible). Confirmation required.
Target registry URL.
Show what would be removed without actually unpublishing.
The CLI always prompts for confirmation when
--version
or --force
is used. This interactive step cannot be bypassed programmatically.Examples
Examples
codemod search
Search for packages in the registry.
Search query
Filter by programming language
Filter by framework
Filter by category
Number of results to return (default: 20)
Pagination offset (default: 0)
Filter by organization scope
Registry URL
Output format (default: table). Possible values: table, json, yaml
Examples
Examples
Search for codemods related to React:Filter by language and category:Get results in JSON format:
codemod cache
Manage the local package cache for codemod packages.
cache info
Show cache information and statistics.
cache list
List cached packages.
Show package details.
cache clear
Clear cache for a specific package, or all packages.
Package name (e.g.,
@org/package
or package
).Clear all cached packages.
cache prune
Prune old or unused cache entries.
Maximum age in days to keep (default: 30).
Dry run - show what would be removed.