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
Changes from 3 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
4 changes: 2 additions & 2 deletions @commitlint/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
@@ -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 () => {
6 changes: 6 additions & 0 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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 = '';

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

isRulesEmpty = true;
}

const report = results.reduce<{
@@ -384,6 +387,9 @@ async function main(args: MainArgs): Promise<void> {
throw new CliError(output, pkg.name, 2);
}
}
if (!report.valid && isRulesEmpty) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@escapedcat if isRulesEmpty=true at line 347, do we really need to perform the invokation at line 348 or can return early?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried that before. We need a "formatted" report (aka output) to display the current message.
We could just throw a new error but I kinda like the current formatted way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still, wouldn't it be better to check isRulesEmpty first? i.e. if (isRulesEmpty && (!report.valid)) {, micro-optimization there; or even just if (isRulesEmpty) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, adjusted, thanks!

throw new CliError(output, pkg.name, 9);
}
if (!report.valid) {
throw new CliError(output, pkg.name);
}