facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.14k stars 471 forks source link

Inject data to transformer file? #363

Open gomesalexandre opened 4 years ago

gomesalexandre commented 4 years ago

Hi! I'm currently trying to work with a transformer file that would receive injected data. It might be a non-standard usage of jscodeshift but this is the flow I'm currently trying to get working:

  1. Run an inquirer prompt
  2. Collect user's Answers
  3. Pass answers to jscodeshift transformer file
  4. Parse the AST of some file (that the user will have chosen), and add/update/modify a node there, depending on user answers.

Although not documented, I've found how to use jscodeshift without the CLI (running the default export of src/Runner.js and reconstructing the arguments), but unfortunately, there is no way to pass data to the transformer file.

It seems like passing data would be fairly trivial (https://github.com/gomesalexandre/jscodeshift/commit/ecf6c50968871b26435ae495642ede191c3ef93f).

Is that something that we would like to have? 🙏

gomesalexandre commented 4 years ago

Actually, it seems that the third and last argument of Runner.run, options, could let us inject data.

With that being said, I'm not a big fan of mixing concerns, in this case jscodeshift API options, and external data we want to inject. I would still vote for another argument

TheRakeshPurohit commented 3 years ago

I would also like to hear more on this

danieldelcore commented 3 years ago

Hey folks pretty sure you can pass arbitrary flags to jscodeshift and read them in your transform.

Try this jscodeshift --myarg foo --transform path/to/transform path/to/code

Now if you log the options argument you should see myarg