GithubHelp home page GithubHelp logo

Add support for config files about jscodeshift HOT 8 OPEN

facebook avatar facebook commented on April 27, 2024 3
Add support for config files

from jscodeshift.

Comments (8)

silvenon avatar silvenon commented on April 27, 2024 2

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

from jscodeshift.

wincent avatar wincent commented on April 27, 2024

Throwing this up here for discussion. Happy to put together a pull request if we think this is a good idea.

from jscodeshift.

cpojer avatar cpojer commented on April 27, 2024

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!

from jscodeshift.

wincent avatar wincent commented on April 27, 2024

I pushed a WIP commit for this here: https://github.com/wincent/jscodeshift/commit/7a8317a4dd66d201b5c97e89893b944e1a1408cb

  • currently doesn't try any validation of config file contents (other than them being parseable by JSON.parse)
  • want to add more tests for the ConfigReader and OptionParser modules, but...
  • ...Jest is acting very strangely with the mocks, and I can't get them to work properly (not mocking things I ask it to mock, mocking others I ask it not to mock etc...)

from jscodeshift.

cpojer avatar cpojer commented on April 27, 2024

@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 .jscodeshiftrc file. At FB, we could add one to our JS codebases with default print settings so a developer doesn't have to worry about picking the right options.

from jscodeshift.

wincent avatar wincent commented on April 27, 2024

Yeah, I have some time, although the exact amount and distribution of it is unpredictable right now.

Traversing up sounds fine.

from jscodeshift.

DrewML avatar DrewML commented on April 27, 2024

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. I did run into a bit of an issue, though.

I would assume that the preference is for command line arguments to override config in .jscodeshiftrc, right? This is problematic when trying to do this in combination with nomnom, because of the fact that nomnom handles setting the defaults. When merging .jscodeshiftrc values with the parsed CLI values, those defaults are always going to take precedence.

I considered pulling the defaults out of nomnom and handling that logic/config elsewhere, but that has the caveat that --help will no longer show defaults.

I looked at the previous WIP that Greg proposed for ideas, but (unless I'm looking at it wrong), it doesn't look like this logic will work, because options (coming from nomnom's parse) will always have the property name being passed to hasOwnProperty.

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 recast print settings, but I can't think of a scenario where you'd ever want to set most of those CLI options as static config. Maybe for now, we just provide the ability to provide print settings in the config file, and structure the config in such a way that we can add more options in the future? Example config being:

{
   "recastPrintOpts": {}
}

@cpojer Any thoughts?

from jscodeshift.

sibelius avatar sibelius commented on April 27, 2024

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',
}

from jscodeshift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.