1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as dispatch from "./dispatch";
import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/msg_generated";
import { assert, unreachable } from "./util";
import { close, File } from "./files";
import { ReadCloser, WriteCloser } from "./io";
/** How to handle subsubprocess stdio.
*
* "inherit" The default if unspecified. The child inherits from the
* corresponding parent descriptor.
*
* "piped" A new pipe should be arranged to connect the parent and child
* subprocesses.
*
* "null" This stream will be ignored. This is the equivalent of attaching the
* stream to /dev/null.
*/
export type ProcessStdio = "inherit" | "piped" | "null";
// TODO Maybe extend VSCode's 'CommandOptions'?
// tslint:disable-next-line:max-line-length
// See https://code.visualstudio.com/docs/editor/tasks-appendix#_schema-for-tasksjson
export interface RunOptions {
args: string[];
cwd?: string;
stdout?: ProcessStdio;
stderr?: ProcessStdio;
stdin?: ProcessStdio;
}
export class Process {
readonly rid: number;
readonly pid: number;
readonly stdin?: WriteCloser;
readonly stdout?: ReadCloser;
readonly stderr?: ReadCloser;
// @internal
constructor(res: msg.RunRes) {
this.rid = res.rid();
this.pid = res.pid();
if (res.stdinRid() > 0) {
this.stdin = new File(res.stdinRid());
}
if (res.stdoutRid() > 0) {
this.stdout = new File(res.stdoutRid());
}
if (res.stderrRid() > 0) {
this.stderr = new File(res.stderrRid());
}
}
async status(): Promise<ProcessStatus> {
return await runStatus(this.rid);
}
close(): void {
close(this.rid);
}
}
export interface ProcessStatus {
success: boolean;
code?: number;
signal?: number; // TODO: Make this a string, e.g. 'SIGTERM'.
}
function stdioMap(s: ProcessStdio): msg.ProcessStdio {
switch (s) {
case "inherit":
return msg.ProcessStdio.Inherit;
case "piped":
return msg.ProcessStdio.Piped;
case "null":
return msg.ProcessStdio.Null;
default:
return unreachable();
}
}
export function run(opt: RunOptions): Process {
const builder = flatbuffers.createBuilder();
const argsOffset = msg.Run.createArgsVector(
builder,
opt.args.map(a => builder.createString(a))
);
const cwdOffset = opt.cwd == null ? -1 : builder.createString(opt.cwd);
msg.Run.startRun(builder);
msg.Run.addArgs(builder, argsOffset);
if (opt.cwd != null) {
msg.Run.addCwd(builder, cwdOffset);
}
if (opt.stdin) {
msg.Run.addStdin(builder, stdioMap(opt.stdin!));
}
if (opt.stdout) {
msg.Run.addStdout(builder, stdioMap(opt.stdout!));
}
if (opt.stderr) {
msg.Run.addStderr(builder, stdioMap(opt.stderr!));
}
const inner = msg.Run.endRun(builder);
const baseRes = dispatch.sendSync(builder, msg.Any.Run, inner);
assert(baseRes != null);
assert(msg.Any.RunRes === baseRes!.innerType());
const res = new msg.RunRes();
assert(baseRes!.inner(res) != null);
return new Process(res);
}
async function runStatus(rid: number): Promise<ProcessStatus> {
const builder = flatbuffers.createBuilder();
msg.RunStatus.startRunStatus(builder);
msg.RunStatus.addRid(builder, rid);
const inner = msg.RunStatus.endRunStatus(builder);
const baseRes = await dispatch.sendAsync(builder, msg.Any.RunStatus, inner);
assert(baseRes != null);
assert(msg.Any.RunStatusRes === baseRes!.innerType());
const res = new msg.RunStatusRes();
assert(baseRes!.inner(res) != null);
if (res.gotSignal()) {
const signal = res.exitSignal();
return { signal, success: false };
} else {
const code = res.exitCode();
return { code, success: code === 0 };
}
}
|