Skip to content
Myr

Fix functions coding style (#296)

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

authored by ehmicky · committed by Sindre Sorhus · parent ab83d8d · Browse files · 2 files+52 added−52 removed
This commit

State

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

Diff stats

2 files+52 added−52 removed

  • tests:+10 added−10 removed
  • other:+42 added−42 removed

2 files+52 added−52 removed

modified:test/test.js+10 added-10 removed
485 unmodified lines
486 const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});487 t.is(signal, undefined);488});489490async function testExitCode(t, num) {490const testExitCode = async (t, num) => {491 const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});492 t.is(exitCode, num);493}493};494495test('error.exitCode is 2', testExitCode, 2);496test('error.exitCode is 3', testExitCode, 3);497test('error.exitCode is 4', testExitCode, 4);
26 unmodified lines
524 const {timedOut} = execa.sync('noop');525 t.false(timedOut);526});527528async function errorMessage(t, expected, ...args) {528const errorMessage = async (t, expected, ...args) => {529 await t.throwsAsync(execa('exit', args), {message: expected});530}530};531532errorMessage.title = (message, expected) => `error.message matches: ${expected}`;533534test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');535test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');536537async function command(t, expected, ...args) {537const command = async (t, expected, ...args) => {538 const {command: failCommand} = await t.throwsAsync(execa('fail', args));539 t.is(failCommand, `fail${expected}`);540541 const {command} = await execa('noop', args);542 t.is(command, `noop${expected}`);543}543};544545command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;546547test(command, ' foo bar', 'foo', 'bar');548test(command, ' baz quz', 'baz', 'quz');549test(command, '');550551// When child process exits before parent process552async function spawnAndExit(t, cleanup, detached) {552const spawnAndExit = async (t, cleanup, detached) => {553 await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));554}554};555556test('spawnAndExit', spawnAndExit, false, false);557test('spawnAndExit cleanup', spawnAndExit, true, false);558test('spawnAndExit detached', spawnAndExit, false, true);559test('spawnAndExit cleanup detached', spawnAndExit, true, true);560561// When parent process exits before child process562async function spawnAndKill(t, signal, cleanup, detached, isKilled) {562const spawnAndKill = async (t, signal, cleanup, detached, isKilled) => {563 const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});564565 const pid = await pEvent(subprocess, 'message');566 t.true(Number.isInteger(pid));
8 unmodified lines
575576 if (!isKilled) {577 process.kill(pid, 'SIGKILL');578 }579}579};580581// Without `options.cleanup`:582// - on Windows subprocesses are killed if `options.detached: false`, but not583// if `options.detached: true`
254 unmodified lines
modified:index.js+42 added-42 removed
16 unmodified lines
17const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;1819const SPACES_REGEXP = / +/g;2021function handleArgs(file, args, options = {}) {21const handleArgs = (file, args, options = {}) => {22 const parsed = crossSpawn._parse(file, args, options);23 file = parsed.command;24 args = parsed.args;25 options = parsed.options;
32 unmodified lines
58 args.unshift('/q');59 }6061 return {file, args, options, parsed};62}62};6364function handleInput(spawned, input) {64const handleInput = (spawned, input) => {65 // Checking for stdin is workaround for https://github.com/nodejs/node/issues/2685266 // TODO: Remove `|| spawned.stdin === undefined` once we drop support for Node.js <=12.2.067 if (input === undefined || spawned.stdin === undefined) {68 return;
3 unmodified lines
72 input.pipe(spawned.stdin);73 } else {74 spawned.stdin.end(input);75 }76}76};7778function handleOutput(options, value, error) {78const handleOutput = (options, value, error) => {79 if (typeof value !== 'string' && !Buffer.isBuffer(value)) {80 // When `execa.sync()` errors, we normalize it to '' to mimic `execa()`81 return error === undefined ? undefined : '';82 }
2 unmodified lines
85 return stripFinalNewline(value);86 }8788 return value;89}89};9091function makeAllStream(spawned) {91const makeAllStream = spawned => {92 if (!spawned.stdout && !spawned.stderr) {93 return;94 }95
7 unmodified lines
103 mixed.add(spawned.stderr);104 }105106 return mixed;107}107};108109async function getBufferedData(stream, streamPromise) {109const getBufferedData = async (stream, streamPromise) => {110 if (!stream) {111 return;112 }113
3 unmodified lines
117 return await streamPromise;118 } catch (error) {119 return error.bufferedData;120 }121}121};122123function getStreamPromise(stream, {encoding, buffer, maxBuffer}) {123const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {124 if (!stream) {125 return;126 }127
10 unmodified lines
138 return getStream(stream, {encoding, maxBuffer});139 }140141 return getStream.buffer(stream, {maxBuffer});142}142};143144async function getPromiseResult({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) {144const getPromiseResult = async ({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) => {145 const stdoutPromise = getStreamPromise(stdout, {encoding, buffer, maxBuffer});146 const stderrPromise = getStreamPromise(stderr, {encoding, buffer, maxBuffer});147 const allPromise = getStreamPromise(all, {encoding, buffer, maxBuffer: maxBuffer * 2});148
6 unmodified lines
155 getBufferedData(stderr, stderrPromise),156 getBufferedData(all, allPromise)157 ]);158 }159}159};160161function makeError(result, options) {161const makeError = (result, options) => {162 const {stdout, stderr, signal} = result;163 let {error} = result;164 const {code, command, timedOut, isCanceled, killed, parsed: {options: {timeout}}} = options;165
31 unmodified lines
197 // it to `undefined`198 error.signal = signal || undefined;199200 return error;201}201};202203function getCode({error = {}}, code) {203const getCode = ({error = {}}, code) => {204 if (error.code) {205 return [error.code, os.constants.errno[error.code]];206 }207
1 unmodified line
209 return [util.getSystemErrorName(-code), code];210 }211212 return [];213}213};214215function getErrorPrefix({timedOut, timeout, signal, exitCodeName, exitCode, isCanceled}) {215const getErrorPrefix = ({timedOut, timeout, signal, exitCodeName, exitCode, isCanceled}) => {216 if (timedOut) {217 return `timed out after ${timeout} milliseconds`;218 }219
9 unmodified lines
229 return `failed with exit code ${exitCode} (${exitCodeName})`;230 }231232 return 'failed';233}233};234235function joinCommand(file, args = []) {235const joinCommand = (file, args = []) => {236 if (!Array.isArray(args)) {237 return file;238 }239240 return [file, ...args].join(' ');241}241};242243function mergePromiseProperty(spawned, getPromise, property) {243const mergePromiseProperty = (spawned, getPromise, property) => {244 Object.defineProperty(spawned, property, {245 value(...args) {246 return getPromise()[property](...args);247 },248 writable: true,249 enumerable: false,250 configurable: true251 });252}252};253254// The return value is a mixin of `childProcess` and `Promise`255function mergePromise(spawned, getPromise) {255const mergePromise = (spawned, getPromise) => {256 mergePromiseProperty(spawned, getPromise, 'then');257 mergePromiseProperty(spawned, getPromise, 'catch');258259 // TODO: Remove the `if`-guard when targeting Node.js 10
1 unmodified line
261 mergePromiseProperty(spawned, getPromise, 'finally');262 }263264 return spawned;265}265};266267function spawnedKill(kill, signal = 'SIGTERM', options = {}) {267const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {268 const killResult = kill(signal);269 setKillTimeout(kill, signal, options, killResult);270 return killResult;271}271};272273function setKillTimeout(kill, signal, options, killResult) {273const setKillTimeout = (kill, signal, options, killResult) => {274 if (!shouldForceKill(signal, options, killResult)) {275 return;276 }277278 const timeout = getForceKillAfterTimeout(options);279 setTimeout(() => {280 kill('SIGKILL');281 }, timeout).unref();282}282};283284function shouldForceKill(signal, {forceKillAfterTimeout}, killResult) {284const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {285 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;286}286};287288function isSigterm(signal) {288const isSigterm = signal => {289 return signal === os.constants.signals.SIGTERM ||290 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');291}291};292293function getForceKillAfterTimeout({forceKillAfterTimeout = true}) {293const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {294 if (forceKillAfterTimeout === true) {295 return DEFAULT_FORCE_KILL_TIMEOUT;296 }297
1 unmodified line
299 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);300 }301302 return forceKillAfterTimeout;303}303};304305function handleSpawned(spawned, context) {305const handleSpawned = (spawned, context) => {306 return new Promise((resolve, reject) => {307 spawned.on('exit', (code, signal) => {308 if (context.timedOut) {309 reject(Object.assign(new Error('Timed out'), {code, signal}));
12 unmodified lines
322 reject(error);323 });324 }325 });326}326};327328const setupTimeout = (spawned, {timeout, killSignal}, context) => {329 if (timeout > 0) {330 context.timeoutId = setTimeout(() => {
160 unmodified lines
491 };492};493494// Allow spaces to be escaped by a backslash if not meant as a delimiter495function handleEscaping(tokens, token, index) {495const handleEscaping = (tokens, token, index) => {496 if (index === 0) {497 return [token];498 }499
3 unmodified lines
503 return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`];504 }505506 return [...tokens, token];507}507};508509function parseCommand(command) {509const parseCommand = command => {510 return command511 .trim()512 .split(SPACES_REGEXP)513 .reduce(handleEscaping, []);514}514};515516module.exports.command = (command, options) => {517 const [file, ...args] = parseCommand(command);518 return execa(file, args, options);
33 unmodified lines