mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
feat: adding error codes to ShellJS
This commit is contained in:
parent
152edb94ab
commit
5bcbc619e4
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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()
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user