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: set exit code depending on what went wrong #66

Merged
merged 9 commits into from
Oct 28, 2022
2 changes: 2 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ rules:
- error
- always
no-console: warn
ignorePatterns:
- test/projects/broken/*.js
23 changes: 19 additions & 4 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ const GUI = new gui();

const logger = Logger("");

const EXIT_CODES = Object.freeze({
success: 0,
violations: 1,
unexpected: 2,
interrupted: 3,
configuration: 4,
});

// used by meow's loud reject
// eslint-disable-next-line no-console
console.error = logger.error.bind(logger);

// Pretty logs all errors, then exits
process.on("uncaughtException", err => {
logger.error(err);
process.exit(1);
process.exit(EXIT_CODES.unexpected);
});

// Handle SIGINT
process.on("SIGINT", () => {
process.exit(EXIT_CODES.interrupted);
});

// Generates the CLI binding using meow
Expand Down Expand Up @@ -69,12 +82,12 @@ process.on("exit", () => {
logger.debug("No configuration file found");
if (cli.flags.config) {
logger.error("Configuration file not found");
process.exit(1);
process.exit(EXIT_CODES.configuration);
}
}
} catch (e) {
logger.error(`Failed to parse config: ${e.stack}`);
process.exit(1);
process.exit(EXIT_CODES.configuration);
}

// lint all the files
Expand All @@ -85,7 +98,9 @@ process.on("exit", () => {
--activeLintings;
logger.debug("Linting done,", activeLintings, "to go");
if (activeLintings <= 0) {
process.exit(hasErrors ? 1 : 0);
process.exit(
hasErrors ? EXIT_CODES.violations : EXIT_CODES.success
);
}
};
files.forEach(filePath => {
Expand Down
14 changes: 11 additions & 3 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,20 @@ describe("CLI", function(){
});

describe("Configuration files", function() {
it("should fail passing an non-existent file path to --config", async function() {
it("should fail with an non-existent configuration file", async function(){
const { failed, exitCode } = await execCliWith(
[VALID_SVG, "--config", "./this/file/does/not-exist.js"],
["--config", "./this/file/does/not-exist.js"]
);
expect(failed).toBeTruthy();
expect(exitCode).toBe(1);
expect(exitCode).toBe(4);
});

it("should fail with a broken configuration file", async function(){
const { failed, exitCode } = await execCliWith(
["--config", "./test/projects/broken/broken-svglint-config.js"]
);
expect(failed).toBeTruthy();
expect(exitCode).toBe(4);
});

it("should succeed passing an existent file path to --config", async function() {
Expand Down
1 change: 1 addition & 0 deletions test/projects/broken/broken-svglint-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
garble garble I'm invalid