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
Provenance
- Commit
37d9bd9- Tree
d29a6d9- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
b7752fb
1 file changed+74 added−74 removed
modified:index.js+74 added-74 removed
11 unmodified lines12 const stdio = require('./lib/stdio');13 14 const TEN_MEGABYTES = 1000 * 1000 * 10;15 16-function handleArgs(cmd, args, opts) {16+function handleArgs(command, args, options) {17 let parsed;18 19- opts = Object.assign({19+ options = Object.assign({20 extendEnv: true,21 env: {}22- }, opts);22+ }, options);23 24- 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 }27 28- 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 }43 44- opts = Object.assign({44+ options = Object.assign({45 maxBuffer: TEN_MEGABYTES,46 buffer: true,47 stripFinalNewline: true,48 preferLocal: true,3 unmodified lines52 cleanup: true53 }, parsed.options);54 55 // 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 }59 60- opts.stdio = stdio(opts);60+ options.stdio = stdio(options);61 62- 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 }65 66- if (opts.detached) {66+ if (options.detached) {67 // #11568- opts.cleanup = false;68+ options.cleanup = false;69 }70 71 if (process.platform === 'win32' && path.basename(parsed.command) === 'cmd.exe') {72 // #11673 parsed.args.unshift('/q');74 }75 76 return {77- cmd: parsed.command,77+ command: parsed.command,78 args: parsed.args,79- opts,79+ options,80 parsed81 };82 }83 8 unmodified lines92 spawned.stdin.end(input);93 }94 }95 96-function handleOutput(opts, val) {97- if (val && opts.stripFinalNewline) {98- val = stripFinalNewline(val);96+function handleOutput(options, value) {97+ if (value && options.stripFinalNewline) {98+ value = stripFinalNewline(value);99 }100 101- return val;101+ return value;102 }103 104-function handleShell(fn, cmd, opts) {104+function handleShell(fn, command, options) {105 let file = '/bin/sh';106- let args = ['-c', cmd];106+ let args = ['-c', command];107 108- opts = Object.assign({}, opts);108+ options = Object.assign({}, options);109 110 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 }116 117- if (opts.shell) {118- file = opts.shell;119- delete opts.shell;117+ if (options.shell) {118+ file = options.shell;119+ delete options.shell;120 }121 122- return fn(file, args, opts);122+ return fn(file, args, options);123 }124 125 function getStream(process, stream, {encoding, buffer, maxBuffer}) {126 if (!process[stream]) {30 unmodified lines157 158 let {error} = result;159 const {code, signal} = result;160 161- const {parsed, joinedCmd} = options;161+ const {parsed, joinedCommand} = options;162 const timedOut = options.timedOut || false;163 164 if (!error) {165 let output = '';166 167- 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 }171 172- 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 }178 179- error = new Error(`Command failed: ${joinedCmd}${output}`);179+ error = new Error(`Command failed: ${joinedCommand}${output}`);180 error.code = code < 0 ? errname(code) : code;181 }182 183 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;189 190 return error;191 }192 193-function joinCmd(cmd, args) {194- let joinedCmd = cmd;193+function joinCommand(command, args) {194+ let joinedCommand = command;195 196 if (Array.isArray(args) && args.length > 0) {197- joinedCmd += ' ' + args.join(' ');197+ joinedCommand += ' ' + args.join(' ');198 }199 200- return joinedCmd;200+ return joinedCommand;201 }202 203-module.exports = (cmd, args, opts) => {204- const parsed = handleArgs(cmd, args, opts);205- const {encoding, buffer, maxBuffer} = parsed.opts;206- const joinedCmd = joinCmd(cmd, args);203+module.exports = (command, args, options) => {204+ const parsed = handleArgs(command, args, options);205+ const {encoding, buffer, maxBuffer} = parsed.options;206+ const joinedCommand = joinCommand(command, args);207 208 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 }214 215 let removeExitHandler;216- if (parsed.opts.cleanup) {216+ if (parsed.options.cleanup) {217 removeExitHandler = onExit(() => {218 spawned.kill();219 });220 }11 unmodified lines232 removeExitHandler();233 }234 };235 236- 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 }243 244 const processDone = new Promise(resolve => {245 spawned.on('exit', (code, signal) => {34 unmodified lines280 result.stderr = arr[2];281 282 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 line290 // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203291 // error.killed = spawned.killed || killed;292 error.killed = error.killed || spawned.killed;293 294- if (!parsed.opts.reject) {294+ if (!parsed.options.reject) {295 return error;296 }297 298 throw error;299 }300 301 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);312 313 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);314 315- handleInput(spawned, parsed.opts.input);315+ handleInput(spawned, parsed.options.input);316 317 spawned.then = (onfulfilled, onrejected) => handlePromise().then(onfulfilled, onrejected);318 spawned.catch = onrejected => handlePromise().catch(onrejected);319 5 unmodified lines325 326 // TODO: set `stdout: 'ignore'` when that option is implemented327 module.exports.stderr = (...args) => module.exports(...args).then(x => x.stderr);328 329-module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts);329+module.exports.shell = (command, options) => handleShell(module.exports, command, options);330 331-module.exports.sync = (cmd, args, opts) => {332- const parsed = handleArgs(cmd, args, opts);333- const joinedCmd = joinCmd(cmd, args);331+module.exports.sync = (command, args, options) => {332+ const parsed = handleArgs(command, args, options);333+ const joinedCommand = joinCommand(command, args);334 335- 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 }338 339- 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;341 342 if (result.error || result.status !== 0 || result.signal !== null) {343 const error = makeError(result, {344- joinedCmd,344+ joinedCommand,345 parsed346 });347 348- if (!parsed.opts.reject) {348+ if (!parsed.options.reject) {349 return error;350 }351 352 throw error;353 }354 355 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 };365 366-module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts);366+module.exports.shellSync = (command, options) => handleShell(module.exports.sync, command, options);