summaryrefslogtreecommitdiff
path: root/u/process/run.ts
diff options
context:
space:
mode:
Diffstat (limited to 'u/process/run.ts')
-rw-r--r--u/process/run.ts102
1 files changed, 40 insertions, 62 deletions
diff --git a/u/process/run.ts b/u/process/run.ts
index abe143c..e3c4c3d 100644
--- a/u/process/run.ts
+++ b/u/process/run.ts
@@ -1,68 +1,46 @@
import {
- Either,
- type IEither,
- type ITraceable,
- LogLevel,
- type LogTraceSupplier,
- TraceUtil,
-} from "@emprespresso/pengueno";
+ Either,
+ type IEither,
+ type ITraceable,
+ LogLevel,
+ type LogTraceSupplier,
+ 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;
-type CommandOutputDecoded = {
- code: number;
- stdoutText: string;
- stderrText: string;
-};
+export type StdStreams = { stdout: string; stderr: string };
export const getStdout = (
- c: ITraceable<Command, LogTraceSupplier>,
- options: Deno.CommandOptions = {},
+ c: ITraceable<Command, LogTraceSupplier>,
+ options: { env?: Record<string, string>; clearEnv?: boolean } = {},
): Promise<IEither<Error, string>> =>
- c
- .bimap(TraceUtil.withFunctionTrace(getStdout))
- .map((tCmd) => {
- const cmd = tCmd.get();
- tCmd.trace.trace(`Command = ${cmd} :> im gonna run this command! `);
- const [exec, ...args] = typeof cmd === "string" ? cmd.split(" ") : cmd;
- return new Deno.Command(exec, {
- args,
- stdout: "piped",
- stderr: "piped",
- ...options,
- });
- })
- .map((tCmd) =>
- Either.fromFailableAsync<Error, Deno.CommandOutput>(() =>
- tCmd.get().output(),
- ),
- )
- .map(
- TraceUtil.promiseify((tEitherOut) =>
- tEitherOut.get().flatMap(({ code, stderr, stdout }) =>
- Either.fromFailable<Error, CommandOutputDecoded>(() => {
- const stdoutText = new TextDecoder().decode(stdout);
- const stderrText = new TextDecoder().decode(stderr);
- return { code, stdoutText, stderrText };
- })
- .mapLeft((e) => {
- tEitherOut.trace.addTrace(LogLevel.ERROR).trace(e);
- return e;
- })
- .flatMap((decodedOutput): IEither<Error, string> => {
- const { code, stdoutText, stderrText } = decodedOutput;
- if (stderrText) {
- tEitherOut.trace
- .addTrace(LogLevel.DEBUG)
- .trace(`stderr: ${stderrText}`);
- }
- if (code !== 0) {
- const msg = `i weceived an exit code of ${code} i wanna zewoooo :<`;
- tEitherOut.trace.addTrace(LogLevel.ERROR).trace(msg);
- return Either.left(new Error(msg));
- }
- return Either.right(stdoutText);
- }),
- ),
- ),
- )
- .get();
+ c
+ .bimap(TraceUtil.withFunctionTrace(getStdout))
+ .bimap((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}`];
+ })
+ .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);
+ }),
+ ),
+ )
+ .get();