update deno commons mod
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64";
|
||||
import {dirname, fromFileUrl} from "https://deno.land/std/path/mod.ts";
|
||||
import {spawn} from "node:child_process";
|
||||
import {spawn, SpawnOptionsWithoutStdio} from "node:child_process";
|
||||
|
||||
// reference: https://docs.deno.com/examples/hex_base64_encoding/
|
||||
// import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
|
||||
@@ -73,7 +73,7 @@ export class ProcessOutput {
|
||||
export async function execCommandAndStdout(
|
||||
command: string,
|
||||
args?: string[],
|
||||
options?: Deno.CommandOptions,
|
||||
options?: Deno.CommandOptions | SpawnOptionsWithoutStdio,
|
||||
): Promise<string> {
|
||||
const processOutput = await execCommand(command, args, options);
|
||||
processOutput.assertSuccess();
|
||||
@@ -83,8 +83,9 @@ export async function execCommandAndStdout(
|
||||
export async function execCommand(
|
||||
command: string,
|
||||
args?: string[],
|
||||
options?: Deno.CommandOptions,
|
||||
options?: Deno.CommandOptions | SpawnOptionsWithoutStdio,
|
||||
): Promise<ProcessOutput> {
|
||||
if (isDeno()) {
|
||||
const opts = options || {};
|
||||
if (args) opts.args = args;
|
||||
const cmd = new Deno.Command(command, opts);
|
||||
@@ -95,11 +96,43 @@ export async function execCommand(
|
||||
new TextDecoder().decode(stderr),
|
||||
);
|
||||
}
|
||||
return await execCommandSpawn(command, args, options);
|
||||
}
|
||||
|
||||
async function execCommandSpawn(
|
||||
command: string,
|
||||
args: string[],
|
||||
options?: SpawnOptionsWithoutStdio,
|
||||
): Promise<ProcessOutput> {
|
||||
const ps = spawn(command, args, options);
|
||||
return new Promise((resolve, reject) => {
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
ps.stdout.on("data", (data) => {
|
||||
stdout += data.toString();
|
||||
});
|
||||
ps.stderr.on("data", (data) => {
|
||||
stderr += data.toString();
|
||||
});
|
||||
ps.on("close", (code) => {
|
||||
try {
|
||||
const output = new ProcessOutput(code, stdout, stderr);
|
||||
ps.stdin.end();
|
||||
resolve(output);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
ps.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function execCommandShell(
|
||||
command: string,
|
||||
args?: string[],
|
||||
options?: Deno.CommandOptions,
|
||||
options?: Deno.CommandOptions | SpawnOptionsWithoutStdio,
|
||||
): Promise<number> {
|
||||
if (isDeno()) {
|
||||
const opts = options || {};
|
||||
@@ -109,7 +142,15 @@ export async function execCommandShell(
|
||||
opts.stderr = "inherit";
|
||||
const cmd = new Deno.Command(command, opts);
|
||||
return (await cmd.spawn().status).code;
|
||||
} else {
|
||||
}
|
||||
return await execCommandShellSpwan(command, args, options);
|
||||
}
|
||||
|
||||
async function execCommandShellSpwan(
|
||||
command: string,
|
||||
args?: string[],
|
||||
options?: SpawnOptionsWithoutStdio,
|
||||
): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const ps = spawn(command, args, {
|
||||
shell: false,
|
||||
@@ -123,7 +164,6 @@ export async function execCommandShell(
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function sleep(timeoutMillis: number): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, timeoutMillis));
|
||||
|
||||
Reference in New Issue
Block a user