Skip to main content

codeshift/cli

To download and run codemods, we provide a CLI tool called @codeshift/cli.

@codeshift/cli is responsible for running the provided transform against your entire codebase. Under the hood, it is a wrapper of jscodeshift's CLI, which provides additional functionality

  1. Ability to run community codemods hosted on npm
  2. Runs versioned codemods in sequence
  3. Always runs the latest version of a codemod

The CLI allows you to run transforms either from the the public registry or on your local machine as per the original implementation of jscodeshift

Note: Codemods will be designed to do the heavy lifting, but they'll often not be perfect so some manual work may still be required in order to successfully migrate.

Usage/Installation

We recommend running the CLI with $ npx to ensure you always have the latest version.

$ npx @codeshift/cli --packages mylib@1.0.0 /project/src

But it can also be installed globally:

$ npm install -g @codeshift/cli or yarn global add @codeshift/cli

and run with:

$ codeshift or $ codeshift-cli

default

The default CLI command (when no subcommand is specified,) will attempt to download and run transform(s) against the specified file path.

In the majority of cases you want to be sure to either provide the --packages flag for running remote codemods, or the --transform, -t flag to run a local transform file.

For running codemods as an end-user it's recommend to use the --packages flag, which accepts the following format: --packages [package-name]@[semver-version]. For example, running the codemod to migrate your codebase to react version 18.0.0 you would specify the following --packages react@18.0.0.

In special cases, codeshift package authors may choose to also expose codemod "presets", which can be considered as utility codemods for that package. Presets are also run via the --packages flag like so: --packages react#remove-spread-props. Notice that we have switched to a hash # here to denote that we want to run a preset.

Codeshift will then attempt to locate codemods from both the public registry and the primary npm package ie React – NPM. (Note: Some packages wont have any codemods, you can use the list subcommand to check if they exist.)

Lastly, when authoring a package, it's possible to test your transforms by omitting both the --packages and --transform flags. In this interactive mode, the codeshift/cli will attempt to locate a local codeshift.config.js from the current or parent directories and present an interactive prompt for you to choose from.

example:

Run a transform for "@mylib/button" version 3.0.0 only

  • $ codeshift --packages @mylib/button@3.0.0 /project/src

Run a transform for "@mylib/button" preset foo-bar only

  • $ codeshift --packages @mylib/button#foo-bar /project/src

Run all transforms for "@mylib/button" greater than version 3.0.0 and @mylib/range greater than 4.0.0

  • $ codeshift --sequence --packages @mylib/button@3.0.0,@mylib/range@4.0.0 /project/src

Run the "my-custom-transform" transform

  • $ codeshift -t path/to/my-custom-transform /project/src

Display a prompt with a list of codemods from my local codeshift.config.js file(s).

  • $ codeshift /project/src

--transform, -t

Allows you to execute local transform file(s).

  • Can be provided with a comma-separated list (see example below).
  • Transforms can be either a single file or directory containing an "index" file.

example:

  • $ codeshift --transform codemods/my-special-mod /project/src/file.js
  • $ codeshift --transform codemods/my-special-mod/index.ts /project/src/file.js
  • $ codeshift --transform path/to/transform1.ts,path/to/transform2.ts,path/to/transform3.ts /project/src/file.js

--packages

Runs transforms for the specified comma separated list of packages, optionally include a version for each package to run all transforms since that version

example:

  • $ codeshift --packages @atlaskit/button /project/src
  • $ codeshift --packages @atlaskit/button@3.0.0,@atlaskit/range@4.0.0 /project/src

--parser, -p

Parser to use for parsing the source files you are code modding.

options:

  • babel
  • babylon
  • flow
  • ts
  • tsx (default)

default:

tsx

tsx is a superset of JavaScript + JSX and should be the most sensible default for modern codebases.

example:

  • $ codeshift --parser tsx /project/src/file.ts
  • $ codeshift -p babel /project/src/file.js

--extensions, -e

Transform files with these file extensions (comma separated list).

default:

js, jsx, ts, tsx

example:

  • $ codeshift --extensions ts,tsx /project/src/file.js
  • $ codeshift -e js /project/src/file.js

--sequence, -s

If the package flag is provided, runs all transforms from the provided version to the latest.

default:

false

example:

  • $ codeshift --packages @atlaskit/button@3.0.0 --sequence /project/src

--ignore-pattern

Ignore files that match a provided glob expression

default:

  • **/node_modules/**

example:

  • $ codeshift --ignore-pattern node_modules /project/src/file.js

--verbose

Show more information about the transform process

default:

  • 0

example:

  • $ codeshift --verbose 2 /project/src/file.js

--version, -v

Get current version number

example:

  • $ codeshift --version
  • $ codeshift -v

--help

Print all help text to the command line

example:

  • $ codeshift --help

list

Subcommand that lists available codemods for the provided packages

example:

Print a list of available codemods for a single package

  • $ codeshift list mylib

Print a list of available codemods for multiple packages

  • $ codeshift list @atlaskit/avatar @emotion/monorepo

init

Subcommand that generates a new codemod at your desired path

example:

Create a new codeshift package called foobar with a transform for version 10 on the Desktop

  • $ codeshift init --version="10.0.0" ~/Desktop/foobar

Create an empty codeshift package called on the Desktop

  • $ codeshift init ~/Desktop/foobar

Create an empty codeshift package called foobar in the current directory

  • $ codeshift init

Create a 'config only' codeshift package in the current directory

  • $ codeshift init --config-only .

--config-only (optional)

Only output a configuration file.

example:

Initializes a configuration file only

  • `$ codeshift init --config-only path/to/src``

Initializes a configuration file and preset source code

  • `$ codeshift init --config-only --preset update-imports path/to/src``

--version (optional)

A semver-compatible string. Will be used to generate mock transform files and configuration.

example:

Create a codemod package called foobar with a versioned transform.

  • $ codeshift init --package-name="foobar" --version="10.0.0" ~/Desktop

--preset (optional)

A kebab-cased string. Will be used to generate mock transform files and configuration.

example:

Create a codemod package called foobar with a preset.

  • $ codeshift init --package-name="foobar" --preset="sort-imports" ~/Desktop

validate

Subcommand that validates a codemod package at the desired path.

example:

Validate a codemod package called "my-codemods".

  • $ codeshift validate ./codemods/my-codemods

Validate a codemod package from the current working directory

  • $ codeshift validate