|
| 1 | +import { promisify } from 'util' |
| 2 | + |
| 3 | +import type { ChildProcess } from '../plugins/spawn.js' |
| 4 | + |
| 5 | +import { BufferedLogs, logsAreBuffered, Logs } from './logger.js' |
| 6 | +import type { OutputFlusher } from './output_flusher.js' |
| 7 | + |
| 8 | +export type StandardStreams = { |
| 9 | + stderr: NodeJS.WriteStream |
| 10 | + stdout: NodeJS.WriteStream |
| 11 | + outputFlusher?: OutputFlusher |
| 12 | +} |
| 13 | + |
| 14 | +type LogsListener = (logs: string[], outputFlusher: OutputFlusher | undefined, chunk: Buffer) => void |
| 15 | +type LogsListeners = { stderrListener: LogsListener; stdoutListener: LogsListener } |
| 16 | + |
| 17 | +// TODO: replace with `timers/promises` after dropping Node < 15.0.0 |
| 18 | +const pSetTimeout = promisify(setTimeout) |
| 19 | + |
| 20 | +// We try to use `stdio: inherit` because it keeps `stdout/stderr` as `TTY`, |
| 21 | +// which solves many problems. However we can only do it in build.command. |
| 22 | +// Plugins have several events, so need to be switch on and off instead. |
| 23 | +// In buffer mode, `pipe` is necessary. |
| 24 | +export const getBuildCommandStdio = function (logs: Logs) { |
| 25 | + if (logsAreBuffered(logs)) { |
| 26 | + return 'pipe' |
| 27 | + } |
| 28 | + |
| 29 | + return 'inherit' |
| 30 | +} |
| 31 | + |
| 32 | +// Add build command output |
| 33 | +export const handleBuildCommandOutput = function ( |
| 34 | + { stdout: commandStdout, stderr: commandStderr }: { stdout: string; stderr: string }, |
| 35 | + logs: Logs, |
| 36 | +) { |
| 37 | + if (!logsAreBuffered(logs)) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + pushBuildCommandOutput(commandStdout, logs.stdout) |
| 42 | + pushBuildCommandOutput(commandStderr, logs.stderr) |
| 43 | +} |
| 44 | + |
| 45 | +const pushBuildCommandOutput = function (output: string, logsArray: string[]) { |
| 46 | + if (output === '') { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + logsArray.push(output) |
| 51 | +} |
| 52 | + |
| 53 | +// Start plugin step output |
| 54 | +export const pipePluginOutput = function (childProcess: ChildProcess, logs: Logs, standardStreams: StandardStreams) { |
| 55 | + if (!logsAreBuffered(logs)) { |
| 56 | + return streamOutput(childProcess, standardStreams) |
| 57 | + } |
| 58 | + |
| 59 | + return pushOutputToLogs(childProcess, logs, standardStreams.outputFlusher) |
| 60 | +} |
| 61 | + |
| 62 | +// Stop streaming/buffering plugin step output |
| 63 | +export const unpipePluginOutput = async function ( |
| 64 | + childProcess: ChildProcess, |
| 65 | + logs: Logs, |
| 66 | + listeners: LogsListeners, |
| 67 | + standardStreams: StandardStreams, |
| 68 | +) { |
| 69 | + // Let `childProcess` `stdout` and `stderr` flush before stopping redirecting |
| 70 | + await pSetTimeout(0) |
| 71 | + |
| 72 | + if (!logsAreBuffered(logs)) { |
| 73 | + return unstreamOutput(childProcess, standardStreams) |
| 74 | + } |
| 75 | + |
| 76 | + unpushOutputToLogs(childProcess, listeners.stdoutListener, listeners.stderrListener) |
| 77 | +} |
| 78 | + |
| 79 | +// Usually, we stream stdout/stderr because it is more efficient |
| 80 | +const streamOutput = function (childProcess: ChildProcess, standardStreams: StandardStreams) { |
| 81 | + childProcess.stdout?.pipe(standardStreams.stdout) |
| 82 | + childProcess.stderr?.pipe(standardStreams.stderr) |
| 83 | +} |
| 84 | + |
| 85 | +const unstreamOutput = function (childProcess: ChildProcess, standardStreams: StandardStreams) { |
| 86 | + childProcess.stdout?.unpipe(standardStreams.stdout) |
| 87 | + childProcess.stderr?.unpipe(standardStreams.stderr) |
| 88 | +} |
| 89 | + |
| 90 | +// In tests, we push to the `logs` array instead |
| 91 | +const pushOutputToLogs = function ( |
| 92 | + childProcess: ChildProcess, |
| 93 | + logs: BufferedLogs, |
| 94 | + outputFlusher?: OutputFlusher, |
| 95 | +): LogsListeners { |
| 96 | + const stdoutListener = logsListener.bind(null, logs.stdout, outputFlusher) |
| 97 | + const stderrListener = logsListener.bind(null, logs.stderr, outputFlusher) |
| 98 | + |
| 99 | + childProcess.stdout?.on('data', stdoutListener) |
| 100 | + childProcess.stderr?.on('data', stderrListener) |
| 101 | + |
| 102 | + return { stdoutListener, stderrListener } |
| 103 | +} |
| 104 | + |
| 105 | +const logsListener: LogsListener = function (logs, outputFlusher, chunk) { |
| 106 | + if (outputFlusher) { |
| 107 | + outputFlusher.flush() |
| 108 | + } |
| 109 | + |
| 110 | + logs.push(chunk.toString().trimEnd()) |
| 111 | +} |
| 112 | + |
| 113 | +const unpushOutputToLogs = function ( |
| 114 | + childProcess: ChildProcess, |
| 115 | + stdoutListener: LogsListener, |
| 116 | + stderrListener: LogsListener, |
| 117 | +) { |
| 118 | + childProcess.stdout?.removeListener('data', stdoutListener) |
| 119 | + childProcess.stderr?.removeListener('data', stderrListener) |
| 120 | +} |
0 commit comments