mozilla / web-ext

A command line tool to help build, run, and test web extensions
Mozilla Public License 2.0
2.7k stars 338 forks source link

Tracker: Set defaults for all options using a config file #176

Open kumar303 opened 8 years ago

kumar303 commented 8 years ago

If a user has a magically named config file in the current working directory (e.g. web-ext-config.json) then web-ext should use this to set default option values (and web-ext should log a message about using it). Here are a few other examples of how it should work:

The config can also be specified explicitly:

web-ext --config=/path/to/web-ext-config.json build ...

The config file should apply to global option values as well as sub-command option values. Here is an example of a config that is the equivalent of web-ext --verbose sign --api-url-prefix https://addons-dev.allizom.org/api/v3:

{
  "verbose": true,
  "sign": {
    "api-url-prefix": "https://addons-dev.allizom.org/api/v3"
  }
}

The camel case variants of each option should also be supported, such as:

{
  "sign": {
    "apiUrlPrefix": "https://addons-dev.allizom.org/api/v3"
  }
}

Some additional rules:

kumar303 commented 8 years ago

I have a patch started but it depends on https://github.com/yargs/yargs/issues/464

kumar303 commented 8 years ago

FWIW, a yaml config might be nicer than JSON because you could put comments in it

MarZab commented 8 years ago

I would suggest making a .js file that exports a settings object instead.

How about using a grunt task ie. Gruntfile.js like we can with jpm? I feel we are needlessly reinventing the wheel here.

kumar303 commented 8 years ago

Sure, a .js file would be nice because you can add comments. I think yargs might need a patch to support that but maybe not.

MarZab commented 8 years ago

Looks like something like this should work:

var argv = require('yargs')
  .config('settings', function (configPath) {
    return require(configPath)
  }).argv

web-ext.config.js

module.exports = {
  "sign": {
    "apiUrlPrefix": "https://addons-dev.allizom.org/api/v3"
  }
}
kumar303 commented 8 years ago

oh cool, that looks like it will work. I like using .js config files, let's do it.

pdehaan commented 8 years ago

You could use something like rc to use JSON or INI config files. Or I think ESLint supports config files as:

I believe their specific config loader logic is in eslint/eslint /lib/config/config-file.js.

kumar303 commented 8 years ago

It looks like rc will parse command line args and env vars but yargs already does that. It would be nice to just fix https://github.com/yargs/yargs/issues/464 but I don't know how difficult that is.

kumar303 commented 7 years ago

After chatting with @shubheksha yesterday, I think we have a good plan to do this without patching yargs:

rpl commented 7 years ago

@kumar303 @shubheksha I think that it can be useful to keep an eye to how eslint does its config file loading, e.g. some of the relevant pieces:

kumar303 commented 7 years ago

Hi @shubheksha , I split this into three issues (linked above). The patch you've already started applies to https://github.com/mozilla/web-ext/issues/728 so don't worry about any other features that we need later on. This should help land each patch sooner!