Skip to content

Commit

Permalink
Merge pull request #12 from jo-sm/exit-codes
Browse files Browse the repository at this point in the history
Add exit codes
  • Loading branch information
jo-sm authored Dec 14, 2017
2 parents 58f77df + bb0fde2 commit 3ed92a9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
56 changes: 54 additions & 2 deletions bin/stylelint_d.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env node

/*
Exit codes:
1: Something else went wrong.
2: At least one rule with an "error"-level severity triggered at least one violations.
*/

var minimist = require('minimist');
var net = require('net');
var spawn = require('child_process').spawn;
Expand Down Expand Up @@ -95,22 +102,57 @@ function lint(args) {
console.log(message.message);
}
} catch(e) {

process.exitCode = 1;

console.log('Could not parse JSON after ended:');
console.log(e);
}

return;
}

// Determine if it's an error, first
var errorObject;
var parsed;
if (data[0] === 'stylelint_d: isError') {
errorObject = data[1];

try {
parsed = JSON.parse(errorObject);
} catch(e) {
throw new Error('Unknown error occurred.');
}

process.exitCode = parsed.code;

console.log(parsed.message);

return;
}

if (format === 'string') {
var result = data.join('');

try {
console.log(JSON.parse(result));
parsed = JSON.parse(result);
} catch(e) {
process.exitCode = 1;

console.log('Error: Could not parse `stylelint_d` result');
console.error(e);

return;
}

var didErrored = parsed.errored;
var output = parsed.output;

if (didErrored) {
process.exitCode = 2;
}

console.log(output);
} else {
var parsedData = data.reduce(function(memo, i) {
if (i === 'stylelint_d: start') {
Expand All @@ -121,6 +163,8 @@ function lint(args) {
memo.currentType = 'invalidOptionWarnings';
} else if (i === 'stylelint_d: warnings') {
memo.currentType = 'warnings';
} else if (i === 'stylelint_d: exitCode') {
memo.currentType = 'exitCode';
} else if (memo.start) {
memo.start = false;
memo.currentIndex++;
Expand All @@ -134,11 +178,17 @@ function lint(args) {
try {
i = JSON.parse(i);
} catch(e) {
process.exitCode = 1;

console.log(i);
throw new Error('Could not parse Stylelint JSON.');
}

memo.outputs[memo.currentIndex][memo.currentType].push(i);
if (memo.currentType === 'exitCode') {
process.exitCode = i;
} else {
memo.outputs[memo.currentIndex][memo.currentType].push(i);
}
}

return memo;
Expand All @@ -153,6 +203,8 @@ function lint(args) {
}
});
}).catch(function(err) {
process.exitCode = 1;

console.log(generateError(err));
});
}
Expand Down
36 changes: 33 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ function connHandler(conn) {
if (formatter === 'string') {
// If the formatter is a string, we just send the raw output
// from stylelint, since it's formatted for us already.
result = JSON.stringify(data.output);
result = JSON.stringify({
output: data.output,
errored: data.errored
});

write(conn, result);
} else {
// To handle large data, we spread out sending the resulting
Expand Down Expand Up @@ -214,14 +218,40 @@ function connHandler(conn) {

write(conn, JSON.stringify(warning));
}

// Send exit code
write(conn, "stylelint_d: exitCode");
if (warnings.length > 0) {
write(conn, JSON.stringify(2));
} else {
write(conn, JSON.stringify(0));
}
}
}
})
.catch(function(error) {
console.info(`Could not lint: ${error}`);

var errorObject = generateError(error, formatter);
conn.write(JSON.stringify(errorObject));
// If the error code is 78, it's due to a configuration error
// If the error code is 80, it's a glob error
// 78 -> 3, 80 -> 4, (other) -> 5

var errorCode;
if (error.code === 78) {
errorCode = 3;
} else if (error.code === 80) {
errorCode = 4;
} else if (Number.isInteger(error.code)) {
errorCode = 5;
}

var errorObject = {
message: generateError(error, formatter),
code: errorCode
};

write(conn, 'stylelint_d: isError');
write(conn, JSON.stringify(errorObject));
})
.then(function() {
// Finally, close the connection
Expand Down

0 comments on commit 3ed92a9

Please sign in to comment.