Request: Support codemods published to npm
See original GitHub issueRight now, there are 2 ways that a developer can run a codemod:
- Copy the codemod to their local machine, and pass the file path in with
-t
- Provide a URI to a remote
.js
file (with the caveat that the script must be entirely self contained).
I’d like to propose a 3rd option: being able to npm install -g
a codemod.
I can think of 2 different options for how this would work.
- A developer passes in the name of the globally-installed transform (either with
-t
or a new flag) jscodeshift
can look for all globally-installed modules matching a specific prefix, and present the developer with a list of available transforms when one is not explicitly provided on the command line (this is how theyo
CLI works foryeoman
).
I think the major benefit here would be that it then becomes easier to author + distribute a codemod that is not entirely self-contained. Right now, if I wanted to publish a codemod that uses require
for modules other than node built-ins, a consumer of my codemod needs to:
- Clone the repository
- Run
npm install
- Navigate to their project
- Specify the exact path to my codemod’s entry point with the cloned repository
Another benefit is discoverability: If the docs encourage users to publish to npm
with specific tags, it will make searching for available transforms much less of a struggle.
This is work I’d definitely be willing to contribute, but wanted to elicit feedback before taking that work on.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:9 (4 by maintainers)
Top GitHub Comments
Hey folks, I’m working on a project based on jscodeshift that acts as a community registry for codemods.
The way it works in a nutshell is:
npx @codeshift/cli --packages mypackage@14 path/to/src
If you want to publish your codemods you can get started by reading the Authoring and the Contribution guides.
The project is called CodeshiftCommunity, if you’re interested 😄 It’s early days, but would be keen to see what you all think about it/ if you find it useful 😄
I’m in favor of changing jscodeshift’s CLI to look for npm packages of transforms automatically, especially since it could theoretically be done without any changes to the existing transforms. As long as they’re published on npm, jscodeshift could locate them locally or in the package repository and just provide abstractions for running specific transforms. I think it would also be useful to have an option for running all of a package’s transforms together, since many pck have related transforms that are meant to be run together. Perhaps something like
jscodeshift --package NAME
could run all transforms, with the-t
flag modified to support locating the file by its name only (file
instead of.../transforms/file.js
).That being said, I do think that the custom CLi feature could be very useful, though for what I would want I would find the package-based approach more convenient since I wouldn’t have to modify as much existing code or publish new packages.
I would be interested in working on this, I just wanted to post my ideas first and see if anyone else has progress or similar ideas.