diff --git a/src/cat.js b/src/cat.js index 27889b7..14954cd 100644 --- a/src/cat.js +++ b/src/cat.js @@ -31,6 +31,6 @@ function _cat(options, files) { cat += fs.readFileSync(file, 'utf8'); }); - return new common.ShellString(cat, common.state.error); + return new common.ShellString(cat, common.state.error, common.state.errorCode); } module.exports = _cat; diff --git a/src/common.js b/src/common.js index 9270181..19fb63a 100644 --- a/src/common.js +++ b/src/common.js @@ -9,6 +9,8 @@ var shell = require('..'); var _to = require('./to'); var _toEnd = require('./toEnd'); +var DEFAULT_ERROR_CODE = 1; + // Module globals var config = { silent: false, @@ -20,6 +22,7 @@ exports.config = config; var state = { error: null, + errorCode: 0, currentCmd: 'shell.js', tempDir: null }; @@ -36,8 +39,18 @@ function log() { } exports.log = log; -// Shows error message. Throws unless _continue or config.fatal are true -function error(msg, _continue) { +// Shows error message. Throws if config.fatal is true +function error(msg, _code, _continue) { + if (typeof _code === 'boolean') { + _continue = _code; + _code = DEFAULT_ERROR_CODE; + } + if (typeof _code !== 'number') + _code = DEFAULT_ERROR_CODE; + + if (state.errorCode === 0) + state.errorCode = _code; + if (state.error === null) state.error = ''; var log_entry = state.currentCmd + ': ' + msg; @@ -46,9 +59,17 @@ function error(msg, _continue) { else state.error += '\n' + log_entry; - if(!_continue || config.fatal) + if(config.fatal) throw new Error(log_entry); + if(!_continue) { + // throw new Error(log_entry); + throw { + msg: 'earlyExit', + retValue: (new ShellString('', state.error, state.errorCode)) + }; + } + if (msg.length > 0) log(log_entry); } @@ -65,7 +86,7 @@ exports.error = error; //@ //@ Turns a regular string into a string-like object similar to what each //@ command returns. This has special methods, like `.to()` and `.toEnd()` -var ShellString = function (stdout, stderr) { +var ShellString = function (stdout, stderr, code) { var that; if (stdout instanceof Array) { that = stdout; @@ -75,6 +96,7 @@ var ShellString = function (stdout, stderr) { that.stdout = stdout; } that.stderr = stderr; + that.code = code; that.to = function() {wrap('to', _to, {idx: 1}).apply(that.stdout, arguments); return that;}; that.toEnd = function() {wrap('toEnd', _toEnd, {idx: 1}).apply(that.stdout, arguments); return that;}; ['cat', 'sed', 'grep', 'exec'].forEach(function (cmd) { @@ -230,6 +252,7 @@ function wrap(cmd, fn, options) { state.currentCmd = cmd; state.error = null; + state.errorCode = 0; try { var args = [].slice.call(arguments, 0); @@ -274,7 +297,13 @@ function wrap(cmd, fn, options) { }); if (!config.noglob && options && typeof options.idx === 'number') args = args.slice(0, options.idx).concat(expand(args.slice(options.idx))); - retValue = fn.apply(this, args); + try { + retValue = fn.apply(this, args); + } catch (e) { + if (e.msg === 'earlyExit') + retValue = e.retValue; + else throw e; + } } } catch (e) { if (!state.error) { diff --git a/src/echo.js b/src/echo.js index 7b8f85a..db3b5e1 100644 --- a/src/echo.js +++ b/src/echo.js @@ -16,6 +16,6 @@ function _echo(opts, messages) { // allow strings starting with '-', see issue #20 messages = [].slice.call(arguments, opts ? 0 : 1); console.log.apply(console, messages); - return new common.ShellString(messages.join(' ')); + return new common.ShellString(messages.join(' '), '', 0); } module.exports = _echo; diff --git a/src/exec.js b/src/exec.js index 4791625..b8b18f3 100644 --- a/src/exec.js +++ b/src/exec.js @@ -151,10 +151,7 @@ function execSync(cmd, opts, pipe) { if (code !== 0) { common.error('', true); } - var obj = common.ShellString(stdout, stderr); - // obj.stdout = stdout; - // obj.stderr = stderr; - obj.code = code; + var obj = common.ShellString(stdout, stderr, code); return obj; } // execSync() diff --git a/src/grep.js b/src/grep.js index 3c0106a..ef28e3b 100644 --- a/src/grep.js +++ b/src/grep.js @@ -56,6 +56,6 @@ function _grep(options, regex, files) { } }); - return new common.ShellString(grep.join('\n')+'\n', common.state.error); + return new common.ShellString(grep.join('\n')+'\n', common.state.error, common.state.errorCode); } module.exports = _grep; diff --git a/src/ls.js b/src/ls.js index 8088df7..42fcc59 100644 --- a/src/ls.js +++ b/src/ls.js @@ -100,7 +100,7 @@ function _ls(options, paths) { }); // Add methods, to make this more compatible with ShellStrings - return new common.ShellString(list, common.state.error); + return new common.ShellString(list, common.state.error, common.state.errorCode); } function addLsAttributes(path, stats) { diff --git a/src/pwd.js b/src/pwd.js index 9353b76..3bc310d 100644 --- a/src/pwd.js +++ b/src/pwd.js @@ -6,6 +6,6 @@ var common = require('./common'); //@ Returns the current directory. function _pwd() { var pwd = path.resolve(process.cwd()); - return new common.ShellString(pwd); + return new common.ShellString(pwd, '', common.state.errorCode); } module.exports = _pwd; diff --git a/src/sed.js b/src/sed.js index a9c675a..b302ff6 100644 --- a/src/sed.js +++ b/src/sed.js @@ -63,6 +63,6 @@ function _sed(options, regex, replacement, files) { fs.writeFileSync(file, result, 'utf8'); }); - return new common.ShellString(sed.join('\n'), common.state.error); + return new common.ShellString(sed.join('\n'), common.state.error, common.state.errorCode); } module.exports = _sed; diff --git a/src/which.js b/src/which.js index 2523886..86952d3 100644 --- a/src/which.js +++ b/src/which.js @@ -93,6 +93,6 @@ function _which(options, cmd) { where = where || path.resolve(cmd); - return new common.ShellString(where); + return new common.ShellString(where, '', common.state.errorCode); } module.exports = _which;