question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for config files

See original GitHub issue

Proposal

Teach the binary to accept a --config option

This 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:

--cpus 2 -v

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 to 4 for an invocation like this:

jscodeshift --config my-config --cpus 4 target.js

In the absence of a --config option, look for config in default locations

Default 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 to toSource()).

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:

{
  'cpus': 2,
  'v': true,
  'printOptions': {'quote': 'single'}
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
silvenoncommented, Feb 6, 2020

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).

0reactions
sibeliuscommented, Jun 19, 2020

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',
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add support for config files · Issue #1584 · tj/commander.js
Problem Adding support of config files when you have an option with a default value and you want the following priorities, is complicated....
Read more >
Add Service Config Files - Harness.io Docs
As a part of managing the Services you created, add Service Config Files to use throughout your configuration settings.
Read more >
Creating configuration files in Node.js using node-config
Managing configuration files across environments can be challenging, but using node-config to create Node.js configuration files can help.
Read more >
What Are Config Files? How to Edit Them Safely - MakeUseOf
On Windows, right-click the text editor, and select Run as administrator · On macOS and Linux, try escalating your privileges with the sudo ......
Read more >
How to add an app.config file to a project - Microsoft Learn
To add an application configuration file to a C# project · In Solution Explorer, right-click the project node, and then select Add >...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found