Skip to content

Commit

Permalink
refactor: refactor run method
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Apr 25, 2021
1 parent 3b0c02f commit 56acafd
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions codeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,8 @@ const codeview = new Command<void>()
});
}

type RunOptions = {
type RunOptions = Deno.RunOptions & {
process?: (process: Deno.Process) => Promise<void>;
cmd: Array<string>;
};

async function run(opts: RunOptions) {
Expand All @@ -574,13 +573,16 @@ const codeview = new Command<void>()
stdout: !opts.process && options.logLevel === "debug"
? "inherit"
: "piped",
stderr: options.logLevel === "debug" ? "inherit" : "piped",
cmd: opts.cmd,
stderr: options.logLevel ? "inherit" : "piped",
...opts,
});

processes.add(process);

const [status] = await Promise.all([
const [stdOutput, status] = await Promise.all([
(!opts.process && options.logLevel === "debug"
? Promise.resolve()
: process.output()) as Promise<Uint8Array | void>,
process.status(),
opts.process?.(process),
]);
Expand All @@ -591,17 +593,15 @@ const codeview = new Command<void>()
// don't throw an error on signal!
return;
}
let output = stdOutput ? new TextDecoder().decode(stdOutput) : "";

let output: string = new TextDecoder().decode(await process.output());
const errorMsg: string = new TextDecoder().decode(
await process.stderrOutput(),
);

if (errorMsg.trim()) {
output += "\n\n" + errorMsg;
if (!options.logLevel) {
output += "\n\n" + new TextDecoder().decode(
await process.stderrOutput(),
);
}

throw new Error(output || "Unknown error");
throw new Error(output || "Command failed.");
}
debug(green("Done: %s"), opts.cmd.join(" "));
}
Expand All @@ -623,7 +623,7 @@ const codeview = new Command<void>()
}

function debug(...args: Array<unknown>) {
if (options.logLevel === "debug") {
if (options.logLevel) {
log(...args);
}
}
Expand Down

0 comments on commit 56acafd

Please sign in to comment.