1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import {
Either,
IEither,
type ITraceable,
LogLevel,
LogMetricTraceSupplier,
Metric,
TraceUtil,
} from '@emprespresso/pengueno';
import { promisify } from 'node:util';
import { exec as execCallback } from 'node:child_process';
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, LogMetricTraceSupplier>,
options: { env?: Record<string, string>; clearEnv?: boolean } = {},
): Promise<IEither<Error, string>> =>
c
.flatMap(TraceUtil.withFunctionTrace(getStdout))
.flatMap((tCmd) => tCmd.traceScope(() => `Command = ${tCmd.get()}`))
.map((tCmd) => {
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<Error, StdStreams>(exec(_exec, { env }));
})
.map(
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();
|