2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-08 08:09:22 -04:00
|
|
|
import { File } from "./files.ts";
|
|
|
|
import { close } from "./ops/resources.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { Closer, Reader, Writer } from "./io.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { readAll } from "./buffer.ts";
|
2020-03-09 10:18:02 -04:00
|
|
|
import { kill, runStatus as runStatusOp, run as runOp } from "./ops/process.ts";
|
2018-11-15 23:07:40 -05:00
|
|
|
|
|
|
|
// TODO Maybe extend VSCode's 'CommandOptions'?
|
|
|
|
// See https://code.visualstudio.com/docs/editor/tasks-appendix#_schema-for-tasksjson
|
|
|
|
export interface RunOptions {
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: string[];
|
2018-11-15 23:07:40 -05:00
|
|
|
cwd?: string;
|
2020-07-06 21:45:39 -04:00
|
|
|
env?: Record<string, string>;
|
2020-06-09 07:18:18 -04:00
|
|
|
stdout?: "inherit" | "piped" | "null" | number;
|
|
|
|
stderr?: "inherit" | "piped" | "null" | number;
|
|
|
|
stdin?: "inherit" | "piped" | "null" | number;
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
async function runStatus(rid: number): Promise<ProcessStatus> {
|
2020-03-09 10:18:02 -04:00
|
|
|
const res = await runStatusOp(rid);
|
2019-03-09 12:30:38 -05:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
if (res.gotSignal) {
|
|
|
|
const signal = res.exitSignal;
|
2020-06-10 11:10:08 -04:00
|
|
|
return { success: false, code: 128 + signal, signal };
|
|
|
|
} else if (res.exitCode != 0) {
|
|
|
|
return { success: false, code: res.exitCode };
|
2019-03-09 12:30:38 -05:00
|
|
|
} else {
|
2020-06-10 11:10:08 -04:00
|
|
|
return { success: true, code: 0 };
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 07:18:18 -04:00
|
|
|
export class Process<T extends RunOptions = RunOptions> {
|
2018-11-15 23:07:40 -05:00
|
|
|
readonly rid: number;
|
|
|
|
readonly pid: number;
|
2020-06-27 07:44:02 -04:00
|
|
|
readonly stdin!: T["stdin"] extends "piped"
|
|
|
|
? Writer & Closer
|
|
|
|
: (Writer & Closer) | null;
|
|
|
|
readonly stdout!: T["stdout"] extends "piped"
|
|
|
|
? Reader & Closer
|
2020-07-07 06:21:49 -04:00
|
|
|
: (Reader & Closer) | null;
|
2020-06-27 07:44:02 -04:00
|
|
|
readonly stderr!: T["stderr"] extends "piped"
|
|
|
|
? Reader & Closer
|
2020-07-07 06:21:49 -04:00
|
|
|
: (Reader & Closer) | null;
|
2018-11-15 23:07:40 -05:00
|
|
|
|
|
|
|
// @internal
|
2019-08-26 08:50:21 -04:00
|
|
|
constructor(res: RunResponse) {
|
|
|
|
this.rid = res.rid;
|
|
|
|
this.pid = res.pid;
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
if (res.stdinRid && res.stdinRid > 0) {
|
2020-06-09 07:18:18 -04:00
|
|
|
this.stdin = (new File(res.stdinRid) as unknown) as Process<T>["stdin"];
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
if (res.stdoutRid && res.stdoutRid > 0) {
|
2020-06-09 07:18:18 -04:00
|
|
|
this.stdout = (new File(res.stdoutRid) as unknown) as Process<
|
|
|
|
T
|
|
|
|
>["stdout"];
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
if (res.stderrRid && res.stderrRid > 0) {
|
2020-06-09 07:18:18 -04:00
|
|
|
this.stderr = (new File(res.stderrRid) as unknown) as Process<
|
|
|
|
T
|
|
|
|
>["stderr"];
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
status(): Promise<ProcessStatus> {
|
2020-03-16 05:22:16 -04:00
|
|
|
return runStatus(this.rid);
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:44:05 -05:00
|
|
|
async output(): Promise<Uint8Array> {
|
|
|
|
if (!this.stdout) {
|
2020-06-09 07:18:18 -04:00
|
|
|
throw new TypeError("stdout was not piped");
|
2018-11-30 13:44:05 -05:00
|
|
|
}
|
|
|
|
try {
|
2020-06-09 07:18:18 -04:00
|
|
|
return await readAll(this.stdout as Reader & Closer);
|
2018-11-30 13:44:05 -05:00
|
|
|
} finally {
|
2020-06-09 07:18:18 -04:00
|
|
|
(this.stdout as Reader & Closer).close();
|
2018-11-30 13:44:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:09:46 -04:00
|
|
|
async stderrOutput(): Promise<Uint8Array> {
|
|
|
|
if (!this.stderr) {
|
2020-06-09 07:18:18 -04:00
|
|
|
throw new TypeError("stderr was not piped");
|
2019-03-28 16:09:46 -04:00
|
|
|
}
|
|
|
|
try {
|
2020-06-09 07:18:18 -04:00
|
|
|
return await readAll(this.stderr as Reader & Closer);
|
2019-03-28 16:09:46 -04:00
|
|
|
} finally {
|
2020-06-09 07:18:18 -04:00
|
|
|
(this.stderr as Reader & Closer).close();
|
2019-03-28 16:09:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 23:07:40 -05:00
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
2019-04-21 21:26:56 -04:00
|
|
|
|
|
|
|
kill(signo: number): void {
|
|
|
|
kill(this.pid, signo);
|
|
|
|
}
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:10:08 -04:00
|
|
|
export type ProcessStatus =
|
2020-07-06 21:45:39 -04:00
|
|
|
| { success: true; code: 0; signal?: undefined }
|
|
|
|
| { success: false; code: number; signal?: number };
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-06-21 19:00:14 -04:00
|
|
|
function isRid(arg: unknown): arg is number {
|
|
|
|
return !isNaN(arg as number);
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
interface RunResponse {
|
|
|
|
rid: number;
|
|
|
|
pid: number;
|
|
|
|
stdinRid: number | null;
|
|
|
|
stdoutRid: number | null;
|
|
|
|
stderrRid: number | null;
|
|
|
|
}
|
2020-06-09 07:18:18 -04:00
|
|
|
|
|
|
|
export function run<T extends RunOptions = RunOptions>({
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd,
|
2020-03-10 12:08:58 -04:00
|
|
|
cwd = undefined,
|
|
|
|
env = {},
|
|
|
|
stdout = "inherit",
|
|
|
|
stderr = "inherit",
|
2020-03-28 13:03:49 -04:00
|
|
|
stdin = "inherit",
|
2020-06-09 07:18:18 -04:00
|
|
|
}: T): Process<T> {
|
2020-03-10 12:08:58 -04:00
|
|
|
const res = runOp({
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: cmd.map(String),
|
2020-03-10 12:08:58 -04:00
|
|
|
cwd,
|
|
|
|
env: Object.entries(env),
|
|
|
|
stdin: isRid(stdin) ? "" : stdin,
|
|
|
|
stdout: isRid(stdout) ? "" : stdout,
|
|
|
|
stderr: isRid(stderr) ? "" : stderr,
|
|
|
|
stdinRid: isRid(stdin) ? stdin : 0,
|
|
|
|
stdoutRid: isRid(stdout) ? stdout : 0,
|
2020-03-28 13:03:49 -04:00
|
|
|
stderrRid: isRid(stderr) ? stderr : 0,
|
2020-03-10 12:08:58 -04:00
|
|
|
}) as RunResponse;
|
2020-06-09 07:18:18 -04:00
|
|
|
return new Process<T>(res);
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|