initial commit
This commit is contained in:
120
node_modules/execa/lib/command.js
generated
vendored
Normal file
120
node_modules/execa/lib/command.js
generated
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
import {Buffer} from 'node:buffer';
|
||||
import {ChildProcess} from 'node:child_process';
|
||||
|
||||
const normalizeArgs = (file, args = []) => {
|
||||
if (!Array.isArray(args)) {
|
||||
return [file];
|
||||
}
|
||||
|
||||
return [file, ...args];
|
||||
};
|
||||
|
||||
const NO_ESCAPE_REGEXP = /^[\w.-]+$/;
|
||||
const DOUBLE_QUOTES_REGEXP = /"/g;
|
||||
|
||||
const escapeArg = arg => {
|
||||
if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;
|
||||
};
|
||||
|
||||
export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');
|
||||
|
||||
export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');
|
||||
|
||||
const SPACES_REGEXP = / +/g;
|
||||
|
||||
// Handle `execaCommand()`
|
||||
export const parseCommand = command => {
|
||||
const tokens = [];
|
||||
for (const token of command.trim().split(SPACES_REGEXP)) {
|
||||
// Allow spaces to be escaped by a backslash if not meant as a delimiter
|
||||
const previousToken = tokens[tokens.length - 1];
|
||||
if (previousToken && previousToken.endsWith('\\')) {
|
||||
// Merge previous token with current one
|
||||
tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;
|
||||
} else {
|
||||
tokens.push(token);
|
||||
}
|
||||
}
|
||||
|
||||
return tokens;
|
||||
};
|
||||
|
||||
const parseExpression = expression => {
|
||||
const typeOfExpression = typeof expression;
|
||||
|
||||
if (typeOfExpression === 'string') {
|
||||
return expression;
|
||||
}
|
||||
|
||||
if (typeOfExpression === 'number') {
|
||||
return String(expression);
|
||||
}
|
||||
|
||||
if (
|
||||
typeOfExpression === 'object'
|
||||
&& expression !== null
|
||||
&& !(expression instanceof ChildProcess)
|
||||
&& 'stdout' in expression
|
||||
) {
|
||||
const typeOfStdout = typeof expression.stdout;
|
||||
|
||||
if (typeOfStdout === 'string') {
|
||||
return expression.stdout;
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(expression.stdout)) {
|
||||
return expression.stdout.toString();
|
||||
}
|
||||
|
||||
throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);
|
||||
}
|
||||
|
||||
throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);
|
||||
};
|
||||
|
||||
const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0
|
||||
? [...tokens, ...nextTokens]
|
||||
: [
|
||||
...tokens.slice(0, -1),
|
||||
`${tokens[tokens.length - 1]}${nextTokens[0]}`,
|
||||
...nextTokens.slice(1),
|
||||
];
|
||||
|
||||
const parseTemplate = ({templates, expressions, tokens, index, template}) => {
|
||||
const templateString = template ?? templates.raw[index];
|
||||
const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean);
|
||||
const newTokens = concatTokens(
|
||||
tokens,
|
||||
templateTokens,
|
||||
templateString.startsWith(' '),
|
||||
);
|
||||
|
||||
if (index === expressions.length) {
|
||||
return newTokens;
|
||||
}
|
||||
|
||||
const expression = expressions[index];
|
||||
const expressionTokens = Array.isArray(expression)
|
||||
? expression.map(expression => parseExpression(expression))
|
||||
: [parseExpression(expression)];
|
||||
return concatTokens(
|
||||
newTokens,
|
||||
expressionTokens,
|
||||
templateString.endsWith(' '),
|
||||
);
|
||||
};
|
||||
|
||||
export const parseTemplates = (templates, expressions) => {
|
||||
let tokens = [];
|
||||
|
||||
for (const [index, template] of templates.entries()) {
|
||||
tokens = parseTemplate({templates, expressions, tokens, index, template});
|
||||
}
|
||||
|
||||
return tokens;
|
||||
};
|
||||
|
87
node_modules/execa/lib/error.js
generated
vendored
Normal file
87
node_modules/execa/lib/error.js
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
import process from 'node:process';
|
||||
import {signalsByName} from 'human-signals';
|
||||
|
||||
const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {
|
||||
if (timedOut) {
|
||||
return `timed out after ${timeout} milliseconds`;
|
||||
}
|
||||
|
||||
if (isCanceled) {
|
||||
return 'was canceled';
|
||||
}
|
||||
|
||||
if (errorCode !== undefined) {
|
||||
return `failed with ${errorCode}`;
|
||||
}
|
||||
|
||||
if (signal !== undefined) {
|
||||
return `was killed with ${signal} (${signalDescription})`;
|
||||
}
|
||||
|
||||
if (exitCode !== undefined) {
|
||||
return `failed with exit code ${exitCode}`;
|
||||
}
|
||||
|
||||
return 'failed';
|
||||
};
|
||||
|
||||
export const makeError = ({
|
||||
stdout,
|
||||
stderr,
|
||||
all,
|
||||
error,
|
||||
signal,
|
||||
exitCode,
|
||||
command,
|
||||
escapedCommand,
|
||||
timedOut,
|
||||
isCanceled,
|
||||
killed,
|
||||
parsed: {options: {timeout, cwd = process.cwd()}},
|
||||
}) => {
|
||||
// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.
|
||||
// We normalize them to `undefined`
|
||||
exitCode = exitCode === null ? undefined : exitCode;
|
||||
signal = signal === null ? undefined : signal;
|
||||
const signalDescription = signal === undefined ? undefined : signalsByName[signal].description;
|
||||
|
||||
const errorCode = error && error.code;
|
||||
|
||||
const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});
|
||||
const execaMessage = `Command ${prefix}: ${command}`;
|
||||
const isError = Object.prototype.toString.call(error) === '[object Error]';
|
||||
const shortMessage = isError ? `${execaMessage}\n${error.message}` : execaMessage;
|
||||
const message = [shortMessage, stderr, stdout].filter(Boolean).join('\n');
|
||||
|
||||
if (isError) {
|
||||
error.originalMessage = error.message;
|
||||
error.message = message;
|
||||
} else {
|
||||
error = new Error(message);
|
||||
}
|
||||
|
||||
error.shortMessage = shortMessage;
|
||||
error.command = command;
|
||||
error.escapedCommand = escapedCommand;
|
||||
error.exitCode = exitCode;
|
||||
error.signal = signal;
|
||||
error.signalDescription = signalDescription;
|
||||
error.stdout = stdout;
|
||||
error.stderr = stderr;
|
||||
error.cwd = cwd;
|
||||
|
||||
if (all !== undefined) {
|
||||
error.all = all;
|
||||
}
|
||||
|
||||
if ('bufferedData' in error) {
|
||||
delete error.bufferedData;
|
||||
}
|
||||
|
||||
error.failed = true;
|
||||
error.timedOut = Boolean(timedOut);
|
||||
error.isCanceled = isCanceled;
|
||||
error.killed = killed && !timedOut;
|
||||
|
||||
return error;
|
||||
};
|
102
node_modules/execa/lib/kill.js
generated
vendored
Normal file
102
node_modules/execa/lib/kill.js
generated
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
import os from 'node:os';
|
||||
import onExit from 'signal-exit';
|
||||
|
||||
const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;
|
||||
|
||||
// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior
|
||||
export const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {
|
||||
const killResult = kill(signal);
|
||||
setKillTimeout(kill, signal, options, killResult);
|
||||
return killResult;
|
||||
};
|
||||
|
||||
const setKillTimeout = (kill, signal, options, killResult) => {
|
||||
if (!shouldForceKill(signal, options, killResult)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const timeout = getForceKillAfterTimeout(options);
|
||||
const t = setTimeout(() => {
|
||||
kill('SIGKILL');
|
||||
}, timeout);
|
||||
|
||||
// Guarded because there's no `.unref()` when `execa` is used in the renderer
|
||||
// process in Electron. This cannot be tested since we don't run tests in
|
||||
// Electron.
|
||||
// istanbul ignore else
|
||||
if (t.unref) {
|
||||
t.unref();
|
||||
}
|
||||
};
|
||||
|
||||
const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;
|
||||
|
||||
const isSigterm = signal => signal === os.constants.signals.SIGTERM
|
||||
|| (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');
|
||||
|
||||
const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {
|
||||
if (forceKillAfterTimeout === true) {
|
||||
return DEFAULT_FORCE_KILL_TIMEOUT;
|
||||
}
|
||||
|
||||
if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {
|
||||
throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);
|
||||
}
|
||||
|
||||
return forceKillAfterTimeout;
|
||||
};
|
||||
|
||||
// `childProcess.cancel()`
|
||||
export const spawnedCancel = (spawned, context) => {
|
||||
const killResult = spawned.kill();
|
||||
|
||||
if (killResult) {
|
||||
context.isCanceled = true;
|
||||
}
|
||||
};
|
||||
|
||||
const timeoutKill = (spawned, signal, reject) => {
|
||||
spawned.kill(signal);
|
||||
reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));
|
||||
};
|
||||
|
||||
// `timeout` option handling
|
||||
export const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {
|
||||
if (timeout === 0 || timeout === undefined) {
|
||||
return spawnedPromise;
|
||||
}
|
||||
|
||||
let timeoutId;
|
||||
const timeoutPromise = new Promise((resolve, reject) => {
|
||||
timeoutId = setTimeout(() => {
|
||||
timeoutKill(spawned, killSignal, reject);
|
||||
}, timeout);
|
||||
});
|
||||
|
||||
const safeSpawnedPromise = spawnedPromise.finally(() => {
|
||||
clearTimeout(timeoutId);
|
||||
});
|
||||
|
||||
return Promise.race([timeoutPromise, safeSpawnedPromise]);
|
||||
};
|
||||
|
||||
export const validateTimeout = ({timeout}) => {
|
||||
if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {
|
||||
throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);
|
||||
}
|
||||
};
|
||||
|
||||
// `cleanup` option handling
|
||||
export const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {
|
||||
if (!cleanup || detached) {
|
||||
return timedPromise;
|
||||
}
|
||||
|
||||
const removeExitHandler = onExit(() => {
|
||||
spawned.kill();
|
||||
});
|
||||
|
||||
return timedPromise.finally(() => {
|
||||
removeExitHandler();
|
||||
});
|
||||
};
|
42
node_modules/execa/lib/pipe.js
generated
vendored
Normal file
42
node_modules/execa/lib/pipe.js
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
import {createWriteStream} from 'node:fs';
|
||||
import {ChildProcess} from 'node:child_process';
|
||||
import {isWritableStream} from 'is-stream';
|
||||
|
||||
const isExecaChildProcess = target => target instanceof ChildProcess && typeof target.then === 'function';
|
||||
|
||||
const pipeToTarget = (spawned, streamName, target) => {
|
||||
if (typeof target === 'string') {
|
||||
spawned[streamName].pipe(createWriteStream(target));
|
||||
return spawned;
|
||||
}
|
||||
|
||||
if (isWritableStream(target)) {
|
||||
spawned[streamName].pipe(target);
|
||||
return spawned;
|
||||
}
|
||||
|
||||
if (!isExecaChildProcess(target)) {
|
||||
throw new TypeError('The second argument must be a string, a stream or an Execa child process.');
|
||||
}
|
||||
|
||||
if (!isWritableStream(target.stdin)) {
|
||||
throw new TypeError('The target child process\'s stdin must be available.');
|
||||
}
|
||||
|
||||
spawned[streamName].pipe(target.stdin);
|
||||
return target;
|
||||
};
|
||||
|
||||
export const addPipeMethods = spawned => {
|
||||
if (spawned.stdout !== null) {
|
||||
spawned.pipeStdout = pipeToTarget.bind(undefined, spawned, 'stdout');
|
||||
}
|
||||
|
||||
if (spawned.stderr !== null) {
|
||||
spawned.pipeStderr = pipeToTarget.bind(undefined, spawned, 'stderr');
|
||||
}
|
||||
|
||||
if (spawned.all !== undefined) {
|
||||
spawned.pipeAll = pipeToTarget.bind(undefined, spawned, 'all');
|
||||
}
|
||||
};
|
36
node_modules/execa/lib/promise.js
generated
vendored
Normal file
36
node_modules/execa/lib/promise.js
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
// eslint-disable-next-line unicorn/prefer-top-level-await
|
||||
const nativePromisePrototype = (async () => {})().constructor.prototype;
|
||||
|
||||
const descriptors = ['then', 'catch', 'finally'].map(property => [
|
||||
property,
|
||||
Reflect.getOwnPropertyDescriptor(nativePromisePrototype, property),
|
||||
]);
|
||||
|
||||
// The return value is a mixin of `childProcess` and `Promise`
|
||||
export const mergePromise = (spawned, promise) => {
|
||||
for (const [property, descriptor] of descriptors) {
|
||||
// Starting the main `promise` is deferred to avoid consuming streams
|
||||
const value = typeof promise === 'function'
|
||||
? (...args) => Reflect.apply(descriptor.value, promise(), args)
|
||||
: descriptor.value.bind(promise);
|
||||
|
||||
Reflect.defineProperty(spawned, property, {...descriptor, value});
|
||||
}
|
||||
};
|
||||
|
||||
// Use promises instead of `child_process` events
|
||||
export const getSpawnedPromise = spawned => new Promise((resolve, reject) => {
|
||||
spawned.on('exit', (exitCode, signal) => {
|
||||
resolve({exitCode, signal});
|
||||
});
|
||||
|
||||
spawned.on('error', error => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
if (spawned.stdin) {
|
||||
spawned.stdin.on('error', error => {
|
||||
reject(error);
|
||||
});
|
||||
}
|
||||
});
|
49
node_modules/execa/lib/stdio.js
generated
vendored
Normal file
49
node_modules/execa/lib/stdio.js
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
const aliases = ['stdin', 'stdout', 'stderr'];
|
||||
|
||||
const hasAlias = options => aliases.some(alias => options[alias] !== undefined);
|
||||
|
||||
export const normalizeStdio = options => {
|
||||
if (!options) {
|
||||
return;
|
||||
}
|
||||
|
||||
const {stdio} = options;
|
||||
|
||||
if (stdio === undefined) {
|
||||
return aliases.map(alias => options[alias]);
|
||||
}
|
||||
|
||||
if (hasAlias(options)) {
|
||||
throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`);
|
||||
}
|
||||
|
||||
if (typeof stdio === 'string') {
|
||||
return stdio;
|
||||
}
|
||||
|
||||
if (!Array.isArray(stdio)) {
|
||||
throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);
|
||||
}
|
||||
|
||||
const length = Math.max(stdio.length, aliases.length);
|
||||
return Array.from({length}, (value, index) => stdio[index]);
|
||||
};
|
||||
|
||||
// `ipc` is pushed unless it is already present
|
||||
export const normalizeStdioNode = options => {
|
||||
const stdio = normalizeStdio(options);
|
||||
|
||||
if (stdio === 'ipc') {
|
||||
return 'ipc';
|
||||
}
|
||||
|
||||
if (stdio === undefined || typeof stdio === 'string') {
|
||||
return [stdio, stdio, stdio, 'ipc'];
|
||||
}
|
||||
|
||||
if (stdio.includes('ipc')) {
|
||||
return stdio;
|
||||
}
|
||||
|
||||
return [...stdio, 'ipc'];
|
||||
};
|
119
node_modules/execa/lib/stream.js
generated
vendored
Normal file
119
node_modules/execa/lib/stream.js
generated
vendored
Normal file
@ -0,0 +1,119 @@
|
||||
import {createReadStream, readFileSync} from 'node:fs';
|
||||
import {isStream} from 'is-stream';
|
||||
import getStream from 'get-stream';
|
||||
import mergeStream from 'merge-stream';
|
||||
|
||||
const validateInputOptions = input => {
|
||||
if (input !== undefined) {
|
||||
throw new TypeError('The `input` and `inputFile` options cannot be both set.');
|
||||
}
|
||||
};
|
||||
|
||||
const getInputSync = ({input, inputFile}) => {
|
||||
if (typeof inputFile !== 'string') {
|
||||
return input;
|
||||
}
|
||||
|
||||
validateInputOptions(input);
|
||||
return readFileSync(inputFile);
|
||||
};
|
||||
|
||||
// `input` and `inputFile` option in sync mode
|
||||
export const handleInputSync = options => {
|
||||
const input = getInputSync(options);
|
||||
|
||||
if (isStream(input)) {
|
||||
throw new TypeError('The `input` option cannot be a stream in sync mode');
|
||||
}
|
||||
|
||||
return input;
|
||||
};
|
||||
|
||||
const getInput = ({input, inputFile}) => {
|
||||
if (typeof inputFile !== 'string') {
|
||||
return input;
|
||||
}
|
||||
|
||||
validateInputOptions(input);
|
||||
return createReadStream(inputFile);
|
||||
};
|
||||
|
||||
// `input` and `inputFile` option in async mode
|
||||
export const handleInput = (spawned, options) => {
|
||||
const input = getInput(options);
|
||||
|
||||
if (input === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isStream(input)) {
|
||||
input.pipe(spawned.stdin);
|
||||
} else {
|
||||
spawned.stdin.end(input);
|
||||
}
|
||||
};
|
||||
|
||||
// `all` interleaves `stdout` and `stderr`
|
||||
export const makeAllStream = (spawned, {all}) => {
|
||||
if (!all || (!spawned.stdout && !spawned.stderr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mixed = mergeStream();
|
||||
|
||||
if (spawned.stdout) {
|
||||
mixed.add(spawned.stdout);
|
||||
}
|
||||
|
||||
if (spawned.stderr) {
|
||||
mixed.add(spawned.stderr);
|
||||
}
|
||||
|
||||
return mixed;
|
||||
};
|
||||
|
||||
// On failure, `result.stdout|stderr|all` should contain the currently buffered stream
|
||||
const getBufferedData = async (stream, streamPromise) => {
|
||||
// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve
|
||||
if (!stream || streamPromise === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream.destroy();
|
||||
|
||||
try {
|
||||
return await streamPromise;
|
||||
} catch (error) {
|
||||
return error.bufferedData;
|
||||
}
|
||||
};
|
||||
|
||||
const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {
|
||||
if (!stream || !buffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (encoding) {
|
||||
return getStream(stream, {encoding, maxBuffer});
|
||||
}
|
||||
|
||||
return getStream.buffer(stream, {maxBuffer});
|
||||
};
|
||||
|
||||
// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)
|
||||
export const getSpawnedResult = async ({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) => {
|
||||
const stdoutPromise = getStreamPromise(stdout, {encoding, buffer, maxBuffer});
|
||||
const stderrPromise = getStreamPromise(stderr, {encoding, buffer, maxBuffer});
|
||||
const allPromise = getStreamPromise(all, {encoding, buffer, maxBuffer: maxBuffer * 2});
|
||||
|
||||
try {
|
||||
return await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);
|
||||
} catch (error) {
|
||||
return Promise.all([
|
||||
{error, signal: error.signal, timedOut: error.timedOut},
|
||||
getBufferedData(stdout, stdoutPromise),
|
||||
getBufferedData(stderr, stderrPromise),
|
||||
getBufferedData(all, allPromise),
|
||||
]);
|
||||
}
|
||||
};
|
19
node_modules/execa/lib/verbose.js
generated
vendored
Normal file
19
node_modules/execa/lib/verbose.js
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
import {debuglog} from 'node:util';
|
||||
import process from 'node:process';
|
||||
|
||||
export const verboseDefault = debuglog('execa').enabled;
|
||||
|
||||
const padField = (field, padding) => String(field).padStart(padding, '0');
|
||||
|
||||
const getTimestamp = () => {
|
||||
const date = new Date();
|
||||
return `${padField(date.getHours(), 2)}:${padField(date.getMinutes(), 2)}:${padField(date.getSeconds(), 2)}.${padField(date.getMilliseconds(), 3)}`;
|
||||
};
|
||||
|
||||
export const logCommand = (escapedCommand, {verbose}) => {
|
||||
if (!verbose) {
|
||||
return;
|
||||
}
|
||||
|
||||
process.stderr.write(`[${getTimestamp()}] ${escapedCommand}\n`);
|
||||
};
|
Reference in New Issue
Block a user