-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockcli.ts
More file actions
57 lines (52 loc) · 1.24 KB
/
mockcli.ts
File metadata and controls
57 lines (52 loc) · 1.24 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
import { copy } from "https://deno.land/std@0.143.0/streams/conversion.ts";
const help = `
usage: deno run [--allow-read] mockcli.ts <command>
available commands:
\tanswer
\t\twrites the line "42" to stdout
\techo
\t\twrites stdin to stdout
\tfail
\t\twrites stdin to stderr and returns an error code
\tcwd
\t\twrites the CWD as a line to stdout
\t\t--allow-read permission must be used
`.trimStart();
if (import.meta.main) {
await main(Deno);
}
export async function main(opts: {
args: string[];
stdin: Deno.Reader;
stdout: Deno.Writer;
stderr: Deno.Writer;
cwd: () => string;
exit: (n: number) => void;
}) {
async function writeTextToStdout(text: string) {
const answer = new TextEncoder().encode(text);
await opts.stdout.write(answer);
}
switch (Deno.args[0]) {
case "answer": {
await writeTextToStdout("42\n");
return opts.exit(0);
}
case "echo": {
await copy(Deno.stdin, Deno.stdout);
return opts.exit(0);
}
case "fail": {
await copy(Deno.stdin, Deno.stderr);
return opts.exit(-1);
}
case "cwd": {
await writeTextToStdout(opts.cwd() + "\n");
return opts.exit(0);
}
default: {
await writeTextToStdout(help);
return opts.exit(0);
}
}
}