import * as dntShim from "../../_dnt.shims.js"; import { readableStreamFromReader, writableStreamFromWriter } from "../std/streams.js" export class Command implements dntShim.Deno.Command { #command #options constructor(command: string, options?: dntShim.Deno.CommandOptions) { this.#command = command this.#options = options } async output(): Promise { const { cwd, args = [], stdin = "inherit", stdout = "piped", stderr = "piped", env, signal, } = this.#options ?? {} const process = dntShim.Deno.run({ cmd: [this.#command, ...args], cwd: cwd as string | undefined, stdin, stdout, stderr, env, }) signal?.addEventListener("abort", () => { try { process.kill("SIGKILL") } catch {} }) return new ChildProcess(process).output() } outputSync(): dntShim.Deno.CommandOutput { throw new Error("not implemented") } spawn(): ChildProcess { const { cwd, args = [], stdin = "inherit", stdout = "inherit", stderr = "inherit", env, signal, } = this.#options ?? {} const process = dntShim.Deno.run({ cmd: [this.#command, ...args], cwd: cwd as string | undefined, stdin, stdout, stderr, env, }) signal?.addEventListener("abort", () => { try { process.kill("SIGKILL") } catch {} }) return new ChildProcess(process) } } class ChildProcess implements dntShim.Deno.ChildProcess { get pid() { return this.#process.pid } #status?: Promise get status() { return this.#status ??= this.#process.status().then((status) => { return { code: status.code, success: status.success, signal: null, } }) } #stdin?: WritableStream get stdin() { return this.#stdin ??= writableStreamFromWriter(this.#process.stdin!) } #stdout?: ReadableStream get stdout() { return this.#stdout ??= readableStreamFromReader(this.#process.stdout!) } #stderr?: ReadableStream get stderr() { return this.#stderr ??= readableStreamFromReader(this.#process.stderr!) } #process constructor(process: dntShim.Deno.Process) { this.#process = process } ref() { throw new Error("not implemented") } unref() { throw new Error("not implemented") } kill(signal: dntShim.Deno.Signal) { this.#process.kill(signal) } async output(): Promise { const [ status, stdoutData, stderrData, ] = await Promise.all([ this.#process.status(), this.#process.stdout ? this.#process.output() : null, this.#process.stderr ? this.#process.stderrOutput() : null, ]) return { code: status.code, success: status.success, signal: null, stdout: stdoutData!, stderr: stderrData!, } } }