Skip to content

Commit

Permalink
feat: set exit code depending on what went wrong (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcornelissen committed Oct 28, 2022
1 parent 4e75fb3 commit 65f16bd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
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

0 comments on commit 65f16bd

Please sign in to comment.