From fdff2bee2d688698555de1cab904d0f5038075b1 Mon Sep 17 00:00:00 2001 From: SchweizS <18433748+SchweizS@users.noreply.github.com> Date: Sun, 18 Sep 2022 12:19:43 +0200 Subject: [PATCH] feat(cli): add strict mode (#3384) (#3385) --- .../cli/fixtures/warning/commitlint.config.js | 6 +++++ @commitlint/cli/src/cli-error.ts | 4 +++- @commitlint/cli/src/cli.test.ts | 22 ++++++++++++++++++- @commitlint/cli/src/cli.ts | 18 +++++++++++++-- @commitlint/cli/src/types.ts | 1 + 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 @commitlint/cli/fixtures/warning/commitlint.config.js diff --git a/@commitlint/cli/fixtures/warning/commitlint.config.js b/@commitlint/cli/fixtures/warning/commitlint.config.js new file mode 100644 index 0000000000..6d5ae18c22 --- /dev/null +++ b/@commitlint/cli/fixtures/warning/commitlint.config.js @@ -0,0 +1,6 @@ +module.exports = { + rules: { + 'type-enum': [2, 'always', ['feat']], + 'subject-max-length': [1, 'always', 4] + } +}; diff --git a/@commitlint/cli/src/cli-error.ts b/@commitlint/cli/src/cli-error.ts index 757814a1ed..eb1c985179 100644 --- a/@commitlint/cli/src/cli-error.ts +++ b/@commitlint/cli/src/cli-error.ts @@ -2,11 +2,13 @@ export class CliError extends Error { __proto__ = Error; public type: string; + public error_code: number; - constructor(message: string, type: string) { + constructor(message: string, type: string, error_code = 1) { super(message); this.type = type; + this.error_code = error_code; Object.setPrototypeOf(this, CliError.prototype); } diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index 6430383d96..f12a3ea589 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -482,6 +482,24 @@ test('should work with relative formatter path', async () => { expect(actual.exitCode).toBe(0); }); +test('strict: should exit with 3 on error', async () => { + const cwd = await gitBootstrap('fixtures/warning'); + const actual = await cli(['--strict'], {cwd})('foo: abcdef'); + expect(actual.exitCode).toBe(3); +}); + +test('strict: should exit with 2 on warning', async () => { + const cwd = await gitBootstrap('fixtures/warning'); + const actual = await cli(['--strict'], {cwd})('feat: abcdef'); + expect(actual.exitCode).toBe(2); +}); + +test('strict: should exit with 0 on success', async () => { + const cwd = await gitBootstrap('fixtures/warning'); + const actual = await cli(['--strict'], {cwd})('feat: abc'); + expect(actual.exitCode).toBe(0); +}); + test('should print help', async () => { const cwd = await gitBootstrap('fixtures/default'); const actual = await cli(['--help'], {cwd})(); @@ -507,7 +525,7 @@ test('should print help', async () => { -f, --from lower end of the commit range to lint; applies if edit=false [string] --git-log-args addditional git log arguments as space separated string, - example \'--first-parent --cherry-pick\' [string] + example '--first-parent --cherry-pick' [string] -o, --format output format of the results [string] -p, --parser-preset configuration preset to use for conventional-commits-parser [string] @@ -516,6 +534,8 @@ test('should print help', async () => { edit=false [string] -V, --verbose enable verbose output for reports without problems [boolean] + -s, --strict enable strict mode; result code 2 for warnings, 3 for + errors [boolean] -v, --version display version information [boolean] -h, --help Show help [boolean]" `); diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 3c66282614..0768d01ecf 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -110,6 +110,12 @@ const cli = yargs type: 'boolean', description: 'enable verbose output for reports without problems', }, + strict: { + alias: 's', + type: 'boolean', + description: + 'enable strict mode; result code 2 for warnings, 3 for errors', + }, }) .version( 'version', @@ -128,7 +134,7 @@ const cli = yargs main(cli.argv).catch((err) => { setTimeout(() => { if (err.type === pkg.name) { - process.exit(1); + process.exit(err.error_code); } throw err; }, 0); @@ -160,7 +166,7 @@ async function resolveArgs(args: MainArgs): Promise { return typeof args.then === 'function' ? await args : args; } -async function main(args: MainArgs) { +async function main(args: MainArgs): Promise { const options = await resolveArgs(args); if (typeof options.edit === 'undefined') { options.edit = false; @@ -314,6 +320,14 @@ async function main(args: MainArgs) { console.log(output); } + if (flags.strict) { + if (report.errorCount > 0) { + throw new CliError(output, pkg.name, 3); + } + if (report.warningCount > 0) { + throw new CliError(output, pkg.name, 2); + } + } if (!report.valid) { throw new CliError(output, pkg.name); } diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 236bd225d5..a951180e7a 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -16,6 +16,7 @@ export interface CliFlags { version?: boolean; verbose?: boolean; 'print-config'?: boolean; + strict?: boolean; _: (string | number)[]; $0: string; }