-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import process from 'node:process'; | ||
import {stripVTControlCharacters} from 'node:util'; | ||
|
||
export const getContext = (previous, rawFile, rawArguments) => { | ||
const start = previous.start ?? process.hrtime.bigint(); | ||
const command = [previous.command, getCommand(rawFile, rawArguments)].filter(Boolean).join(' | '); | ||
return {start, command, state: {stdout: '', stderr: '', output: ''}}; | ||
}; | ||
|
||
const getCommand = (rawFile, rawArguments) => [rawFile, ...rawArguments] | ||
.map(part => getCommandPart(part)) | ||
.join(' '); | ||
|
||
const getCommandPart = part => { | ||
part = stripVTControlCharacters(part); | ||
return /[^\w./-]/.test(part) | ||
? `'${part.replaceAll('\'', '\'\\\'\'')}'` | ||
: part; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import path from 'node:path'; | ||
import {fileURLToPath} from 'node:url'; | ||
import process from 'node:process'; | ||
|
||
export const getOptions = ({ | ||
stdin, | ||
stdout, | ||
stderr, | ||
stdio = [stdin, stdout, stderr], | ||
env: envOption, | ||
preferLocal, | ||
cwd: cwdOption = '.', | ||
...options | ||
}) => { | ||
const cwd = cwdOption instanceof URL ? fileURLToPath(cwdOption) : path.resolve(cwdOption); | ||
const env = envOption === undefined ? undefined : {...process.env, ...envOption}; | ||
const [stdioOption, input] = stdio[0]?.string === undefined | ||
? [stdio] | ||
: [['pipe', ...stdio.slice(1)], stdio[0].string]; | ||
return { | ||
...options, | ||
stdio: stdioOption, | ||
input, | ||
env: preferLocal ? addLocalPath(env ?? process.env, cwd) : env, | ||
cwd, | ||
}; | ||
}; | ||
|
||
const addLocalPath = ({Path = '', PATH = Path, ...env}, cwd) => { | ||
const pathParts = PATH.split(path.delimiter); | ||
const localPaths = getLocalPaths([], path.resolve(cwd)) | ||
.map(localPath => path.join(localPath, 'node_modules/.bin')) | ||
.filter(localPath => !pathParts.includes(localPath)); | ||
return {...env, PATH: [...localPaths, PATH].filter(Boolean).join(path.delimiter)}; | ||
}; | ||
|
||
const getLocalPaths = (localPaths, localPath) => localPaths.at(-1) === localPath | ||
? localPaths | ||
: getLocalPaths([...localPaths, localPath], path.resolve(localPath, '..')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {once, on} from 'node:events'; | ||
import process from 'node:process'; | ||
|
||
export const getResult = async (nodeChildProcess, options, context) => { | ||
const instance = await nodeChildProcess; | ||
useInput(instance, options); | ||
const onClose = once(instance, 'close'); | ||
|
||
try { | ||
await Promise.race([onClose, ...onStreamErrors(instance)]); | ||
checkFailure(context, getErrorOutput(instance)); | ||
return getOutputs(context); | ||
} catch (error) { | ||
await Promise.allSettled([onClose]); | ||
throw getResultError(error, instance, context); | ||
} | ||
}; | ||
|
||
const useInput = (instance, {input}) => { | ||
if (input !== undefined) { | ||
instance.stdin.end(input); | ||
} | ||
}; | ||
|
||
const onStreamErrors = ({stdio}) => stdio.filter(Boolean).map(stream => onStreamError(stream)); | ||
|
||
const onStreamError = async stream => { | ||
for await (const [error] of on(stream, 'error')) { | ||
if (!IGNORED_CODES.has(error?.code)) { | ||
throw error; | ||
} | ||
} | ||
}; | ||
|
||
// Ignore errors that are due to closing errors when the subprocesses exit normally, or due to piping | ||
const IGNORED_CODES = new Set(['ERR_STREAM_PREMATURE_CLOSE', 'EPIPE']); | ||
|
||
export const getResultError = (error, instance, context) => Object.assign( | ||
getErrorInstance(error, context), | ||
getErrorOutput(instance), | ||
getOutputs(context), | ||
); | ||
|
||
const getErrorInstance = (error, {command}) => error?.message.startsWith('Command ') | ||
? error | ||
: new Error(`Command failed: ${command}`, {cause: error}); | ||
|
||
const getErrorOutput = ({exitCode, signalCode}) => ({ | ||
// `exitCode` can be a negative number (`errno`) when the `error` event is emitted on the `instance` | ||
...(exitCode === null || exitCode < 1 ? {} : {exitCode}), | ||
...(signalCode === null ? {} : {signalName: signalCode}), | ||
}); | ||
|
||
const getOutputs = ({state: {stdout, stderr, output}, command, start}) => ({ | ||
stdout: getOutput(stdout), | ||
stderr: getOutput(stderr), | ||
output: getOutput(output), | ||
command, | ||
durationMs: Number(process.hrtime.bigint() - start) / 1e6, | ||
}); | ||
|
||
const getOutput = input => input?.at(-1) === '\n' | ||
? input.slice(0, input.at(-2) === '\r' ? -2 : -1) | ||
: input; | ||
|
||
const checkFailure = ({command}, {exitCode, signalName}) => { | ||
if (signalName !== undefined) { | ||
throw new Error(`Command was terminated with ${signalName}: ${command}`); | ||
} | ||
|
||
if (exitCode !== undefined) { | ||
throw new Error(`Command failed with exit code ${exitCode}: ${command}`); | ||
} | ||
}; |
Oops, something went wrong.