import { Either, type Traceable } from "./mod.ts"; export class ProcessError extends Error {} export const getStdout = async ( { item: cmd, logger: _logger }: Traceable, options: Deno.CommandOptions = {}, ): Promise> => { 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( new ProcessError(`command failed\n${stderrText}`), ); } logger.log("yay! i got code 0 :3", cmd); return Either.right(stdoutText); } catch (e) { logger.error(`o.o wat`, e); if (e instanceof Error) { return Either.left(e); } throw new Error("unknown error " + e); } };