diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/cross-spawn-async/lib | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/cross-spawn-async/lib')
-rw-r--r-- | node_modules/cross-spawn-async/lib/enoent.js | 51 | ||||
-rw-r--r-- | node_modules/cross-spawn-async/lib/parse.js | 128 | ||||
-rw-r--r-- | node_modules/cross-spawn-async/lib/resolveCommand.js | 38 |
3 files changed, 217 insertions, 0 deletions
diff --git a/node_modules/cross-spawn-async/lib/enoent.js b/node_modules/cross-spawn-async/lib/enoent.js new file mode 100644 index 000000000..3254833ae --- /dev/null +++ b/node_modules/cross-spawn-async/lib/enoent.js @@ -0,0 +1,51 @@ +'use strict'; + +var isWin = process.platform === 'win32'; + +function notFoundError(command, syscall) { + var err; + + err = new Error(syscall + ' ' + command + ' ENOENT'); + err.code = err.errno = 'ENOENT'; + err.syscall = syscall + ' ' + command; + + return err; +} + +function hookChildProcess(cp, parsed) { + var originalEmit; + + if (!isWin) { + return; + } + + originalEmit = cp.emit; + cp.emit = function (name, arg1) { + var err; + + // If emitting "exit" event and exit code is 1, we need to check if + // the command exists and emit an "error" instead + // See: https://github.com/IndigoUnited/node-cross-spawn/issues/16 + if (name === 'exit') { + err = verifyENOENT(arg1, parsed, 'spawn'); + + if (err) { + return originalEmit.call(cp, 'error', err); + } + } + + return originalEmit.apply(cp, arguments); + }; +} + +function verifyENOENT(status, parsed, syscall) { + if (isWin && status === 1 && !parsed.file) { + return notFoundError(parsed.original, syscall); + } + + return null; +} + +module.exports.hookChildProcess = hookChildProcess; +module.exports.verifyENOENT = verifyENOENT; +module.exports.notFoundError = notFoundError; diff --git a/node_modules/cross-spawn-async/lib/parse.js b/node_modules/cross-spawn-async/lib/parse.js new file mode 100644 index 000000000..923e05e7a --- /dev/null +++ b/node_modules/cross-spawn-async/lib/parse.js @@ -0,0 +1,128 @@ +'use strict'; + +var fs = require('fs'); +var LRU = require('lru-cache'); +var resolveCommand = require('./resolveCommand'); + +var isWin = process.platform === 'win32'; +var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec + +function readShebang(command) { + var buffer; + var fd; + var match; + var shebang; + + // Check if it is in the cache first + if (shebangCache.has(command)) { + return shebangCache.get(command); + } + + // Read the first 150 bytes from the file + buffer = new Buffer(150); + + try { + fd = fs.openSync(command, 'r'); + fs.readSync(fd, buffer, 0, 150, 0); + fs.closeSync(fd); + } catch (e) { /* empty */ } + + // Check if it is a shebang + match = buffer.toString().trim().match(/#!(.+)/i); + + if (match) { + shebang = match[1].replace(/\/usr\/bin\/env\s+/i, ''); // Remove /usr/bin/env + } + + // Store the shebang in the cache + shebangCache.set(command, shebang); + + return shebang; +} + +function escapeArg(arg, quote) { + // Convert to string + arg = '' + arg; + + // If we are not going to quote the argument, + // escape shell metacharacters, including double and single quotes: + if (!quote) { + arg = arg.replace(/([\(\)%!\^<>&|;,"'\s])/g, '^$1'); + } else { + // Sequence of backslashes followed by a double quote: + // double up all the backslashes and escape the double quote + arg = arg.replace(/(\\*)"/g, '$1$1\\"'); + + // Sequence of backslashes followed by the end of the string + // (which will become a double quote later): + // double up all the backslashes + arg = arg.replace(/(\\*)$/, '$1$1'); + + // All other backslashes occur literally + + // Quote the whole thing: + arg = '"' + arg + '"'; + } + + return arg; +} + +function escapeCommand(command) { + // Do not escape if this command is not dangerous.. + // We do this so that commands like "echo" or "ifconfig" work + // Quoting them, will make them unaccessible + return /^[a-z0-9_-]+$/i.test(command) ? command : escapeArg(command, true); +} + +function parse(command, args, options) { + var shebang; + var applyQuotes; + var file; + var original; + + // Normalize arguments, similar to nodejs + if (args && !Array.isArray(args)) { + options = args; + args = null; + } + + args = args ? args.slice(0) : []; // Clone array to avoid changing the original + options = options || {}; + original = command; + + if (isWin) { + // Detect & add support for shebangs + file = resolveCommand(command); + file = file || resolveCommand(command, true); + shebang = file && readShebang(file); + + if (shebang) { + args.unshift(file); + command = shebang; + } + + // Escape command & arguments + applyQuotes = command !== 'echo'; // Do not quote arguments for the special "echo" command + command = escapeCommand(command); + args = args.map(function (arg) { + return escapeArg(arg, applyQuotes); + }); + + // Use cmd.exe + args = ['/s', '/c', '"' + command + (args.length ? ' ' + args.join(' ') : '') + '"']; + command = process.env.comspec || 'cmd.exe'; + + // Tell node's spawn that the arguments are already escaped + options.windowsVerbatimArguments = true; + } + + return { + command: command, + args: args, + options: options, + file: file, + original: original, + }; +} + +module.exports = parse; diff --git a/node_modules/cross-spawn-async/lib/resolveCommand.js b/node_modules/cross-spawn-async/lib/resolveCommand.js new file mode 100644 index 000000000..391d3d9b0 --- /dev/null +++ b/node_modules/cross-spawn-async/lib/resolveCommand.js @@ -0,0 +1,38 @@ +'use strict'; + +var path = require('path'); +var which = require('which'); +var LRU = require('lru-cache'); + +var commandCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec +var hasSepInPathRegExp = new RegExp(process.platform === 'win32' ? /[\/\\]/ : /\//); + +function resolveCommand(command, noExtension) { + var resolved; + + // If command looks like a file path, make it absolute to make it canonical + // and also to circuvent a bug in which, see: https://github.com/npm/node-which/issues/33 + if (hasSepInPathRegExp.test(command)) { + command = path.resolve(command); + } + + noExtension = !!noExtension; + resolved = commandCache.get(command + '!' + noExtension); + + // Check if its resolved in the cache + if (commandCache.has(command)) { + return commandCache.get(command); + } + + try { + resolved = !noExtension ? + which.sync(command) : + which.sync(command, { pathExt: path.delimiter + (process.env.PATHEXT || '') }); + } catch (e) { /* empty */ } + + commandCache.set(command + '!' + noExtension, resolved); + + return resolved; +} + +module.exports = resolveCommand; |