From 8a0c5f5cae2d6cdc66315172d7ffdffc8954ffaa Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 26 Jul 2025 15:15:56 -0700 Subject: Uses correct subpackage for server in ci and streams stdout and stderr during ci builds --- u/process/exec.ts | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'u/process') diff --git a/u/process/exec.ts b/u/process/exec.ts index a2cdbca..7721f61 100644 --- a/u/process/exec.ts +++ b/u/process/exec.ts @@ -7,19 +7,17 @@ import { Metric, TraceUtil, } from '@emprespresso/pengueno'; -import { promisify } from 'node:util'; -import { exec as execCallback } from 'node:child_process'; -const exec = promisify(execCallback); +import { exec } from 'node:child_process'; export type Command = string[] | string; export type StdStreams = { stdout: string; stderr: string }; export const CmdMetric = Metric.fromName('Exec').asResult(); type Environment = Record; -type Options = { env?: Environment; clearEnv?: boolean }; +type Options = { streamTraceable?: Array<'stdout' | 'stderr'>; env?: Environment; clearEnv?: boolean }; export const getStdout = ( cmd: ITraceable, - options: Options = {}, + options: Options = { streamTraceable: [] }, ): Promise> => cmd .flatMap(TraceUtil.withFunctionTrace(getStdout)) @@ -28,7 +26,36 @@ export const getStdout = ( const cmd = tCmd.get(); const _exec = typeof cmd === 'string' ? cmd : cmd.join(' '); const env = options.clearEnv ? options.env : { ...process.env, ...options.env }; - return Either.fromFailableAsync(exec(_exec, { env })); + return Either.fromFailableAsync( + new Promise((res, rej) => { + const proc = exec(_exec, { env }); + let stdout = ''; + proc.stdout?.on('data', (d) => { + const s = d.toString(); + stdout += s; + if (options.streamTraceable?.includes('stdout')) { + tCmd.trace.trace(s); + } + }); + let stderr = ''; + proc.stderr?.on('data', (d) => { + const s = d.toString(); + stdout += s; + if (options.streamTraceable?.includes('stderr')) { + tCmd.trace.trace(s); + } + }); + + proc.on('exit', (code) => { + const streams = { stdout, stderr }; + if (code === 0) { + res(streams); + } else { + rej(new Error(`exited with non-zero code: ${code}`)); + } + }); + }), + ); }) .map( TraceUtil.promiseify((tEitherStdStreams) => @@ -43,7 +70,7 @@ export const getStdout = ( export const getStdoutMany = ( cmds: ITraceable, LogMetricTraceSupplier>, - options: Options = {}, + options: Options = { streamTraceable: [] }, ): Promise>> => cmds .coExtend((t) => t.get()) -- cgit v1.2.3-70-g09d2