From d6795a3c4633ba6efd7a0fcff48339dc291cd832 Mon Sep 17 00:00:00 2001 From: Ellis Clayton Date: Tue, 29 Dec 2020 14:26:40 +1100 Subject: [PATCH] feat(load): allow specifying helpUrl via config (#2180) It can make configuration a bit more DRY if one can use the commitlint configuration object to specify a custom help URL vs. having wrapper scripts and similar things to make every invocation of `commitlint` run with `--help-url` specified. To allow this, `helpUrl` is added as an optional base configuration property, and falls back to the original default URL if neither the CLI flag nor config option is specified. The CLI option takes precedence over the config object, if both are supplied. --- @commitlint/cli/fixtures/help-url/commitlint.config.js | 6 ++++++ @commitlint/cli/src/cli.test.ts | 7 +++++++ @commitlint/cli/src/cli.ts | 6 +++--- @commitlint/load/src/load.ts | 6 ++++++ @commitlint/load/src/utils/pick-config.ts | 3 ++- @commitlint/types/src/lint.ts | 1 + @commitlint/types/src/load.ts | 2 ++ docs/reference-api.md | 8 ++++++++ docs/reference-configuration.md | 9 +++++++++ 9 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 @commitlint/cli/fixtures/help-url/commitlint.config.js diff --git a/@commitlint/cli/fixtures/help-url/commitlint.config.js b/@commitlint/cli/fixtures/help-url/commitlint.config.js new file mode 100644 index 0000000000..ab43d7482a --- /dev/null +++ b/@commitlint/cli/fixtures/help-url/commitlint.config.js @@ -0,0 +1,6 @@ +module.exports = { + rules: { + 'type-enum': [2, 'never', ['foo']] + }, + helpUrl: 'https://www.example.com/foo' +}; diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index 654ac2721d..4cc3a304ea 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -120,6 +120,13 @@ test('should fail for input from stdin with rule from js', async () => { expect(actual.exitCode).toBe(1); }); +test('should output help URL defined in config file', async () => { + const cwd = await gitBootstrap('fixtures/help-url'); + const actual = await cli([], {cwd})('foo: bar'); + expect(actual.stdout).toContain('Get help: https://www.example.com/foo'); + expect(actual.exitCode).toBe(1); +}); + test('should produce no error output with --quiet flag', async () => { const cwd = await gitBootstrap('fixtures/simple'); const actual = await cli(['--quiet'], {cwd})('foo: bar'); diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 950521817c..b8d4b127a2 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -229,12 +229,12 @@ async function main(options: CliFlags) { } ); + const helpUrl = flags['help-url']?.trim() || loaded.helpUrl; + const output = format(report, { color: flags.color, verbose: flags.verbose, - helpUrl: flags['help-url'] - ? flags['help-url'].trim() - : 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint', + helpUrl, }); if (!flags.quiet && output !== '') { diff --git a/@commitlint/load/src/load.ts b/@commitlint/load/src/load.ts index 93097a81fc..f6b2e7d002 100644 --- a/@commitlint/load/src/load.ts +++ b/@commitlint/load/src/load.ts @@ -112,6 +112,11 @@ export default async function load( return registry; }, {}); + const helpUrl = + typeof config.helpUrl === 'string' + ? config.helpUrl + : 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint'; + return { extends: preset.extends!, formatter: preset.formatter!, @@ -120,5 +125,6 @@ export default async function load( defaultIgnores: preset.defaultIgnores!, plugins: preset.plugins!, rules: qualifiedRules, + helpUrl, }; } diff --git a/@commitlint/load/src/utils/pick-config.ts b/@commitlint/load/src/utils/pick-config.ts index 881db12103..e5e01181c0 100644 --- a/@commitlint/load/src/utils/pick-config.ts +++ b/@commitlint/load/src/utils/pick-config.ts @@ -10,5 +10,6 @@ export const pickConfig = (input: unknown): UserConfig => 'parserPreset', 'formatter', 'ignores', - 'defaultIgnores' + 'defaultIgnores', + 'helpUrl' ); diff --git a/@commitlint/types/src/lint.ts b/@commitlint/types/src/lint.ts index e9f047e9e0..60f0a9456c 100644 --- a/@commitlint/types/src/lint.ts +++ b/@commitlint/types/src/lint.ts @@ -19,6 +19,7 @@ export interface LintOptions { parserOpts?: ParserOptions; plugins?: PluginRecords; + helpUrl?: string; } export interface LintOutcome { diff --git a/@commitlint/types/src/load.ts b/@commitlint/types/src/load.ts index cfcd59acc4..481b477e0f 100644 --- a/@commitlint/types/src/load.ts +++ b/@commitlint/types/src/load.ts @@ -21,6 +21,7 @@ export interface UserConfig { ignores?: ((commit: string) => boolean)[]; defaultIgnores?: boolean; plugins?: (string | Plugin)[]; + helpUrl?: string; } export interface UserPreset { @@ -43,6 +44,7 @@ export interface QualifiedConfig { ignores: ((commit: string) => boolean)[]; defaultIgnores: boolean; plugins: PluginRecords; + helpUrl: string; } export interface ParserPreset { diff --git a/docs/reference-api.md b/docs/reference-api.md index 0ed9d63dc1..95c4d6bd74 100644 --- a/docs/reference-api.md +++ b/docs/reference-api.md @@ -203,6 +203,10 @@ type Seed = { * Initial map of rules to check against */ rules?: {[ruleName: string]: Rule}; + /** + * URL to print as help for reports with problems + */ + helpUrl?: string; }; type Config = { @@ -218,6 +222,10 @@ type Config = { * Merged map of rules to check against */ rules: {[ruleName: string]: Rule}; + /** + * URL to print as help for reports with problems + */ + helpUrl?: string; }; type LoadOptions = { diff --git a/docs/reference-configuration.md b/docs/reference-configuration.md index 926b9df87d..2a1f7582fb 100644 --- a/docs/reference-configuration.md +++ b/docs/reference-configuration.md @@ -34,6 +34,10 @@ type Config = { * Whether commitlint uses the default ignore rules. */ defaultIgnores?: boolean; + /* + * Custom URL to show upon failure + */ + helpUrl?: string; }; const Configuration: Config = { @@ -66,6 +70,11 @@ const Configuration: Config = { * Whether commitlint uses the default ignore rules. */ defaultIgnores: true, + /* + * Custom URL to show upon failure + */ + helpUrl: + 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint', }; module.exports = Configuration;