diff options
Diffstat (limited to 'tools/eslint/node_modules/shelljs/src/common.js')
-rw-r--r-- | tools/eslint/node_modules/shelljs/src/common.js | 106 |
1 files changed, 80 insertions, 26 deletions
diff --git a/tools/eslint/node_modules/shelljs/src/common.js b/tools/eslint/node_modules/shelljs/src/common.js index 8d10f3dde7..33198bd8a0 100644 --- a/tools/eslint/node_modules/shelljs/src/common.js +++ b/tools/eslint/node_modules/shelljs/src/common.js @@ -5,13 +5,15 @@ var _ls = require('./ls'); // Module globals var config = { silent: false, - fatal: false + fatal: false, + verbose: false, }; exports.config = config; var state = { error: null, currentCmd: 'shell.js', + previousDir: null, tempDir: null }; exports.state = state; @@ -21,7 +23,7 @@ exports.platform = platform; function log() { if (!config.silent) - console.log.apply(this, arguments); + console.error.apply(console, arguments); } exports.log = log; @@ -29,10 +31,14 @@ exports.log = log; function error(msg, _continue) { if (state.error === null) state.error = ''; - state.error += state.currentCmd + ': ' + msg + '\n'; + var log_entry = state.currentCmd + ': ' + msg; + if (state.error === '') + state.error = log_entry; + else + state.error += '\n' + log_entry; if (msg.length > 0) - log(state.error); + log(log_entry); if (config.fatal) process.exit(1); @@ -49,38 +55,69 @@ function ShellString(str) { } exports.ShellString = ShellString; -// Returns {'alice': true, 'bob': false} when passed a dictionary, e.g.: +// Return the home directory in a platform-agnostic way, with consideration for +// older versions of node +function getUserHome() { + var result; + if (os.homedir) + result = os.homedir(); // node 3+ + else + result = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']; + return result; +} +exports.getUserHome = getUserHome; + +// Returns {'alice': true, 'bob': false} when passed a string and dictionary as follows: // parseOptions('-a', {'a':'alice', 'b':'bob'}); -function parseOptions(str, map) { +// Returns {'reference': 'string-value', 'bob': false} when passed two dictionaries of the form: +// parseOptions({'-r': 'string-value'}, {'r':'reference', 'b':'bob'}); +function parseOptions(opt, map) { if (!map) error('parseOptions() internal error: no map given'); // All options are false by default var options = {}; - for (var letter in map) - options[map[letter]] = false; + for (var letter in map) { + if (map[letter][0] !== '!') + options[map[letter]] = false; + } - if (!str) + if (!opt) return options; // defaults - if (typeof str !== 'string') - error('parseOptions() internal error: wrong str'); - - // e.g. match[1] = 'Rf' for str = '-Rf' - var match = str.match(/^\-(.+)/); - if (!match) - return options; + var optionName; + if (typeof opt === 'string') { + if (opt[0] !== '-') + return options; - // e.g. chars = ['R', 'f'] - var chars = match[1].split(''); - - chars.forEach(function(c) { - if (c in map) - options[map[c]] = true; - else - error('option not recognized: '+c); - }); + // e.g. chars = ['R', 'f'] + var chars = opt.slice(1).split(''); + chars.forEach(function(c) { + if (c in map) { + optionName = map[c]; + if (optionName[0] === '!') + options[optionName.slice(1, optionName.length-1)] = false; + else + options[optionName] = true; + } else { + error('option not recognized: '+c); + } + }); + } else if (typeof opt === 'object') { + for (var key in opt) { + // key is a string of the form '-r', '-d', etc. + var c = key[1]; + if (c in map) { + optionName = map[c]; + options[optionName] = opt[key]; // assign the given value + } else { + error('option not recognized: '+c); + } + } + } else { + error('options must be strings or key-value pairs'); + } return options; } exports.parseOptions = parseOptions; @@ -178,11 +215,28 @@ function wrap(cmd, fn, options) { try { var args = [].slice.call(arguments, 0); + if (config.verbose) { + args.unshift(cmd); + console.log.apply(console, args); + args.shift(); + } + if (options && options.notUnix) { retValue = fn.apply(this, args); } else { - if (args.length === 0 || typeof args[0] !== 'string' || args[0][0] !== '-') + if (typeof args[0] === 'object' && args[0].constructor.name === 'Object') { + args = args; // object count as options + } else if (args.length === 0 || typeof args[0] !== 'string' || args[0].length <= 1 || args[0][0] !== '-') { args.unshift(''); // only add dummy option if '-option' not already present + } + // Expand the '~' if appropriate + var homeDir = getUserHome(); + args = args.map(function(arg) { + if (typeof arg === 'string' && arg.slice(0, 2) === '~/' || arg === '~') + return arg.replace(/^~/, homeDir); + else + return arg; + }); retValue = fn.apply(this, args); } } catch (e) { |