import { Either } from "./mod.ts"; export class ProcessError extends Error {} export const getStdout = async ( cmd: string[] | string, options: Deno.CommandOptions = {}, ): Promise> => { 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) { return Either.left( new ProcessError(`command failed\n${stderrText}`), ); } return Either.right(stdoutText); } catch (e) { if (e instanceof Error) { return Either.left(e); } throw new Error("unknown error " + e); } };