description and source-codefunction shx(argv) {
var parsedArgs = (0, _minimist2.default)(argv.slice(2), { stopEarly: true, boolean: true });
var _parsedArgs$_ = _toArray(parsedArgs._),
fnName = _parsedArgs$_[0],
args = _parsedArgs$_.slice(1);
if (!fnName) {
console.error('Error: Missing ShellJS command name');
console.error((0, _help2.default)());
return _config.EXIT_CODES.SHX_ERROR;
}
// Load ShellJS plugins
var CONFIG_PATH = _path2.default.join(process.cwd(), _config.CONFIG_FILE);
if (_fs2.default.existsSync(CONFIG_PATH)) {
var shxConfig = void 0;
try {
shxConfig = require(CONFIG_PATH);
} catch (e) {
throw new Error('Unable to read config file ' + _config.CONFIG_FILE);
}
(shxConfig.plugins || []).forEach(function (pluginName) {
try {
require(pluginName);
} catch (e) {
throw new Error('Unable to find plugin \'' + pluginName + '\'');
}
});
}
// validate command
if (typeof _shelljs2.default[fnName] !== 'function') {
console.error('Error: Invalid ShellJS command: ' + fnName + '.');
console.error((0, _help2.default)());
return _config.EXIT_CODES.SHX_ERROR;
} else if (_config.CMD_BLACKLIST.indexOf(fnName) > -1) {
console.error('Warning: shx ' + fnName + ' is not supported');
console.error('Please run `shx help` for a list of commands.');
return _config.EXIT_CODES.SHX_ERROR;
}
var input = this !== null ? new _shelljs2.default.ShellString(this) : null;
// Set shell.config with parsed options
Object.assign(_shelljs2.default.config, parsedArgs);
// Workaround for sed syntax
var newArgs = void 0;
var ret = void 0;
if (fnName === 'sed') {
(function () {
newArgs = [];
var lookingForSubstString = true;
args.forEach(function (arg) {
var match = arg.match(/^s\/((?:\\\/|[^\/])+)\/((?:\\\/|[^\/])*)\/(g?)$/);
if (match && lookingForSubstString) {
var regexString = match[1].replace(/\\\//g, '/');
var replacement = match[2].replace(/\\\//g, '/').replace(/\\./g, '.');
var regexFlags = match[3];
newArgs.push(new RegExp(regexString, regexFlags));
newArgs.push(replacement);
lookingForSubstString = false;
} else {
newArgs.push(arg);
}
});
ret = _shelljs2.default[fnName].apply(input, newArgs);
})();
} else {
ret = _shelljs2.default[fnName].apply(input, args);
}
if (ret === null) ret = _shelljs2.default.ShellString('', '', 1);
var code = ret.hasOwnProperty('code') && ret.code;
if ((fnName === 'pwd' || fnName === 'which') && !ret.match(/\n$/) && ret.length > 1) {
ret += '\n';
}
// echo already prints
if (fnName !== 'echo') (0, _printCmdRet.printCmdRet)(ret);
if (typeof ret === 'boolean') {
code = ret ? 0 : 1;
}
if (typeof code === 'number') {
return code;
} else if (_shelljs2.default.error()) {
return _config.EXIT_CODES.CMD_FAILED;
}
return _config.EXIT_CODES.SUCCESS;
}