Skip to content
Myr

Improve code readability

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent b7752fb · Browse files · 1 file+74 added−74 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+74 added−74 removed

  • other:+74 added−74 removed

1 file+74 added−74 removed

modified:index.js+74 added-74 removed
11 unmodified lines
12const stdio = require('./lib/stdio');1314const TEN_MEGABYTES = 1000 * 1000 * 10;1516function handleArgs(cmd, args, opts) {16function handleArgs(command, args, options) {17 let parsed;1819 opts = Object.assign({19 options = Object.assign({20 extendEnv: true,21 env: {}22 }, opts);22 }, options);2324 if (opts.extendEnv) {25 opts.env = Object.assign({}, process.env, opts.env);24 if (options.extendEnv) {25 options.env = Object.assign({}, process.env, options.env);26 }2728 if (opts.__winShell === true) {29 delete opts.__winShell;28 if (options.__winShell === true) {29 delete options.__winShell;30 parsed = {31 command: cmd,31 command,32 args,33 options: opts,34 file: cmd,33 options,34 file: command,35 original: {36 cmd,36 command,37 args38 }39 };40 } else {41 parsed = crossSpawn._parse(cmd, args, opts);41 parsed = crossSpawn._parse(command, args, options);42 }4344 opts = Object.assign({44 options = Object.assign({45 maxBuffer: TEN_MEGABYTES,46 buffer: true,47 stripFinalNewline: true,48 preferLocal: true,
3 unmodified lines
52 cleanup: true53 }, parsed.options);5455 // TODO: Remove in the next major release56 if (opts.stripEof === false) {57 opts.stripFinalNewline = false;56 if (options.stripEof === false) {57 options.stripFinalNewline = false;58 }5960 opts.stdio = stdio(opts);60 options.stdio = stdio(options);6162 if (opts.preferLocal) {63 opts.env = npmRunPath.env(Object.assign({}, opts, {cwd: opts.localDir}));62 if (options.preferLocal) {63 options.env = npmRunPath.env(Object.assign({}, options, {cwd: options.localDir}));64 }6566 if (opts.detached) {66 if (options.detached) {67 // #11568 opts.cleanup = false;68 options.cleanup = false;69 }7071 if (process.platform === 'win32' && path.basename(parsed.command) === 'cmd.exe') {72 // #11673 parsed.args.unshift('/q');74 }7576 return {77 cmd: parsed.command,77 command: parsed.command,78 args: parsed.args,79 opts,79 options,80 parsed81 };82}83
8 unmodified lines
92 spawned.stdin.end(input);93 }94}9596function handleOutput(opts, val) {97 if (val && opts.stripFinalNewline) {98 val = stripFinalNewline(val);96function handleOutput(options, value) {97 if (value && options.stripFinalNewline) {98 value = stripFinalNewline(value);99 }100101 return val;101 return value;102}103104function handleShell(fn, cmd, opts) {104function handleShell(fn, command, options) {105 let file = '/bin/sh';106 let args = ['-c', cmd];106 let args = ['-c', command];107108 opts = Object.assign({}, opts);108 options = Object.assign({}, options);109110 if (process.platform === 'win32') {111 opts.__winShell = true;111 options.__winShell = true;112 file = process.env.comspec || 'cmd.exe';113 args = ['/s', '/c', `"${cmd}"`];114 opts.windowsVerbatimArguments = true;113 args = ['/s', '/c', `"${command}"`];114 options.windowsVerbatimArguments = true;115 }116117 if (opts.shell) {118 file = opts.shell;119 delete opts.shell;117 if (options.shell) {118 file = options.shell;119 delete options.shell;120 }121122 return fn(file, args, opts);122 return fn(file, args, options);123}124125function getStream(process, stream, {encoding, buffer, maxBuffer}) {126 if (!process[stream]) {
30 unmodified lines
157158 let {error} = result;159 const {code, signal} = result;160161 const {parsed, joinedCmd} = options;161 const {parsed, joinedCommand} = options;162 const timedOut = options.timedOut || false;163164 if (!error) {165 let output = '';166167 if (Array.isArray(parsed.opts.stdio)) {168 if (parsed.opts.stdio[2] !== 'inherit') {167 if (Array.isArray(parsed.options.stdio)) {168 if (parsed.options.stdio[2] !== 'inherit') {169 output += output.length > 0 ? stderr : `\n${stderr}`;170 }171172 if (parsed.opts.stdio[1] !== 'inherit') {172 if (parsed.options.stdio[1] !== 'inherit') {173 output += `\n${stdout}`;174 }175 } else if (parsed.opts.stdio !== 'inherit') {175 } else if (parsed.options.stdio !== 'inherit') {176 output = `\n${stderr}${stdout}`;177 }178179 error = new Error(`Command failed: ${joinedCmd}${output}`);179 error = new Error(`Command failed: ${joinedCommand}${output}`);180 error.code = code < 0 ? errname(code) : code;181 }182183 error.stdout = stdout;184 error.stderr = stderr;185 error.failed = true;186 error.signal = signal || null;187 error.cmd = joinedCmd;187 error.cmd = joinedCommand;188 error.timedOut = timedOut;189190 return error;191}192193function joinCmd(cmd, args) {194 let joinedCmd = cmd;193function joinCommand(command, args) {194 let joinedCommand = command;195196 if (Array.isArray(args) && args.length > 0) {197 joinedCmd += ' ' + args.join(' ');197 joinedCommand += ' ' + args.join(' ');198 }199200 return joinedCmd;200 return joinedCommand;201}202203module.exports = (cmd, args, opts) => {204 const parsed = handleArgs(cmd, args, opts);205 const {encoding, buffer, maxBuffer} = parsed.opts;206 const joinedCmd = joinCmd(cmd, args);203module.exports = (command, args, options) => {204 const parsed = handleArgs(command, args, options);205 const {encoding, buffer, maxBuffer} = parsed.options;206 const joinedCommand = joinCommand(command, args);207208 let spawned;209 try {210 spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);210 spawned = childProcess.spawn(parsed.command, parsed.args, parsed.options);211 } catch (error) {212 return Promise.reject(error);213 }214215 let removeExitHandler;216 if (parsed.opts.cleanup) {216 if (parsed.options.cleanup) {217 removeExitHandler = onExit(() => {218 spawned.kill();219 });220 }
11 unmodified lines
232 removeExitHandler();233 }234 };235236 if (parsed.opts.timeout > 0) {236 if (parsed.options.timeout > 0) {237 timeoutId = setTimeout(() => {238 timeoutId = null;239 timedOut = true;240 spawned.kill(parsed.opts.killSignal);241 }, parsed.opts.timeout);240 spawned.kill(parsed.options.killSignal);241 }, parsed.options.timeout);242 }243244 const processDone = new Promise(resolve => {245 spawned.on('exit', (code, signal) => {
34 unmodified lines
280 result.stderr = arr[2];281282 if (result.error || result.code !== 0 || result.signal !== null) {283 const error = makeError(result, {284 joinedCmd,284 joinedCommand,285 parsed,286 timedOut287 });288
1 unmodified line
290 // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203291 // error.killed = spawned.killed || killed;292 error.killed = error.killed || spawned.killed;293294 if (!parsed.opts.reject) {294 if (!parsed.options.reject) {295 return error;296 }297298 throw error;299 }300301 return {302 stdout: handleOutput(parsed.opts, result.stdout),303 stderr: handleOutput(parsed.opts, result.stderr),302 stdout: handleOutput(parsed.options, result.stdout),303 stderr: handleOutput(parsed.options, result.stderr),304 code: 0,305 failed: false,306 killed: false,307 signal: null,308 cmd: joinedCmd,308 cmd: joinedCommand,309 timedOut: false310 };311 }), destroy);312313 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);314315 handleInput(spawned, parsed.opts.input);315 handleInput(spawned, parsed.options.input);316317 spawned.then = (onfulfilled, onrejected) => handlePromise().then(onfulfilled, onrejected);318 spawned.catch = onrejected => handlePromise().catch(onrejected);319
5 unmodified lines
325326// TODO: set `stdout: 'ignore'` when that option is implemented327module.exports.stderr = (...args) => module.exports(...args).then(x => x.stderr);328329module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts);329module.exports.shell = (command, options) => handleShell(module.exports, command, options);330331module.exports.sync = (cmd, args, opts) => {332 const parsed = handleArgs(cmd, args, opts);333 const joinedCmd = joinCmd(cmd, args);331module.exports.sync = (command, args, options) => {332 const parsed = handleArgs(command, args, options);333 const joinedCommand = joinCommand(command, args);334335 if (isStream(parsed.opts.input)) {335 if (isStream(parsed.options.input)) {336 throw new TypeError('The `input` option cannot be a stream in sync mode');337 }338339 const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);339 const result = childProcess.spawnSync(parsed.command, parsed.args, parsed.options);340 result.code = result.status;341342 if (result.error || result.status !== 0 || result.signal !== null) {343 const error = makeError(result, {344 joinedCmd,344 joinedCommand,345 parsed346 });347348 if (!parsed.opts.reject) {348 if (!parsed.options.reject) {349 return error;350 }351352 throw error;353 }354355 return {356 stdout: handleOutput(parsed.opts, result.stdout),357 stderr: handleOutput(parsed.opts, result.stderr),356 stdout: handleOutput(parsed.options, result.stdout),357 stderr: handleOutput(parsed.options, result.stderr),358 code: 0,359 failed: false,360 signal: null,361 cmd: joinedCmd,361 cmd: joinedCommand,362 timedOut: false363 };364};365366module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts);366module.exports.shellSync = (command, options) => handleShell(module.exports.sync, command, options);