From 9e6ce015acb41870de3c830fa7525a247d84f023 Mon Sep 17 00:00:00 2001 From: Nate Cavanaugh Date: Wed, 6 Jun 2018 15:54:49 -0700 Subject: [PATCH] Fixes #26; For interfaces that need to have either a non-zero exit code, 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. --- README.md | 27 +++++++++++++++++++++++++++ bin/index.js | 6 +++++- lib/argv.js | 4 ++++ lib/cli.js | 4 ++++ test/cli.js | 26 ++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a131763..d390dda 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/bin/index.js b/bin/index.js index 613bc80..cba7502 100755 --- a/bin/index.js +++ b/bin/index.js @@ -22,7 +22,7 @@ var config = new ConfigStore( ); var cli = require('../lib/cli').init().then( - function() { + function(results) { var deprecated = deprecationCheck( { config, @@ -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; + } } ); \ No newline at end of file diff --git a/lib/argv.js b/lib/argv.js index bb1867b..2737104 100644 --- a/lib/argv.js +++ b/lib/argv.js @@ -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, diff --git a/lib/cli.js b/lib/cli.js index 4e147fe..8d6556b 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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; } diff --git a/test/cli.js b/test/cli.js index d6e4fb6..432831e 100644 --- a/test/cli.js +++ b/test/cli.js @@ -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() {