-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.ts
More file actions
133 lines (109 loc) · 2.71 KB
/
exec.ts
File metadata and controls
133 lines (109 loc) · 2.71 KB
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
export class ExecError extends Error {
code = 0;
stderr = "";
stdout = "";
text = "";
signal: number | undefined = undefined;
cmd: string[] | undefined = undefined;
constructor(message: string) {
super(message);
this.name = this.constructor.name;
if (typeof Error["captureStackTrace"] === "function") {
Error["captureStackTrace"](this, this.constructor);
} else {
this.stack = new Error(message).stack;
}
}
}
async function waitForExit(cmd: string | string[], process: Deno.Process) {
if (!Array.isArray(cmd)) {
cmd = cmd.split(" ");
}
const [status, stdout, stderr] = await Promise.all([
process.status(),
process.output(),
process.stderrOutput(),
]);
process.close();
if (!status.success) {
let msg = `process '${cmd.join(" ")}' exited with code ${status.code}`;
const stdoutText = new TextDecoder().decode(stdout);
const stderrText = new TextDecoder().decode(stderr);
const text = (stdoutText || stderrText).trim();
if (text) {
msg = `${msg}: ${text}`;
}
const err = new ExecError(msg);
err.code = status.code;
err.signal = status.signal;
err.text = text;
err.cmd = cmd;
err.stdout = stdoutText;
err.stderr = stderrText;
throw err;
}
return { status, stdout, stderr };
}
export async function exec(
cmd: string | string[],
stdin = "",
opts?: Partial<Deno.RunOptions>
) {
if (!Array.isArray(cmd)) {
cmd = cmd.split(" ").filter((x) => !!x);
}
opts = {
stderr: "piped",
stdout: "piped",
cmd,
...(opts || {}),
};
if (stdin) {
opts.stdin = "piped";
}
const p = Deno.run(opts as Deno.RunOptions);
if (stdin) {
const encoder = new TextEncoder();
await p.stdin!.write(encoder.encode(stdin));
p.stdin!.close();
}
const { stdout } = await waitForExit(cmd, p);
return new TextDecoder().decode(stdout).trim();
}
export type TailOptions = {
fromStderr?: boolean;
stdin?: string;
};
export async function* tail(cmd: string | string[], _opts?: TailOptions) {
if (!Array.isArray(cmd)) {
cmd = cmd.split(" ");
}
const opts: Deno.RunOptions = {
stderr: "piped",
stdout: "piped",
cmd,
};
if (_opts?.stdin) {
opts.stdin = "piped";
}
const p = Deno.run(opts);
let s = p.stdout!;
if (_opts?.fromStderr) {
s = p.stderr!;
}
if (_opts?.stdin) {
const encoder = new TextEncoder();
await p.stdin!.write(encoder.encode(_opts?.stdin));
p.stdin!.close();
}
for (;;) {
const buf = new Uint8Array(4096);
const n = await s.read(buf);
if (n === null || n === undefined) {
break;
}
const text = new TextDecoder().decode(buf.slice(0, n));
yield text;
}
await waitForExit(cmd, p);
}