From 08995dda6017846ffe694d174bd32f12dd9971fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 6 Sep 2021 00:16:44 +0200 Subject: [PATCH] Add error message from subcommands --- src/normalize.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/normalize.ts b/src/normalize.ts index f513f50..5380f8f 100755 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -49,10 +49,23 @@ export class ProjectNormalizer { ); } + async run(...cmd: string[]): Promise { + const p = this.sys.run({ cmd, stdout: "piped", stderr: "piped" }); + const [status, stdout, stderr] = await Promise.all([ + p.status(), + p.output(), + p.stderrOutput(), + ]); + if (status.success) { + return new TextDecoder().decode(stdout); + } else { + console.error(new TextDecoder().decode(stderr)); + throw new Error(`Command failed: ${cmd.join(" ")}`); + } + } + async formatPath(path: string) { - await this.sys.run({ - cmd: ["./run", "fmt", "-q", path], - }).status(); + await this.run("./run", "fmt", "-q", path); } async updateRunScript() { @@ -84,11 +97,7 @@ export class ProjectNormalizer { } async updateDenoDefs() { - const process = this.sys.run({ - cmd: ["./run", "types"], - stdout: "piped", - }); - const defs = new TextDecoder("utf-8").decode(await process.output()); + const defs = await this.run("./run", "types"); await this.sys.writeTextFile("deno.d.ts", defs); }