From d51c9d74857aca3c2f172609297266968bc7f809 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Mon, 12 May 2025 09:40:12 -0700 Subject: The big refactor TM --- u/process/run.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 u/process/run.ts (limited to 'u/process/run.ts') diff --git a/u/process/run.ts b/u/process/run.ts new file mode 100644 index 0000000..4954438 --- /dev/null +++ b/u/process/run.ts @@ -0,0 +1,64 @@ +import { + Either, + type IEither, + type ITraceable, + LogLevel, + TraceUtil, +} from "@emprespresso/pengueno"; + +export type Command = string[] | string; +type CommandOutputDecoded = { + code: number; + stdoutText: string; + stderrText: string; +}; + +export const getStdout = ( + c: ITraceable, + options: Deno.CommandOptions = {}, +): Promise> => + c.bimap(TraceUtil.withFunctionTrace(getStdout)) + .map((tCmd) => { + const cmd = tCmd.get(); + tCmd.trace.trace(`:> im gonna run this command! ${cmd}`); + const [exec, ...args] = (typeof cmd === "string") ? cmd.split(" ") : cmd; + return new Deno.Command(exec, { + args, + stdout: "piped", + stderr: "piped", + ...options, + }).output(); + }) + .map((tOut) => + Either.fromFailableAsync(tOut.get()) + ) + .map( + TraceUtil.promiseify((tEitherOut) => + tEitherOut.get().flatMap(({ code, stderr, stdout }) => + Either + .fromFailable(() => { + const stdoutText = new TextDecoder().decode(stdout); + const stderrText = new TextDecoder().decode(stderr); + return { code, stdoutText, stderrText }; + }) + .mapLeft((e) => { + tEitherOut.trace.addTrace(LogLevel.ERROR).trace(`o.o wat ${e}`); + return new Error(`${e}`); + }) + .flatMap((decodedOutput): Either => { + const { code, stdoutText, stderrText } = decodedOutput; + tEitherOut.trace.addTrace(LogLevel.DEBUG).trace( + `stderr hehehe ${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(); -- cgit v1.2.3-70-g09d2