diff options
Diffstat (limited to 'deps/npm/node_modules/update-notifier/node_modules/boxen/node_modules/term-size/node_modules/execa/index.js')
-rw-r--r-- | deps/npm/node_modules/update-notifier/node_modules/boxen/node_modules/term-size/node_modules/execa/index.js | 285 |
1 files changed, 211 insertions, 74 deletions
diff --git a/deps/npm/node_modules/update-notifier/node_modules/boxen/node_modules/term-size/node_modules/execa/index.js b/deps/npm/node_modules/update-notifier/node_modules/boxen/node_modules/term-size/node_modules/execa/index.js index 5f3714a084..74ba8ee2de 100644 --- a/deps/npm/node_modules/update-notifier/node_modules/boxen/node_modules/term-size/node_modules/execa/index.js +++ b/deps/npm/node_modules/update-notifier/node_modules/boxen/node_modules/term-size/node_modules/execa/index.js @@ -1,53 +1,64 @@ 'use strict'; -var childProcess = require('child_process'); -var crossSpawnAsync = require('cross-spawn-async'); -var stripEof = require('strip-eof'); -var objectAssign = require('object-assign'); -var npmRunPath = require('npm-run-path'); -var isStream = require('is-stream'); -var pathKey = require('path-key')(); -var TEN_MEBIBYTE = 1024 * 1024 * 10; +const childProcess = require('child_process'); +const util = require('util'); +const crossSpawn = require('cross-spawn'); +const stripEof = require('strip-eof'); +const npmRunPath = require('npm-run-path'); +const isStream = require('is-stream'); +const _getStream = require('get-stream'); +const pFinally = require('p-finally'); +const onExit = require('signal-exit'); +const errname = require('./lib/errname'); +const stdio = require('./lib/stdio'); + +const TEN_MEGABYTES = 1000 * 1000 * 10; function handleArgs(cmd, args, opts) { - var parsed; + let parsed; + + if (opts && opts.env && opts.extendEnv !== false) { + opts.env = Object.assign({}, process.env, opts.env); + } if (opts && opts.__winShell === true) { delete opts.__winShell; parsed = { command: cmd, - args: args, + args, options: opts, file: cmd, original: cmd }; } else { - parsed = crossSpawnAsync._parse(cmd, args, opts); + parsed = crossSpawn._parse(cmd, args, opts); } - opts = objectAssign({ - maxBuffer: TEN_MEBIBYTE, + opts = Object.assign({ + maxBuffer: TEN_MEGABYTES, stripEof: true, preferLocal: true, - encoding: 'utf8' + localDir: parsed.options.cwd || process.cwd(), + encoding: 'utf8', + reject: true, + cleanup: true }, parsed.options); + opts.stdio = stdio(opts); + if (opts.preferLocal) { - opts.env = objectAssign({}, opts.env || process.env); - opts.env[pathKey] = npmRunPath({ - cwd: opts.cwd, - path: opts.env[pathKey] - }); + opts.env = npmRunPath.env(Object.assign({}, opts, {cwd: opts.localDir})); } return { cmd: parsed.command, args: parsed.args, - opts: opts + opts, + parsed }; } function handleInput(spawned, opts) { - var input = opts.input; + const input = opts.input; if (input === null || input === undefined) { return; @@ -61,7 +72,7 @@ function handleInput(spawned, opts) { } function handleOutput(opts, val) { - if (opts.stripEof) { + if (val && opts.stripEof) { val = stripEof(val); } @@ -69,54 +80,193 @@ function handleOutput(opts, val) { } function handleShell(fn, cmd, opts) { - var file; - var args; + let file = '/bin/sh'; + let args = ['-c', cmd]; - opts = objectAssign({}, opts); + opts = Object.assign({}, opts); if (process.platform === 'win32') { opts.__winShell = true; file = process.env.comspec || 'cmd.exe'; - args = ['/s', '/c', '"' + cmd + '"']; + args = ['/s', '/c', `"${cmd}"`]; opts.windowsVerbatimArguments = true; - } else { - file = '/bin/sh'; - args = ['-c', cmd]; } if (opts.shell) { file = opts.shell; + delete opts.shell; } return fn(file, args, opts); } -module.exports = function (cmd, args, opts) { - var spawned; +function getStream(process, stream, encoding, maxBuffer) { + if (!process[stream]) { + return null; + } - var promise = new Promise(function (resolve, reject) { - var parsed = handleArgs(cmd, args, opts); + let ret; - spawned = childProcess.execFile(parsed.cmd, parsed.args, parsed.opts, function (err, stdout, stderr) { - if (err) { - err.stdout = stdout; - err.stderr = stderr; - err.message += stdout; - reject(err); - return; - } + if (encoding) { + ret = _getStream(process[stream], { + encoding, + maxBuffer + }); + } else { + ret = _getStream.buffer(process[stream], {maxBuffer}); + } - resolve({ - stdout: handleOutput(parsed.opts, stdout), - stderr: handleOutput(parsed.opts, stderr) - }); + return ret.catch(err => { + err.stream = stream; + err.message = `${stream} ${err.message}`; + throw err; + }); +} + +module.exports = (cmd, args, opts) => { + let joinedCmd = cmd; + + if (Array.isArray(args) && args.length > 0) { + joinedCmd += ' ' + args.join(' '); + } + + const parsed = handleArgs(cmd, args, opts); + const encoding = parsed.opts.encoding; + const maxBuffer = parsed.opts.maxBuffer; + + let spawned; + try { + spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts); + } catch (err) { + return Promise.reject(err); + } + + let removeExitHandler; + if (parsed.opts.cleanup) { + removeExitHandler = onExit(() => { + spawned.kill(); + }); + } + + let timeoutId = null; + let timedOut = false; + + const cleanupTimeout = () => { + if (timeoutId) { + clearTimeout(timeoutId); + timeoutId = null; + } + }; + + if (parsed.opts.timeout > 0) { + timeoutId = setTimeout(() => { + timeoutId = null; + timedOut = true; + spawned.kill(parsed.opts.killSignal); + }, parsed.opts.timeout); + } + + const processDone = new Promise(resolve => { + spawned.on('exit', (code, signal) => { + cleanupTimeout(); + resolve({code, signal}); }); - crossSpawnAsync._enoent.hookChildProcess(spawned, parsed); + spawned.on('error', err => { + cleanupTimeout(); + resolve({err}); + }); - handleInput(spawned, parsed.opts); + if (spawned.stdin) { + spawned.stdin.on('error', err => { + cleanupTimeout(); + resolve({err}); + }); + } }); + function destroy() { + if (spawned.stdout) { + spawned.stdout.destroy(); + } + + if (spawned.stderr) { + spawned.stderr.destroy(); + } + } + + const promise = pFinally(Promise.all([ + processDone, + getStream(spawned, 'stdout', encoding, maxBuffer), + getStream(spawned, 'stderr', encoding, maxBuffer) + ]).then(arr => { + const result = arr[0]; + const stdout = arr[1]; + const stderr = arr[2]; + + let err = result.err; + const code = result.code; + const signal = result.signal; + + if (removeExitHandler) { + removeExitHandler(); + } + + if (err || code !== 0 || signal !== null) { + if (!err) { + let output = ''; + + if (Array.isArray(parsed.opts.stdio)) { + if (parsed.opts.stdio[2] !== 'inherit') { + output += output.length > 0 ? stderr : `\n${stderr}`; + } + + if (parsed.opts.stdio[1] !== 'inherit') { + output += `\n${stdout}`; + } + } else if (parsed.opts.stdio !== 'inherit') { + output = `\n${stderr}${stdout}`; + } + + err = new Error(`Command failed: ${joinedCmd}${output}`); + err.code = code < 0 ? errname(code) : code; + } + + // TODO: missing some timeout logic for killed + // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203 + // err.killed = spawned.killed || killed; + err.killed = err.killed || spawned.killed; + + err.stdout = stdout; + err.stderr = stderr; + err.failed = true; + err.signal = signal || null; + err.cmd = joinedCmd; + err.timedOut = timedOut; + + if (!parsed.opts.reject) { + return err; + } + + throw err; + } + + return { + stdout: handleOutput(parsed.opts, stdout), + stderr: handleOutput(parsed.opts, stderr), + code: 0, + failed: false, + killed: false, + signal: null, + cmd: joinedCmd, + timedOut: false + }; + }), destroy); + + crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed); + + handleInput(spawned, parsed.opts); + spawned.then = promise.then.bind(promise); spawned.catch = promise.catch.bind(promise); @@ -125,48 +275,35 @@ module.exports = function (cmd, args, opts) { module.exports.stdout = function () { // TODO: set `stderr: 'ignore'` when that option is implemented - return module.exports.apply(null, arguments).then(function (x) { - return x.stdout; - }); + return module.exports.apply(null, arguments).then(x => x.stdout); }; module.exports.stderr = function () { // TODO: set `stdout: 'ignore'` when that option is implemented - return module.exports.apply(null, arguments).then(function (x) { - return x.stderr; - }); + return module.exports.apply(null, arguments).then(x => x.stderr); }; -module.exports.shell = function (cmd, opts) { - return handleShell(module.exports, cmd, opts); -}; - -module.exports.spawn = function (cmd, args, opts) { - var parsed = handleArgs(cmd, args, opts); - var spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts); +module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts); - crossSpawnAsync._enoent.hookChildProcess(spawned, parsed); - - return spawned; -}; - -module.exports.sync = function (cmd, args, opts) { - var parsed = handleArgs(cmd, args, opts); +module.exports.sync = (cmd, args, opts) => { + const parsed = handleArgs(cmd, args, opts); if (isStream(parsed.opts.input)) { throw new TypeError('The `input` option cannot be a stream in sync mode'); } - var result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts); + const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts); - if (parsed.opts.stripEof) { - result.stdout = stripEof(result.stdout); - result.stderr = stripEof(result.stderr); + if (result.error || result.status !== 0) { + throw (result.error || new Error(result.stderr === '' ? result.stdout : result.stderr)); } + result.stdout = handleOutput(parsed.opts, result.stdout); + result.stderr = handleOutput(parsed.opts, result.stderr); + return result; }; -module.exports.shellSync = function (cmd, opts) { - return handleShell(module.exports.sync, cmd, opts); -}; +module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts); + +module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.'); |