Skip to content

Commit d6795a3

Browse files
authoredDec 29, 2020
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.
1 parent 59a8132 commit d6795a3

File tree

9 files changed

+44
-4
lines changed

9 files changed

+44
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'never', ['foo']]
4+
},
5+
helpUrl: 'https://www.example.com/foo'
6+
};

‎@commitlint/cli/src/cli.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ test('should fail for input from stdin with rule from js', async () => {
120120
expect(actual.exitCode).toBe(1);
121121
});
122122

123+
test('should output help URL defined in config file', async () => {
124+
const cwd = await gitBootstrap('fixtures/help-url');
125+
const actual = await cli([], {cwd})('foo: bar');
126+
expect(actual.stdout).toContain('Get help: https://www.example.com/foo');
127+
expect(actual.exitCode).toBe(1);
128+
});
129+
123130
test('should produce no error output with --quiet flag', async () => {
124131
const cwd = await gitBootstrap('fixtures/simple');
125132
const actual = await cli(['--quiet'], {cwd})('foo: bar');

‎@commitlint/cli/src/cli.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ async function main(options: CliFlags) {
229229
}
230230
);
231231

232+
const helpUrl = flags['help-url']?.trim() || loaded.helpUrl;
233+
232234
const output = format(report, {
233235
color: flags.color,
234236
verbose: flags.verbose,
235-
helpUrl: flags['help-url']
236-
? flags['help-url'].trim()
237-
: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
237+
helpUrl,
238238
});
239239

240240
if (!flags.quiet && output !== '') {

‎@commitlint/load/src/load.ts

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export default async function load(
112112
return registry;
113113
}, {});
114114

115+
const helpUrl =
116+
typeof config.helpUrl === 'string'
117+
? config.helpUrl
118+
: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint';
119+
115120
return {
116121
extends: preset.extends!,
117122
formatter: preset.formatter!,
@@ -120,5 +125,6 @@ export default async function load(
120125
defaultIgnores: preset.defaultIgnores!,
121126
plugins: preset.plugins!,
122127
rules: qualifiedRules,
128+
helpUrl,
123129
};
124130
}

‎@commitlint/load/src/utils/pick-config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const pickConfig = (input: unknown): UserConfig =>
1010
'parserPreset',
1111
'formatter',
1212
'ignores',
13-
'defaultIgnores'
13+
'defaultIgnores',
14+
'helpUrl'
1415
);

‎@commitlint/types/src/lint.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface LintOptions {
1919
parserOpts?: ParserOptions;
2020

2121
plugins?: PluginRecords;
22+
helpUrl?: string;
2223
}
2324

2425
export interface LintOutcome {

‎@commitlint/types/src/load.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface UserConfig {
2121
ignores?: ((commit: string) => boolean)[];
2222
defaultIgnores?: boolean;
2323
plugins?: (string | Plugin)[];
24+
helpUrl?: string;
2425
}
2526

2627
export interface UserPreset {
@@ -43,6 +44,7 @@ export interface QualifiedConfig {
4344
ignores: ((commit: string) => boolean)[];
4445
defaultIgnores: boolean;
4546
plugins: PluginRecords;
47+
helpUrl: string;
4648
}
4749

4850
export interface ParserPreset {

‎docs/reference-api.md

+8
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ type Seed = {
203203
* Initial map of rules to check against
204204
*/
205205
rules?: {[ruleName: string]: Rule};
206+
/**
207+
* URL to print as help for reports with problems
208+
*/
209+
helpUrl?: string;
206210
};
207211

208212
type Config = {
@@ -218,6 +222,10 @@ type Config = {
218222
* Merged map of rules to check against
219223
*/
220224
rules: {[ruleName: string]: Rule};
225+
/**
226+
* URL to print as help for reports with problems
227+
*/
228+
helpUrl?: string;
221229
};
222230

223231
type LoadOptions = {

‎docs/reference-configuration.md

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Config = {
3434
* Whether commitlint uses the default ignore rules.
3535
*/
3636
defaultIgnores?: boolean;
37+
/*
38+
* Custom URL to show upon failure
39+
*/
40+
helpUrl?: string;
3741
};
3842

3943
const Configuration: Config = {
@@ -66,6 +70,11 @@ const Configuration: Config = {
6670
* Whether commitlint uses the default ignore rules.
6771
*/
6872
defaultIgnores: true,
73+
/*
74+
* Custom URL to show upon failure
75+
*/
76+
helpUrl:
77+
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
6978
};
7079

7180
module.exports = Configuration;

0 commit comments

Comments
 (0)
Please sign in to comment.