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.ts97
1 files changed, 60 insertions, 37 deletions
diff --git a/u/process/run.ts b/u/process/run.ts
index 6dc37d0..670f567 100644
--- a/u/process/run.ts
+++ b/u/process/run.ts
@@ -1,40 +1,63 @@
-import { Either, type Traceable } from "@emprespresso/utils";
+import {
+ Either,
+ type IEither,
+ type ITraceable,
+ LogLevel,
+ TraceUtil,
+} from "@emprespresso/pengueno";
+
+type Command = string[] | string;
+type CommandOutputDecoded = {
+ code: number;
+ stdoutText: string;
+ stderrText: string;
+};
export class ProcessError extends Error {}
-export const getStdout = async (
- { item: cmd, logger: _logger }: Traceable<string[] | string>,
+export const getStdout = (
+ c: ITraceable<Command>,
options: Deno.CommandOptions = {},
-): Promise<Either<ProcessError, string>> => {
- const logger = _logger.addTracer(() => "[getStdout]");
-
- logger.log(`:> im gonna run this command!`, cmd);
- const [exec, ...args] = (typeof cmd === "string") ? cmd.split(" ") : cmd;
- const command = new Deno.Command(exec, {
- args,
- stdout: "piped",
- stderr: "piped",
- ...options,
- });
-
- try {
- const { code, stdout, stderr } = await command.output();
- const stdoutText = new TextDecoder().decode(stdout);
- const stderrText = new TextDecoder().decode(stderr);
-
- if (code !== 0) {
- logger.error(`i weceived an exit code of ${code} i wanna zeroooo :<`);
- return Either.left<ProcessError, string>(
- new ProcessError(`command failed\n${stderrText}`),
- );
- }
-
- logger.log("yay! i got code 0 :3", cmd);
- return Either.right<ProcessError, string>(stdoutText);
- } catch (e) {
- logger.error(`o.o wat`, e);
- if (e instanceof Error) {
- return Either.left<ProcessError, string>(e);
- }
- throw new Error("unknown error " + e);
- }
-};
+): Promise<IEither<ProcessError, string>> =>
+ c.bimap(TraceUtil.withFunctionTrace(getStdout))
+ .map(({ item: cmd, trace }) => {
+ 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(({ item: p }) =>
+ Either.fromFailableAsync<Error, Deno.CommandOutput>(p)
+ )
+ .map(
+ TraceUtil.promiseify(({ item: eitherOutput, trace }) =>
+ eitherOutput.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) => {
+ trace.addTrace(LogLevel.ERROR).trace(`o.o wat ${e}`);
+ return new ProcessError(`${e}`);
+ })
+ .flatMap((decodedOutput): Either<ProcessError, string> => {
+ const { code, stdoutText, stderrText } = decodedOutput;
+ trace.addTrace(LogLevel.DEBUG).trace(
+ `stderr hehehe ${stderrText}`,
+ );
+ if (code !== 0) {
+ const msg =
+ `i weceived an exit code of ${code} i wanna zewoooo :<`;
+ trace.addTrace(LogLevel.ERROR).trace(msg);
+ return Either.left(new ProcessError(msg));
+ }
+ return Either.right(stdoutText);
+ })
+ )
+ ),
+ ).item;