From 42655eaaa30dcc2605c082d1ea997fdd221f3d4f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 25 Jan 2026 16:54:04 +0800 Subject: [PATCH] update libraries/deno-commons-mod.ts --- libraries/deno-commons-mod.ts | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index f3d3d90..0d957d6 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -10,6 +10,57 @@ import {dirname} from "https://deno.land/std@0.208.0/path/mod.ts"; // import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; // import { decodeHex, encodeHex } from "jsr:@std/encoding/hex"; +export class ProcessOutput { + code: number; + stdout: string; + stderr: string; + + constructor(code: number, stdout: string, stderr: string) { + this.code = code; + this.stdout = stdout; + this.stderr = stderr; + } + + assertSuccess(): ProcessOutput { + if (this.code !== 0) { + throw new Error( + `Failed to execute command, exit code: ${this.code}\n- stdout: ${this.stdout}\n- stderr: ${this.stderr}\n`, + ); + } + return this; + } +} + +export async function execCommand( + command: string, + args?: string[], + options?: Deno.CommandOptions, +): Promise { + const opts = options || {}; + if (args) opts.args = args; + const cmd = new Deno.Command(command, opts); + const { code, stdout, stderr } = await cmd.output(); + return new ProcessOutput( + code, + new TextDecoder().decode(stdout), + new TextDecoder().decode(stderr), + ); +} + +export async function execCommandShell( + command: string, + args?: string[], + options?: Deno.CommandOptions, +): Promise { + const opts = options || {}; + if (args) opts.args = args; + opts.stdin = "inherit"; + opts.stdout = "inherit"; + opts.stderr = "inherit"; + const cmd = new Deno.Command(command, opts); + return (await cmd.spawn().status).code; +} + export async function sleep(timeoutMillis: number): Promise { await new Promise((resolve) => setTimeout(resolve, timeoutMillis)); }