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