New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for config files #4
Comments
Throwing this up here for discussion. Happy to put together a pull request if we think this is a good idea. |
It should definitely be JSON. I think this makes sense. I'm a little bit worried that people would put a lot of script-specific options into those config files and that they'd potentially collide. I guess that is not a big deal though. Go for it! |
I pushed a WIP commit for this here: https://github.com/wincent/jscodeshift/commit/7a8317a4dd66d201b5c97e89893b944e1a1408cb
|
@wincent do you have time to rebase your diff against the current version of jscodeshift past #24? I'd really love for us to add this feature. I'd like it to travel up the hierarchy until it finds a |
Yeah, I have some time, although the exact amount and distribution of it is unpredictable right now. Traversing up sounds fine. |
Doesn't look like there has been progress on this in awhile (and I would like to see this feature for my own use), so I started working on a new branch.
On second thought, I guess what I was doing doesn't have too much value. I was trying to make it possible to override CLI options in addition to {
"recastPrintOpts": {}
} @cpojer Any thoughts? |
I think this suggestion should be revisited (there are nice solutions today like cosmiconfig for dealing with format). I currently have to use a custom npm script which passes options to jscodeshift the way I want, I think a config file would be a cleaner solution. Also, libraries like react-codemod could read your configuration instead of asking you (and I still couldn't configure it to work, but that's another story). |
that's I'm doing inside my transformer file import path from 'path';
const cwd = process.cwd();
const getConfig = (options: Options) => {
if (!options.config) {
return defaultConfig;
}
try {
const configPath = path.join(cwd, options.config);
const config = require(configPath); // eslint-disable-line
return config;
} catch (e) {
// eslint-disable-next-line
console.log('Config not found');
return defaultConfig;
}
};
function transform(file: FileInfo, api: API, options: Options) {
const config = getConfig(options);
} how to use it jscodeshift -t transform.ts --config path/to/config.js config.js module.exports = {
options: 'optionA',
} |
Proposal
Teach the binary to accept a
--config
optionThis option would allow you to specify a config file on disk from which additional options would be read. The file would contain additional options to be included with the invocation; for example:
Options explicitly provided on the command-line would override options read from the config file. For example, given the config above,
--cpus
would be set to4
for an invocation like this:In the absence of a
--config
option, look for config in default locationsDefault locations could be:
.jscodeshiftrc
in current directory$HOME/.jscodeshiftrc
/etc/jscodeshiftrc
(or something like that)This would, for example, enable you to provide a consistent set of Recast
printOptions
that you would use, by convention, in all of your transform scripts (ie. in the call totoSource()
).Considerations
Do we want the config file to be just a dumb string that we split and pass into nomnom as though the args were passed on the commandline? Or do we prefer a more structured format (like JSON) which would allow us to pass richer configuration objects rather than just scalar values (strings, numbers, bools)? I'm inclined to think the latter, so that we could configure things like
printOptions
for Recast with:The text was updated successfully, but these errors were encountered: