Skip to content

Commit

Permalink
feat(cli): add strict mode (#3384) (#3385)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchweizS committed Sep 18, 2022
1 parent 9abd75f commit fdff2be
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions @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]
}
};
4 changes: 3 additions & 1 deletion @commitlint/cli/src/cli-error.ts
Expand Up @@ -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);
}
Expand Down
22 changes: 21 additions & 1 deletion @commitlint/cli/src/cli.test.ts
Expand Up @@ -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})();
Expand All @@ -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]
Expand All @@ -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]"
`);
Expand Down
18 changes: 16 additions & 2 deletions @commitlint/cli/src/cli.ts
Expand Up @@ -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',
Expand All @@ -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);
Expand Down Expand Up @@ -160,7 +166,7 @@ async function resolveArgs(args: MainArgs): Promise<MainArgsObject> {
return typeof args.then === 'function' ? await args : args;
}

async function main(args: MainArgs) {
async function main(args: MainArgs): Promise<void> {
const options = await resolveArgs(args);
if (typeof options.edit === 'undefined') {
options.edit = false;
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions @commitlint/cli/src/types.ts
Expand Up @@ -16,6 +16,7 @@ export interface CliFlags {
version?: boolean;
verbose?: boolean;
'print-config'?: boolean;
strict?: boolean;
_: (string | number)[];
$0: string;
}

0 comments on commit fdff2be

Please sign in to comment.