Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): use special errorCode for missing rules/config #4142 #4143

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions @commitlint/cli/src/cli-error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
export enum ExitCode {
escapedcat marked this conversation as resolved.
Show resolved Hide resolved
Normal = 0,
UncaughtFatalException = 1,
ReservedByBash = 2,
InternalJavaScriptParseError = 3,
InternalJavaScriptEvaluationFailure = 4,
FatalError = 5,
NonFunctionInternalExceptionHandler = 6,
InvalidArgument = 9,
}

export class CliError extends Error {
__proto__ = Error;

public type: string;
public error_code: number;
public error_code: ExitCode;

constructor(message: string, type: string, error_code = 1) {
constructor(
message: string,
type: string,
error_code = ExitCode.UncaughtFatalException
) {
super(message);

this.type = type;
Expand Down
4 changes: 2 additions & 2 deletions @commitlint/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test('should produce help for empty config', async () => {
const result = cli([], {cwd})('foo: bar');
const output = await result;
expect(output.stdout.trim()).toContain('Please add rules');
expect(result.exitCode).toBe(1);
expect(result.exitCode).toBe(9);
});

test('should produce help for problems', async () => {
Expand Down Expand Up @@ -137,7 +137,7 @@ test('should fail for input from stdin without rules', async () => {
const cwd = await gitBootstrap('fixtures/empty');
const result = cli([], {cwd})('foo: bar');
await result;
expect(result.exitCode).toBe(1);
expect(result.exitCode).toBe(9);
});

test('should succeed for input from stdin with rules', async () => {
Expand Down
16 changes: 13 additions & 3 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import yargs, {type Arguments} from 'yargs';

import {CliFlags} from './types.js';

import {CliError} from './cli-error.js';
import {CliError, ExitCode} from './cli-error.js';

const require = createRequire(import.meta.url);

Expand Down Expand Up @@ -316,6 +316,7 @@ async function main(args: MainArgs): Promise<void> {
messages.map((message) => lint(message, loaded.rules, opts))
);

let isRulesEmpty = false;
if (Object.keys(loaded.rules).length === 0) {
let input = '';

Expand All @@ -340,6 +341,8 @@ async function main(args: MainArgs): Promise<void> {
warnings: [],
input,
});

isRulesEmpty = true;
}

const report = results.reduce<{
Expand Down Expand Up @@ -378,12 +381,19 @@ async function main(args: MainArgs): Promise<void> {

if (flags.strict) {
if (report.errorCount > 0) {
throw new CliError(output, pkg.name, 3);
throw new CliError(
output,
pkg.name,
ExitCode.InternalJavaScriptParseError
);
}
if (report.warningCount > 0) {
throw new CliError(output, pkg.name, 2);
throw new CliError(output, pkg.name, ExitCode.ReservedByBash);
}
}
if (isRulesEmpty) {
throw new CliError(output, pkg.name, ExitCode.InvalidArgument);
}
if (!report.valid) {
throw new CliError(output, pkg.name);
}
Expand Down