Skip to content

Commit

Permalink
feat(load): allow specifying helpUrl via config (#2180)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ellsclytn committed Dec 29, 2020
1 parent 59a8132 commit d6795a3
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions @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'
};
7 changes: 7 additions & 0 deletions @commitlint/cli/src/cli.test.ts
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions @commitlint/cli/src/cli.ts
Expand Up @@ -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 !== '') {
Expand Down
6 changes: 6 additions & 0 deletions @commitlint/load/src/load.ts
Expand Up @@ -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!,
Expand All @@ -120,5 +125,6 @@ export default async function load(
defaultIgnores: preset.defaultIgnores!,
plugins: preset.plugins!,
rules: qualifiedRules,
helpUrl,
};
}
3 changes: 2 additions & 1 deletion @commitlint/load/src/utils/pick-config.ts
Expand Up @@ -10,5 +10,6 @@ export const pickConfig = (input: unknown): UserConfig =>
'parserPreset',
'formatter',
'ignores',
'defaultIgnores'
'defaultIgnores',
'helpUrl'
);
1 change: 1 addition & 0 deletions @commitlint/types/src/lint.ts
Expand Up @@ -19,6 +19,7 @@ export interface LintOptions {
parserOpts?: ParserOptions;

plugins?: PluginRecords;
helpUrl?: string;
}

export interface LintOutcome {
Expand Down
2 changes: 2 additions & 0 deletions @commitlint/types/src/load.ts
Expand Up @@ -21,6 +21,7 @@ export interface UserConfig {
ignores?: ((commit: string) => boolean)[];
defaultIgnores?: boolean;
plugins?: (string | Plugin)[];
helpUrl?: string;
}

export interface UserPreset {
Expand All @@ -43,6 +44,7 @@ export interface QualifiedConfig {
ignores: ((commit: string) => boolean)[];
defaultIgnores: boolean;
plugins: PluginRecords;
helpUrl: string;
}

export interface ParserPreset {
Expand Down
8 changes: 8 additions & 0 deletions docs/reference-api.md
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
9 changes: 9 additions & 0 deletions docs/reference-configuration.md
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d6795a3

Please sign in to comment.