summaryrefslogtreecommitdiff
path: root/cli/js/process.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-07-19 19:49:44 +0200
committerGitHub <noreply@github.com>2020-07-19 19:49:44 +0200
commitfa61956f03491101b6ef64423ea2f1f73af26a73 (patch)
treec3800702071ca78aa4dd71bdd0a59a9bbe460bdd /cli/js/process.ts
parent53adde866dd399aa2509d14508642fce37afb8f5 (diff)
Port internal TS code to JS (#6793)
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/js/process.ts')
-rw-r--r--cli/js/process.ts136
1 files changed, 0 insertions, 136 deletions
diff --git a/cli/js/process.ts b/cli/js/process.ts
deleted file mode 100644
index 0844dd8fd..000000000
--- a/cli/js/process.ts
+++ /dev/null
@@ -1,136 +0,0 @@
-// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-
-import { File } from "./files.ts";
-import { close } from "./ops/resources.ts";
-import type { Closer, Reader, Writer } from "./io.ts";
-import { readAll } from "./buffer.ts";
-import { kill, runStatus as runStatusOp, run as runOp } from "./ops/process.ts";
-
-// TODO Maybe extend VSCode's 'CommandOptions'?
-// See https://code.visualstudio.com/docs/editor/tasks-appendix#_schema-for-tasksjson
-export interface RunOptions {
- cmd: string[];
- cwd?: string;
- env?: Record<string, string>;
- stdout?: "inherit" | "piped" | "null" | number;
- stderr?: "inherit" | "piped" | "null" | number;
- stdin?: "inherit" | "piped" | "null" | number;
-}
-
-async function runStatus(rid: number): Promise<ProcessStatus> {
- const res = await runStatusOp(rid);
-
- if (res.gotSignal) {
- const signal = res.exitSignal;
- return { success: false, code: 128 + signal, signal };
- } else if (res.exitCode != 0) {
- return { success: false, code: res.exitCode };
- } else {
- return { success: true, code: 0 };
- }
-}
-
-export class Process<T extends RunOptions = RunOptions> {
- readonly rid: number;
- readonly pid: number;
- readonly stdin!: T["stdin"] extends "piped" ? Writer & Closer
- : (Writer & Closer) | null;
- readonly stdout!: T["stdout"] extends "piped" ? Reader & Closer
- : (Reader & Closer) | null;
- readonly stderr!: T["stderr"] extends "piped" ? Reader & Closer
- : (Reader & Closer) | null;
-
- // @internal
- constructor(res: RunResponse) {
- this.rid = res.rid;
- this.pid = res.pid;
-
- if (res.stdinRid && res.stdinRid > 0) {
- this.stdin = (new File(res.stdinRid) as unknown) as Process<T>["stdin"];
- }
-
- if (res.stdoutRid && res.stdoutRid > 0) {
- this.stdout = (new File(res.stdoutRid) as unknown) as Process<
- T
- >["stdout"];
- }
-
- if (res.stderrRid && res.stderrRid > 0) {
- this.stderr = (new File(res.stderrRid) as unknown) as Process<
- T
- >["stderr"];
- }
- }
-
- status(): Promise<ProcessStatus> {
- return runStatus(this.rid);
- }
-
- async output(): Promise<Uint8Array> {
- if (!this.stdout) {
- throw new TypeError("stdout was not piped");
- }
- try {
- return await readAll(this.stdout as Reader & Closer);
- } finally {
- (this.stdout as Reader & Closer).close();
- }
- }
-
- async stderrOutput(): Promise<Uint8Array> {
- if (!this.stderr) {
- throw new TypeError("stderr was not piped");
- }
- try {
- return await readAll(this.stderr as Reader & Closer);
- } finally {
- (this.stderr as Reader & Closer).close();
- }
- }
-
- close(): void {
- close(this.rid);
- }
-
- kill(signo: number): void {
- kill(this.pid, signo);
- }
-}
-
-export type ProcessStatus =
- | { success: true; code: 0; signal?: undefined }
- | { success: false; code: number; signal?: number };
-
-function isRid(arg: unknown): arg is number {
- return !isNaN(arg as number);
-}
-
-interface RunResponse {
- rid: number;
- pid: number;
- stdinRid: number | null;
- stdoutRid: number | null;
- stderrRid: number | null;
-}
-
-export function run<T extends RunOptions = RunOptions>({
- cmd,
- cwd = undefined,
- env = {},
- stdout = "inherit",
- stderr = "inherit",
- stdin = "inherit",
-}: T): Process<T> {
- const res = runOp({
- cmd: cmd.map(String),
- 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,
- stderrRid: isRid(stderr) ? stderr : 0,
- }) as RunResponse;
- return new Process<T>(res);
-}