Skip to content

feat: allow global options in the config of a sub-command - #3927

Open
itsnjstyle27 wants to merge 1 commit into
mozilla:masterfrom
itsnjstyle27:feat/global-options-in-command-config
Open

itsnjstyle27 wants to merge 1 commit into
mozilla:masterfrom
itsnjstyle27:feat/global-options-in-command-config

Conversation

@itsnjstyle27

Copy link
Copy Markdown

Fixes #3571

Global options such as sourceDir or ignoreFiles can be passed to any sub-command on the command line (web-ext run --source-dir dist), but a config file only accepted them at the top level. In a sub-command section they failed:

{
  "webExt": {
    "run": { "sourceDir": "dist" },
    "lint": { "ignoreFiles": ["dist/**"] }
  }
}
UsageError: The config file at .../package.json specified an unknown option: "sourceDir"

With this change, a sub-command section can also set global options, and they are used only when that sub-command runs. In the example above, web-ext run uses dist as the source directory, and web-ext lint uses the default source directory and ignores dist/**.

How it works, in applyConfigToArgv:

  • When it descends into a sub-command section, the global options are added to the options that section accepts.
  • A global option in the section of the sub-command that is running is applied like any other configured value. One in the section of another sub-command is still type-checked, so a mistake there is reported as before, but it is not applied.
  • Sub-command sections are now applied after the top-level options, so for the running sub-command run.sourceDir takes precedence over a top-level sourceDir, whatever their order in the file. A value on the command line still takes precedence over both.

Options that belong to a sub-command are handled as before.

Tests: I added unit tests to test.config.js for a global option configured for the running sub-command, for the precedence over the same option at the top level (in both orders), for another sub-command not using it, for the CLI value taking precedence, and for the type check. All five fail without this change. ESLint, Prettier and the full unit suite pass (one run test, "throws error when used without firefox-profile or chromium-profile", failed once in the full run on Windows and passes on its own, on master too).

The configuration file docs on Extension Workshop would need a sentence about this. I can open a PR there once this is accepted.

Global options such as sourceDir or ignoreFiles can be passed to any
sub-command on the command line, but a config file only accepted them
at the top level. In a sub-command section they failed with "unknown
option".

Accept them there too, and use them only when that sub-command runs.
Sub-command sections are now applied after the top-level options, so a
value for the running sub-command takes precedence over the top-level
one, while a CLI value still takes precedence over both. A global option
configured for another sub-command is type-checked but not used.

Fixes mozilla#3571
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

package.json webExt section doesn't support global options for commands

1 participant