conventional-changelog / commitlint

đź““ Lint commit messages
https://commitlint.js.org
MIT License
16.94k stars 912 forks source link

Support command line options from a file #4109

Closed benquarmby closed 3 months ago

benquarmby commented 4 months ago

Description

This PR enables yargs' first-class support for a command line options file. It works out of the box and costs very little.

Also updated the CLI options docs while I was here.

Motivation and Context

The commitlint CLI has many arguments now; I counted 20. Composing and encoding all the required arguments for a project at the terminal or in a package script can get hard to maintain. The ability to move those options to a regular JavaScript or JSON module makes them far easier to manage. It is more human readable and writable.

Usage examples

This fabricated example is deliberately verbose and a bit unrealistic to make the point:

{
-   "commit:lint": "commitlint --from $GIT_LAST_TAG --format @some/formatter --parser-preset @some/preset --extends @some/shared-config-1 @some/shared-config-2 --strict --cwd ../project --git-log-args \"--first-parent\""
+   "commit:lint": "commitlint --options ./commitlint.options.cjs"
}
// commitlint.options.cjs
module.exports = {
    from: process.env.GIT_LAST_TAG,
    format: '@some/formatter',
    parserPreset: '@some/preset',

    // Explanatory code comment for this option could go here.
    extends: ['@some/shared-config-1', '@some/shared-config-2'],

    strict: true,
    cwd: '../project',
    gitLogArgs: '--first-parent'
};

How Has This Been Tested?

Added some temporary options files and ran them directly on this repo. Firstly a Common.js module:

// commitlint.options.cjs
module.exports = {
    from: "v19.3.1",
    verbose: true
};
yarn run commitlint --options ./commitlint.options.cjs
CJS Options Output cjs-ouput

And then a JSON file:

{
    "from": "v19.3.1",
    "verbose": true
}
yarn run commitlint --options ./commitlint.options.json
JSON Options Output json-output

Types of changes

Checklist:

codesandbox-ci[bot] commented 4 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

escapedcat commented 4 months ago

Hey @benquarmby , sounds great, thanks! Would you mind updating the failing test-snapshot to get this PR green?