Running Codemods
Learn how to consume codemods. This page covers the different ways you can use codemods to automatically refactor your codebase, including integrating them into your development workflow and running them from the command line.
Running codemods is made simple through our CLI tool, @hypermod/cli.
@hypermod/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
For usage please refer to the @hypermod/cli API reference.
How to run Community codemods
To run a hypermod package, install and use the @hypermod/cli
.
- npm:
npm install -g @hypermod/cli
or - yarn:
yarn global add @hypermod/cli
For example, say we want to run transforms for @mylib/button
and migrate from version 13 to the latest version 14, we could run the following:
@hypermod/cli --packages @mylib/button@14.0.0 project/path/to/src
The following sequence of events will follow:
@hypermod/cli
will then attempt to download a Hypermod package for the@mylib/button
package matching version14.0.0
- Download the package from NPM
- Locate the
hypermod.config.js
- Attempt to find a
transform
for14.0.0
- Run the transform against the path
project/path/to/src
Run codemods in sequence
It's also possible to run a series of codemods, one after the other, to migrate your usage of @mylib/button
across multiple major versions, from say v14, v15 and finally v16. Assuming codemods for those versions exist.
This is done my providing the --sequence
(or -s
) flag to @hypermod/cli
.
@hypermod/cli --packages @mylib/button@14.0.0 --sequence project/path/to/src
This time around, we use the provided version (14.0.0) as the start of a semver range between 14.0.0-@latest
.
We then fetch all codemods that match and run them one after another.
Running local transforms
For local transform files, not published to the community repo, you can supply your own transform the same way you would with jscodeshift.
@hypermod/cli --transform path/to/transform.ts project/path/to/src
Parsing TypeScript
By default @hypermod/cli
will use babel
as the default parser and only transform files with a .js
extensions.
If your repo depends on Typescript, it's important to remember to specify ts
or tsx
as the --parser
and or jsx, ts, tsx
as --extensions
to make sure jscodeshift can interpret the files properly.
Please refer to the @hypermod/cli API reference for more information.