-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
73 lines (62 loc) · 2 KB
/
index.ts
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
import { spawn, SpawnOptions } from "node:child_process";
export const ACCEPT_DEFAULT = "ACCEPT_DEFAULT";
export const ENTER_KEY = "\n";
export const WHITESPACE_KEY = " ";
// Octals literals are banned into ESM
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal#octal_escape_sequences
export const DOWN_KEY = "\u001b[B";
export interface CliGhostwriterParams extends SpawnOptions {
command: string;
answersMap: Record<string, string | undefined>;
endingMarker: string;
enableLogs?: boolean;
}
export function cliGhostwriter({
command,
answersMap,
endingMarker,
enableLogs,
...options
}: CliGhostwriterParams) {
enableLogs = enableLogs ?? false;
return new Promise<void>((resolve, reject) => {
const childProcess = spawn(command, {
...options,
stdio: "pipe",
shell: true,
});
childProcess.stdout.setEncoding("utf8");
childProcess.stdout.on("data", (data: string) => {
if (data.includes(endingMarker)) {
childProcess.stdin.end();
}
enableLogs && console.log("prompt:", data);
for (const question in answersMap) {
if (data.includes(question)) {
const answer = answersMap[question];
enableLogs && console.log("answer:", answer);
// To accept the default value we just need to press enter,
// so we aren't going to write the answer
if (!!answer && answer !== ACCEPT_DEFAULT) {
childProcess.stdin.write(answer);
}
childProcess.stdin.write(ENTER_KEY);
// If we don't remove the question once used, we'd match the line showing
// the question after it has been answered too, generating erratic behavior
delete answersMap[question];
break;
}
}
});
childProcess.on("exit", (code) => {
console.log();
if (code) {
console.log(` Execution FAILED...`);
console.log();
reject();
} else {
resolve();
}
});
});
}