diff options
Diffstat (limited to 'u/process/run.ts')
-rw-r--r-- | u/process/run.ts | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/u/process/run.ts b/u/process/run.ts index e3c4c3d..1d19129 100644 --- a/u/process/run.ts +++ b/u/process/run.ts @@ -1,9 +1,10 @@ import { Either, - type IEither, + IEither, type ITraceable, LogLevel, - type LogTraceSupplier, + LogMetricTraceSupplier, + Metric, TraceUtil, } from '@emprespresso/pengueno'; import { promisify } from 'node:util'; @@ -13,34 +14,27 @@ const exec = promisify(execCallback); export type Command = string[] | string; export type StdStreams = { stdout: string; stderr: string }; +export const CmdMetric = Metric.fromName('Exec').asResult(); export const getStdout = ( - c: ITraceable<Command, LogTraceSupplier>, + c: ITraceable<Command, LogMetricTraceSupplier>, options: { env?: Record<string, string>; clearEnv?: boolean } = {}, ): Promise<IEither<Error, string>> => c - .bimap(TraceUtil.withFunctionTrace(getStdout)) - .bimap((tCmd) => { + .flatMap(TraceUtil.withFunctionTrace(getStdout)) + .flatMap((tCmd) => tCmd.traceScope(() => `Command = ${tCmd.get()}`)) + .map((tCmd) => { const cmd = tCmd.get(); - tCmd.trace.trace(`Command = ${cmd} :> im gonna run this command! `); - const _exec = typeof cmd === 'string' ? cmd : cmd.join(' '); const env = options.clearEnv ? options.env : { ...process.env, ...options.env }; - - const p: Promise<IEither<Error, StdStreams>> = Either.fromFailableAsync(exec(_exec, { env })); - return [p, `Command = ${_exec}`]; + return Either.fromFailableAsync<Error, StdStreams>(exec(_exec, { env })); }) .map( - TraceUtil.promiseify( - (tEitherProcess): IEither<Error, string> => - tEitherProcess.get().fold(({ isLeft, value }) => { - if (isLeft) { - return Either.left(value); - } - if (value.stderr) { - tEitherProcess.trace.addTrace(LogLevel.DEBUG).trace(`StdErr = ${value.stderr}`); - } - return Either.right(value.stdout); - }), + TraceUtil.promiseify((tEitherStdStreams) => + tEitherStdStreams.get().mapRight(({ stderr, stdout }) => { + if (stderr) tEitherStdStreams.trace.traceScope(LogLevel.DEBUG).trace(`StdErr = ${stderr}`); + return stdout; + }), ), ) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(CmdMetric))) .get(); |