update libraries/deno-commons-mod.ts

This commit is contained in:
2026-01-25 16:54:04 +08:00
parent 83c4ea0d0d
commit 42655eaaa3

View File

@@ -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 { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
// import { decodeHex, encodeHex } from "jsr:@std/encoding/hex"; // 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<ProcessOutput> {
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<number> {
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<void> { export async function sleep(timeoutMillis: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, timeoutMillis)); await new Promise((resolve) => setTimeout(resolve, timeoutMillis));
} }