Skip to content

Commit fdff2be

Browse files
authored
feat(cli): add strict mode (#3384) (#3385)
1 parent 9abd75f commit fdff2be

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'always', ['feat']],
4+
'subject-max-length': [1, 'always', 4]
5+
}
6+
};

@commitlint/cli/src/cli-error.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ export class CliError extends Error {
22
__proto__ = Error;
33

44
public type: string;
5+
public error_code: number;
56

6-
constructor(message: string, type: string) {
7+
constructor(message: string, type: string, error_code = 1) {
78
super(message);
89

910
this.type = type;
11+
this.error_code = error_code;
1012

1113
Object.setPrototypeOf(this, CliError.prototype);
1214
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,24 @@ test('should work with relative formatter path', async () => {
482482
expect(actual.exitCode).toBe(0);
483483
});
484484

485+
test('strict: should exit with 3 on error', async () => {
486+
const cwd = await gitBootstrap('fixtures/warning');
487+
const actual = await cli(['--strict'], {cwd})('foo: abcdef');
488+
expect(actual.exitCode).toBe(3);
489+
});
490+
491+
test('strict: should exit with 2 on warning', async () => {
492+
const cwd = await gitBootstrap('fixtures/warning');
493+
const actual = await cli(['--strict'], {cwd})('feat: abcdef');
494+
expect(actual.exitCode).toBe(2);
495+
});
496+
497+
test('strict: should exit with 0 on success', async () => {
498+
const cwd = await gitBootstrap('fixtures/warning');
499+
const actual = await cli(['--strict'], {cwd})('feat: abc');
500+
expect(actual.exitCode).toBe(0);
501+
});
502+
485503
test('should print help', async () => {
486504
const cwd = await gitBootstrap('fixtures/default');
487505
const actual = await cli(['--help'], {cwd})();
@@ -507,7 +525,7 @@ test('should print help', async () => {
507525
-f, --from lower end of the commit range to lint; applies if
508526
edit=false [string]
509527
--git-log-args addditional git log arguments as space separated string,
510-
example \'--first-parent --cherry-pick\' [string]
528+
example '--first-parent --cherry-pick' [string]
511529
-o, --format output format of the results [string]
512530
-p, --parser-preset configuration preset to use for
513531
conventional-commits-parser [string]
@@ -516,6 +534,8 @@ test('should print help', async () => {
516534
edit=false [string]
517535
-V, --verbose enable verbose output for reports without problems
518536
[boolean]
537+
-s, --strict enable strict mode; result code 2 for warnings, 3 for
538+
errors [boolean]
519539
-v, --version display version information [boolean]
520540
-h, --help Show help [boolean]"
521541
`);

@commitlint/cli/src/cli.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ const cli = yargs
110110
type: 'boolean',
111111
description: 'enable verbose output for reports without problems',
112112
},
113+
strict: {
114+
alias: 's',
115+
type: 'boolean',
116+
description:
117+
'enable strict mode; result code 2 for warnings, 3 for errors',
118+
},
113119
})
114120
.version(
115121
'version',
@@ -128,7 +134,7 @@ const cli = yargs
128134
main(cli.argv).catch((err) => {
129135
setTimeout(() => {
130136
if (err.type === pkg.name) {
131-
process.exit(1);
137+
process.exit(err.error_code);
132138
}
133139
throw err;
134140
}, 0);
@@ -160,7 +166,7 @@ async function resolveArgs(args: MainArgs): Promise<MainArgsObject> {
160166
return typeof args.then === 'function' ? await args : args;
161167
}
162168

163-
async function main(args: MainArgs) {
169+
async function main(args: MainArgs): Promise<void> {
164170
const options = await resolveArgs(args);
165171
if (typeof options.edit === 'undefined') {
166172
options.edit = false;
@@ -314,6 +320,14 @@ async function main(args: MainArgs) {
314320
console.log(output);
315321
}
316322

323+
if (flags.strict) {
324+
if (report.errorCount > 0) {
325+
throw new CliError(output, pkg.name, 3);
326+
}
327+
if (report.warningCount > 0) {
328+
throw new CliError(output, pkg.name, 2);
329+
}
330+
}
317331
if (!report.valid) {
318332
throw new CliError(output, pkg.name);
319333
}

@commitlint/cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface CliFlags {
1616
version?: boolean;
1717
verbose?: boolean;
1818
'print-config'?: boolean;
19+
strict?: boolean;
1920
_: (string | number)[];
2021
$0: string;
2122
}

0 commit comments

Comments
 (0)