Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
Fixes #26; For interfaces that need to have either a non-zero exit co…
Browse files Browse the repository at this point in the history
…de, or some other way to determine if things should halt, this flag will allow the caller to have any errors reported as a failure of the process.
  • Loading branch information
natecavanaugh committed Jun 6, 2018
1 parent e7aedfb commit 9e6ce01
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -159,6 +159,33 @@ If you pass `--no-color` it will overwrite the default and give you plain text.

If you pass `-v`, it will give you the lines in each file, as well as a merged version (useful for copy/pasting to update the metadata).

`--fail-on-errors` If this is passed, and *any* files report errors, this will send a non-zero exit code. This is useful if you're using it from the command line, and wish to halt the execution of any other actions.
If you are using the Node API instead of the CLI, then the results array returned from the Promise will have a property of `EXIT_WITH_FAILURE` set to true.
Examples of how you might use this (though, probably not often, at least until I have time to distinguish errors from warnings).

**CLI**
```
csf some_file_with_errors.css --fail-on-errors && do_some_new_build_task
```
**Node API**
```javascript
var cliInstance = new cli.CLI(
{
args: ['some_file_with_errors.js'],
flags: {
failOnErrors: true
}
}
);

cliInstance.init().then(
function(results) {
console.log(results.EXIT_WITH_FAILURE); // logs out 'true'
}
);
```


## Sublime Text Integration
There are now two ways you can integrate this module with Sublime Text:

Expand Down
6 changes: 5 additions & 1 deletion bin/index.js
Expand Up @@ -22,7 +22,7 @@ var config = new ConfigStore(
);

var cli = require('../lib/cli').init().then(
function() {
function(results) {
var deprecated = deprecationCheck(
{
config,
Expand All @@ -34,5 +34,9 @@ var cli = require('../lib/cli').init().then(
if (deprecated) {
console.log(deprecated);
}

if (results.EXIT_WITH_FAILURE === true) {
process.exitCode = 1;
}
}
);
4 changes: 4 additions & 0 deletions lib/argv.js
Expand Up @@ -24,6 +24,10 @@ var optimist = require('optimist')
boolean: true,
default: false
},
'fail-on-errors': {
boolean: true,
default: false
},
i: {
alias: 'inline-edit',
boolean: true,
Expand Down
4 changes: 4 additions & 0 deletions lib/cli.js
Expand Up @@ -212,6 +212,10 @@ class CLI extends EventEmitter {
this.openFiles(results);
}

if (this.flags.failOnErrors && this._logger.testStats.failures) {
results.EXIT_WITH_FAILURE = true;
}

return results;
}

Expand Down
26 changes: 26 additions & 0 deletions test/cli.js
Expand Up @@ -670,6 +670,32 @@ describe(
}
);

it(
'should indicate if the process should should fail on stdout',
function() {
sandbox.stub(fs, 'readFile').callsFake(invalidContentStub);

var logger = new Logger.constructor();

var cliInstance = new cli.CLI(
{
args: ['foo.js'],
flags: {
failOnErrors: true
},
log: _.noop,
logger: logger
}
);

return cliInstance.init().then(
function(results) {
assert.isTrue(results.EXIT_WITH_FAILURE, 'files that output errors should have a non-zero exitCode when --fail-on-errors is passed');
}
);
}
);

it(
'should handle custom config',
function() {
Expand Down

0 comments on commit 9e6ce01

Please sign in to comment.