diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-12-16 16:05:50 +0100 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-12-16 16:05:50 +0100 |
commit | 6c979c941943460b2dcdb84ef0c7f668620bef3f (patch) | |
tree | 8fb1f0983c13e529ba8ae4ff1168083f0415395d /bin/utils.js | |
parent | 7fd77323451e2088a77757049fad0098d048ef3c (diff) | |
download | gitbook-6c979c941943460b2dcdb84ef0c7f668620bef3f.zip gitbook-6c979c941943460b2dcdb84ef0c7f668620bef3f.tar.gz gitbook-6c979c941943460b2dcdb84ef0c7f668620bef3f.tar.bz2 |
Robust CLI error handling
Fixes #528, fixes #524, fixes #518
Diffstat (limited to 'bin/utils.js')
-rw-r--r-- | bin/utils.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/bin/utils.js b/bin/utils.js index 821112e..84d2e2f 100644 --- a/bin/utils.js +++ b/bin/utils.js @@ -30,6 +30,34 @@ function watch(dir) { return d.promise; } +// exit wraps a promise +// and forcefully quits the program when the promise is resolved +function exit(promise) { + promise + .then(function() { + // Prevent badly behaving plugins + // from making the process hang + process.exit(0); + }, function(err) { + // Log error + logError(err); + + // Exit process with failure code + process.exit(-1); + }); +} + +// CLI action wrapper, calling exit when finished +function action(f) { + return function() { + // Call func and get optional promise + var p = f.apply(null, arguments); + + // Exit process + return exit(Q(p)); + } +} + function logError(err) { var message = err.message || err; if (process.env.DEBUG != null) message = err.stack || message; @@ -60,6 +88,8 @@ function runGitCommand(command, args) { // Exports module.exports = { + exit: exit, + action: action, watch: watch, logError: logError, gitCmd: runGitCommand |