@scelar/nodepod 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/bench/integration.bench.d.ts +1 -0
- package/dist/__tests__/bench/memory-volume.bench.d.ts +1 -0
- package/dist/__tests__/bench/polyfills.bench.d.ts +1 -0
- package/dist/__tests__/bench/script-engine.bench.d.ts +1 -0
- package/dist/__tests__/bench/shell.bench.d.ts +1 -0
- package/dist/__tests__/bench/syntax-transforms.bench.d.ts +1 -0
- package/dist/__tests__/bench/version-resolver.bench.d.ts +1 -0
- package/dist/__tests__/buffer.test.d.ts +1 -0
- package/dist/__tests__/byte-encoding.test.d.ts +1 -0
- package/dist/__tests__/digest.test.d.ts +1 -0
- package/dist/__tests__/events.test.d.ts +1 -0
- package/dist/__tests__/memory-volume.test.d.ts +1 -0
- package/dist/__tests__/path.test.d.ts +1 -0
- package/dist/__tests__/process.test.d.ts +1 -0
- package/dist/__tests__/script-engine.test.d.ts +1 -0
- package/dist/__tests__/shell-builtins.test.d.ts +1 -0
- package/dist/__tests__/shell-interpreter.test.d.ts +1 -0
- package/dist/__tests__/shell-parser.test.d.ts +1 -0
- package/dist/__tests__/smoke.test.d.ts +1 -0
- package/dist/__tests__/stream.test.d.ts +1 -0
- package/dist/__tests__/syntax-transforms.test.d.ts +1 -0
- package/dist/__tests__/version-resolver.test.d.ts +1 -0
- package/dist/{child_process-Dopvyd-E.js → child_process-D6oDN2MX.js} +4 -4
- package/dist/{child_process-Dopvyd-E.js.map → child_process-D6oDN2MX.js.map} +1 -1
- package/dist/{child_process-B38qoN6R.cjs → child_process-hmVqFcF7.cjs} +5 -5
- package/dist/{child_process-B38qoN6R.cjs.map → child_process-hmVqFcF7.cjs.map} +1 -1
- package/dist/constants/cdn-urls.d.ts +12 -0
- package/dist/constants/config.d.ts +121 -0
- package/dist/cross-origin.d.ts +5 -0
- package/dist/engine-factory.d.ts +24 -0
- package/dist/engine-types.d.ts +40 -0
- package/dist/helpers/byte-encoding.d.ts +4 -0
- package/dist/helpers/digest.d.ts +1 -0
- package/dist/helpers/event-loop.d.ts +12 -0
- package/dist/helpers/wasm-cache.d.ts +6 -0
- package/dist/iframe-sandbox.d.ts +25 -0
- package/dist/{index--Qr8LVpQ.js → index-Ale2oba_.js} +240 -136
- package/dist/index-Ale2oba_.js.map +1 -0
- package/dist/{index-cnitc68U.cjs → index-BO1i013L.cjs} +236 -191
- package/dist/index-BO1i013L.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +86 -0
- package/dist/index.mjs +1 -1
- package/dist/isolation-helpers.d.ts +13 -0
- package/dist/memory-volume.d.ts +147 -0
- package/dist/module-transformer.d.ts +6 -0
- package/dist/packages/archive-extractor.d.ts +28 -0
- package/dist/packages/browser-bundler.d.ts +14 -0
- package/dist/packages/installer.d.ts +41 -0
- package/dist/packages/registry-client.d.ts +46 -0
- package/dist/packages/version-resolver.d.ts +37 -0
- package/dist/polyfills/assert.d.ts +54 -0
- package/dist/polyfills/async_hooks.d.ts +46 -0
- package/dist/polyfills/buffer.d.ts +111 -0
- package/dist/polyfills/child_process.d.ts +141 -0
- package/dist/polyfills/chokidar.d.ts +63 -0
- package/dist/polyfills/cluster.d.ts +55 -0
- package/dist/polyfills/console.d.ts +72 -0
- package/dist/polyfills/constants.d.ts +207 -0
- package/dist/polyfills/crypto.d.ts +189 -0
- package/dist/polyfills/dgram.d.ts +37 -0
- package/dist/polyfills/diagnostics_channel.d.ts +27 -0
- package/dist/polyfills/dns.d.ts +76 -0
- package/dist/polyfills/domain.d.ts +27 -0
- package/dist/polyfills/esbuild.d.ts +106 -0
- package/dist/polyfills/events.d.ts +39 -0
- package/dist/polyfills/fs.d.ts +274 -0
- package/dist/polyfills/fsevents.d.ts +62 -0
- package/dist/polyfills/http.d.ts +215 -0
- package/dist/polyfills/http2.d.ts +119 -0
- package/dist/polyfills/https.d.ts +24 -0
- package/dist/polyfills/inspector.d.ts +29 -0
- package/dist/polyfills/lightningcss.d.ts +70 -0
- package/dist/polyfills/module.d.ts +35 -0
- package/dist/polyfills/net.d.ts +91 -0
- package/dist/polyfills/os.d.ts +240 -0
- package/dist/polyfills/path.d.ts +95 -0
- package/dist/polyfills/perf_hooks.d.ts +48 -0
- package/dist/polyfills/process.d.ts +190 -0
- package/dist/polyfills/punycode.d.ts +21 -0
- package/dist/polyfills/querystring.d.ts +18 -0
- package/dist/polyfills/quic.d.ts +42 -0
- package/dist/polyfills/readdirp.d.ts +48 -0
- package/dist/polyfills/readline.d.ts +87 -0
- package/dist/polyfills/repl.d.ts +45 -0
- package/dist/polyfills/rollup.d.ts +37 -0
- package/dist/polyfills/sea.d.ts +11 -0
- package/dist/polyfills/sqlite.d.ts +30 -0
- package/dist/polyfills/stream.d.ts +160 -0
- package/dist/polyfills/string_decoder.d.ts +16 -0
- package/dist/polyfills/tailwindcss-oxide.d.ts +42 -0
- package/dist/polyfills/test.d.ts +60 -0
- package/dist/polyfills/timers.d.ts +23 -0
- package/dist/polyfills/tls.d.ts +61 -0
- package/dist/polyfills/trace_events.d.ts +22 -0
- package/dist/polyfills/tty.d.ts +38 -0
- package/dist/polyfills/url.d.ts +55 -0
- package/dist/polyfills/util.d.ts +135 -0
- package/dist/polyfills/v8.d.ts +97 -0
- package/dist/polyfills/vm.d.ts +66 -0
- package/dist/polyfills/volume-registry.d.ts +3 -0
- package/dist/polyfills/wasi.d.ts +16 -0
- package/dist/polyfills/worker_threads.d.ts +111 -0
- package/dist/polyfills/ws.d.ts +92 -0
- package/dist/polyfills/zlib.d.ts +278 -0
- package/dist/request-proxy.d.ts +63 -0
- package/dist/script-engine.d.ts +81 -0
- package/dist/sdk/nodepod-fs.d.ts +27 -0
- package/dist/sdk/nodepod-process.d.ts +28 -0
- package/dist/sdk/nodepod-terminal.d.ts +48 -0
- package/dist/sdk/nodepod.d.ts +39 -0
- package/dist/sdk/types.d.ts +53 -0
- package/dist/shell/commands/bun.d.ts +4 -0
- package/dist/shell/commands/directory.d.ts +2 -0
- package/dist/shell/commands/file-ops.d.ts +2 -0
- package/dist/shell/commands/git.d.ts +2 -0
- package/dist/shell/commands/node.d.ts +4 -0
- package/dist/shell/commands/npm.d.ts +3 -0
- package/dist/shell/commands/pm-types.d.ts +25 -0
- package/dist/shell/commands/pnpm.d.ts +3 -0
- package/dist/shell/commands/search.d.ts +2 -0
- package/dist/shell/commands/shell-env.d.ts +3 -0
- package/dist/shell/commands/text-processing.d.ts +2 -0
- package/dist/shell/commands/yarn.d.ts +3 -0
- package/dist/shell/shell-builtins.d.ts +2 -0
- package/dist/shell/shell-helpers.d.ts +28 -0
- package/dist/shell/shell-interpreter.d.ts +48 -0
- package/dist/shell/shell-parser.d.ts +6 -0
- package/dist/shell/shell-types.d.ts +48 -0
- package/dist/syntax-transforms.d.ts +4 -0
- package/dist/threading/engine-worker.d.ts +1 -0
- package/dist/threading/inline-worker.d.ts +2 -0
- package/dist/threading/offload-types.d.ts +87 -0
- package/dist/threading/offload-worker.d.ts +1 -0
- package/dist/threading/offload.d.ts +16 -0
- package/dist/threading/process-context.d.ts +40 -0
- package/dist/threading/process-handle.d.ts +42 -0
- package/dist/threading/process-manager.d.ts +52 -0
- package/dist/threading/process-worker-entry.d.ts +24 -0
- package/dist/threading/shared-vfs.d.ts +36 -0
- package/dist/threading/sync-channel.d.ts +21 -0
- package/dist/threading/task-queue.d.ts +15 -0
- package/dist/threading/vfs-bridge.d.ts +25 -0
- package/dist/threading/worker-pool.d.ts +38 -0
- package/dist/threading/worker-protocol.d.ts +264 -0
- package/dist/threading/worker-vfs.d.ts +34 -0
- package/dist/types/fs-streams.d.ts +122 -0
- package/dist/types/manifest.d.ts +17 -0
- package/dist/worker-sandbox.d.ts +19 -0
- package/package.json +97 -95
- package/src/__tests__/bench/integration.bench.ts +117 -0
- package/src/__tests__/bench/memory-volume.bench.ts +115 -0
- package/src/__tests__/bench/polyfills.bench.ts +147 -0
- package/src/__tests__/bench/script-engine.bench.ts +104 -0
- package/src/__tests__/bench/shell.bench.ts +101 -0
- package/src/__tests__/bench/syntax-transforms.bench.ts +82 -0
- package/src/__tests__/bench/version-resolver.bench.ts +95 -0
- package/src/__tests__/buffer.test.ts +273 -0
- package/src/__tests__/byte-encoding.test.ts +98 -0
- package/src/__tests__/digest.test.ts +44 -0
- package/src/__tests__/events.test.ts +245 -0
- package/src/__tests__/memory-volume.test.ts +443 -0
- package/src/__tests__/path.test.ts +181 -0
- package/src/__tests__/process.test.ts +129 -0
- package/src/__tests__/script-engine.test.ts +229 -0
- package/src/__tests__/shell-builtins.test.ts +357 -0
- package/src/__tests__/shell-interpreter.test.ts +157 -0
- package/src/__tests__/shell-parser.test.ts +204 -0
- package/src/__tests__/stream.test.ts +142 -0
- package/src/__tests__/syntax-transforms.test.ts +158 -0
- package/src/__tests__/version-resolver.test.ts +184 -0
- package/src/constants/cdn-urls.ts +18 -18
- package/src/helpers/byte-encoding.ts +51 -39
- package/src/memory-volume.ts +962 -941
- package/src/module-transformer.ts +368 -368
- package/src/packages/installer.ts +396 -396
- package/src/polyfills/buffer.ts +633 -628
- package/src/polyfills/esbuild.ts +854 -854
- package/src/polyfills/events.ts +282 -276
- package/src/polyfills/process.ts +695 -690
- package/src/polyfills/readline.ts +692 -692
- package/src/polyfills/tty.ts +71 -71
- package/src/script-engine.ts +3396 -3375
- package/src/syntax-transforms.ts +543 -561
- package/dist/index--Qr8LVpQ.js.map +0 -1
- package/dist/index-cnitc68U.cjs.map +0 -1
|
@@ -1,692 +1,692 @@
|
|
|
1
|
-
// readline polyfill -- createInterface, emitKeypressEvents, terminal helpers.
|
|
2
|
-
// tracks line/cursor on every keystroke because @clack reads rl.line directly.
|
|
3
|
-
|
|
4
|
-
import { EventEmitter } from "./events";
|
|
5
|
-
import { ref as _elRef, unref as _elUnref } from "../helpers/event-loop";
|
|
6
|
-
|
|
7
|
-
// child_process wait loop checks this to avoid exiting while waiting for user input
|
|
8
|
-
let _activeInterfaceCount = 0;
|
|
9
|
-
|
|
10
|
-
export function getActiveInterfaceCount(): number {
|
|
11
|
-
return _activeInterfaceCount;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function setActiveInterfaceCount(n: number): void {
|
|
15
|
-
_activeInterfaceCount = n;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function resetActiveInterfaceCount(): void {
|
|
19
|
-
_activeInterfaceCount = 0;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
export interface InterfaceConfig {
|
|
24
|
-
input?: unknown;
|
|
25
|
-
output?: unknown;
|
|
26
|
-
terminal?: boolean;
|
|
27
|
-
prompt?: string;
|
|
28
|
-
historySize?: number;
|
|
29
|
-
completer?: (line: string) => [string[], string] | void;
|
|
30
|
-
crlfDelay?: number;
|
|
31
|
-
escapeCodeTimeout?: number;
|
|
32
|
-
tabSize?: number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// emitKeypressEvents: parse data events into keypress events
|
|
36
|
-
|
|
37
|
-
const KEYPRESS_DECODER = Symbol("keypressDecoder");
|
|
38
|
-
|
|
39
|
-
// mirrors Node.js internal emitKeys generator
|
|
40
|
-
function parseAndEmitKeypress(
|
|
41
|
-
stream: any,
|
|
42
|
-
char: string,
|
|
43
|
-
escapeBuf: string[],
|
|
44
|
-
): string[] {
|
|
45
|
-
if (escapeBuf.length > 0) {
|
|
46
|
-
escapeBuf.push(char);
|
|
47
|
-
const seq = escapeBuf.join("");
|
|
48
|
-
|
|
49
|
-
if (seq.length >= 3 && seq[1] === "[") {
|
|
50
|
-
// CSI sequence
|
|
51
|
-
const lastChar = seq[seq.length - 1];
|
|
52
|
-
if (/[A-Za-z~]/.test(lastChar)) {
|
|
53
|
-
let name = "";
|
|
54
|
-
if (lastChar === "A") name = "up";
|
|
55
|
-
else if (lastChar === "B") name = "down";
|
|
56
|
-
else if (lastChar === "C") name = "right";
|
|
57
|
-
else if (lastChar === "D") name = "left";
|
|
58
|
-
else if (lastChar === "H") name = "home";
|
|
59
|
-
else if (lastChar === "F") name = "end";
|
|
60
|
-
else if (seq === "\x1b[3~") name = "delete";
|
|
61
|
-
else if (seq === "\x1b[2~") name = "insert";
|
|
62
|
-
else if (seq === "\x1b[5~") name = "pageup";
|
|
63
|
-
else if (seq === "\x1b[6~") name = "pagedown";
|
|
64
|
-
else name = lastChar;
|
|
65
|
-
|
|
66
|
-
stream.emit("keypress", seq, {
|
|
67
|
-
sequence: seq,
|
|
68
|
-
name,
|
|
69
|
-
ctrl: false,
|
|
70
|
-
meta: false,
|
|
71
|
-
shift: false,
|
|
72
|
-
});
|
|
73
|
-
return [];
|
|
74
|
-
}
|
|
75
|
-
if (seq.length > 8) {
|
|
76
|
-
stream.emit("keypress", seq, {
|
|
77
|
-
sequence: seq,
|
|
78
|
-
name: "unknown",
|
|
79
|
-
ctrl: false,
|
|
80
|
-
meta: false,
|
|
81
|
-
shift: false,
|
|
82
|
-
});
|
|
83
|
-
return [];
|
|
84
|
-
}
|
|
85
|
-
return escapeBuf;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (seq.length >= 3 && seq[1] === "O") {
|
|
89
|
-
const lastChar = seq[seq.length - 1];
|
|
90
|
-
let name = "";
|
|
91
|
-
if (lastChar === "A") name = "up";
|
|
92
|
-
else if (lastChar === "B") name = "down";
|
|
93
|
-
else if (lastChar === "C") name = "right";
|
|
94
|
-
else if (lastChar === "D") name = "left";
|
|
95
|
-
else if (lastChar === "H") name = "home";
|
|
96
|
-
else if (lastChar === "F") name = "end";
|
|
97
|
-
else name = lastChar;
|
|
98
|
-
|
|
99
|
-
stream.emit("keypress", seq, {
|
|
100
|
-
sequence: seq,
|
|
101
|
-
name,
|
|
102
|
-
ctrl: false,
|
|
103
|
-
meta: false,
|
|
104
|
-
shift: false,
|
|
105
|
-
});
|
|
106
|
-
return [];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (seq.length >= 2 && seq[1] !== "[" && seq[1] !== "O") {
|
|
110
|
-
stream.emit("keypress", seq, {
|
|
111
|
-
sequence: seq,
|
|
112
|
-
name: "escape",
|
|
113
|
-
ctrl: false,
|
|
114
|
-
meta: true,
|
|
115
|
-
shift: false,
|
|
116
|
-
});
|
|
117
|
-
return [];
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return escapeBuf;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (char === "\x1b") {
|
|
124
|
-
return [char];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
let name = char;
|
|
128
|
-
let ctrl = false;
|
|
129
|
-
if (char === "\r" || char === "\n") name = "return";
|
|
130
|
-
else if (char === "\x7f" || char === "\b") name = "backspace";
|
|
131
|
-
else if (char === "\t") name = "tab";
|
|
132
|
-
else if (char === " ") name = "space";
|
|
133
|
-
else if (char.charCodeAt(0) < 32) {
|
|
134
|
-
// Ctrl+letter
|
|
135
|
-
ctrl = true;
|
|
136
|
-
name = String.fromCharCode(char.charCodeAt(0) + 96);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
stream.emit("keypress", char, {
|
|
140
|
-
sequence: char,
|
|
141
|
-
name,
|
|
142
|
-
ctrl,
|
|
143
|
-
meta: false,
|
|
144
|
-
shift: false,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
return [];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function emitKeypressEvents(stream: unknown, _iface?: Interface): void {
|
|
151
|
-
if (!stream || typeof (stream as any).on !== "function") return;
|
|
152
|
-
if ((stream as any)[KEYPRESS_DECODER]) return;
|
|
153
|
-
(stream as any)[KEYPRESS_DECODER] = true;
|
|
154
|
-
|
|
155
|
-
let escapeBuf: string[] = [];
|
|
156
|
-
let escapeTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
157
|
-
|
|
158
|
-
(stream as any).on("data", (data: unknown) => {
|
|
159
|
-
if ((stream as any).listenerCount("keypress") <= 0) return;
|
|
160
|
-
const text = typeof data === "string" ? data : String(data);
|
|
161
|
-
|
|
162
|
-
for (const char of text) {
|
|
163
|
-
if (escapeTimeout) {
|
|
164
|
-
clearTimeout(escapeTimeout);
|
|
165
|
-
escapeTimeout = null;
|
|
166
|
-
}
|
|
167
|
-
escapeBuf = parseAndEmitKeypress(stream as any, char, escapeBuf);
|
|
168
|
-
|
|
169
|
-
// flush pending escape after timeout
|
|
170
|
-
if (escapeBuf.length > 0) {
|
|
171
|
-
escapeTimeout = setTimeout(() => {
|
|
172
|
-
if (escapeBuf.length > 0) {
|
|
173
|
-
const seq = escapeBuf.join("");
|
|
174
|
-
(stream as any).emit("keypress", seq, {
|
|
175
|
-
sequence: seq,
|
|
176
|
-
name: "escape",
|
|
177
|
-
ctrl: false,
|
|
178
|
-
meta: false,
|
|
179
|
-
shift: false,
|
|
180
|
-
});
|
|
181
|
-
escapeBuf = [];
|
|
182
|
-
}
|
|
183
|
-
}, 50);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
export interface Interface extends EventEmitter {
|
|
191
|
-
_promptStr: string;
|
|
192
|
-
_input: unknown;
|
|
193
|
-
_output: unknown;
|
|
194
|
-
_closed: boolean;
|
|
195
|
-
_lineBuffer: string;
|
|
196
|
-
_pendingQuestions: Array<{
|
|
197
|
-
query: string;
|
|
198
|
-
handler: (answer: string) => void;
|
|
199
|
-
}>;
|
|
200
|
-
terminal: boolean;
|
|
201
|
-
line: string;
|
|
202
|
-
cursor: number;
|
|
203
|
-
_refreshLine(): void;
|
|
204
|
-
_onKeypress(char: string | undefined, key: any): void;
|
|
205
|
-
_onData(text: string): void;
|
|
206
|
-
prompt(preserveCursor?: boolean): void;
|
|
207
|
-
setPrompt(text: string): void;
|
|
208
|
-
getPrompt(): string;
|
|
209
|
-
question(query: string, optsOrHandler?: unknown, handler?: (answer: string) => void): void;
|
|
210
|
-
pause(): this;
|
|
211
|
-
resume(): this;
|
|
212
|
-
close(): void;
|
|
213
|
-
write(data: string | null, _key?: { ctrl?: boolean; name?: string; meta?: boolean; shift?: boolean; sequence?: string }): void;
|
|
214
|
-
getCursorPos(): { rows: number; cols: number };
|
|
215
|
-
[Symbol.asyncIterator](): AsyncGenerator<string, void, undefined>;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
interface InterfaceConstructor {
|
|
219
|
-
new (cfg?: InterfaceConfig): Interface;
|
|
220
|
-
(this: any, cfg?: InterfaceConfig): void;
|
|
221
|
-
prototype: any;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export const Interface = function Interface(this: any, cfg?: InterfaceConfig) {
|
|
225
|
-
if (!this) return;
|
|
226
|
-
EventEmitter.call(this);
|
|
227
|
-
this._promptStr = cfg?.prompt ?? "> ";
|
|
228
|
-
this._input = cfg?.input;
|
|
229
|
-
this._output = cfg?.output;
|
|
230
|
-
this._closed = false;
|
|
231
|
-
this._lineBuffer = "";
|
|
232
|
-
this._pendingQuestions = [];
|
|
233
|
-
this.terminal = cfg?.terminal ?? false;
|
|
234
|
-
this.line = "";
|
|
235
|
-
this.cursor = 0;
|
|
236
|
-
|
|
237
|
-
if (this._input && typeof (this._input as any).on === "function") {
|
|
238
|
-
_activeInterfaceCount++;
|
|
239
|
-
_elRef();
|
|
240
|
-
const inputStream = this._input as EventEmitter;
|
|
241
|
-
const self = this;
|
|
242
|
-
|
|
243
|
-
if (this.terminal) {
|
|
244
|
-
emitKeypressEvents(this._input, this);
|
|
245
|
-
|
|
246
|
-
inputStream.on("keypress", (char: string | undefined, key: any) => {
|
|
247
|
-
if (self._closed) return;
|
|
248
|
-
self._onKeypress(char, key);
|
|
249
|
-
});
|
|
250
|
-
} else {
|
|
251
|
-
inputStream.on("data", (data: unknown) => {
|
|
252
|
-
if (self._closed) return;
|
|
253
|
-
const text = typeof data === "string" ? data : String(data);
|
|
254
|
-
self._onData(text);
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
inputStream.on("end", () => {
|
|
259
|
-
if (!self._closed) self.close();
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
} as unknown as InterfaceConstructor;
|
|
263
|
-
|
|
264
|
-
Object.setPrototypeOf(Interface.prototype, EventEmitter.prototype);
|
|
265
|
-
|
|
266
|
-
Interface.prototype._refreshLine = function _refreshLine(this: any): void {
|
|
267
|
-
if (this._output && typeof (this._output as any).write === "function") {
|
|
268
|
-
(this._output as any).write(this.line);
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
Interface.prototype._onKeypress = function _onKeypress(this: any, char: string | undefined, key: any): void {
|
|
273
|
-
if (!key) return;
|
|
274
|
-
|
|
275
|
-
if (key.ctrl && key.name === "c") {
|
|
276
|
-
this.close();
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
if (key.name === "return") {
|
|
281
|
-
const line = this._lineBuffer;
|
|
282
|
-
this._lineBuffer = "";
|
|
283
|
-
this.line = line;
|
|
284
|
-
|
|
285
|
-
if (this._pendingQuestions.length > 0) {
|
|
286
|
-
const q = this._pendingQuestions.shift()!;
|
|
287
|
-
q.handler(line);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
this.emit("line", line);
|
|
291
|
-
this.line = "";
|
|
292
|
-
this.cursor = 0;
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (key.name === "backspace") {
|
|
297
|
-
if (this.cursor > 0) {
|
|
298
|
-
this._lineBuffer =
|
|
299
|
-
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
300
|
-
this._lineBuffer.slice(this.cursor);
|
|
301
|
-
this.cursor--;
|
|
302
|
-
this.line = this._lineBuffer;
|
|
303
|
-
this._refreshLine();
|
|
304
|
-
}
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
if (key.name === "delete") {
|
|
309
|
-
if (this.cursor < this._lineBuffer.length) {
|
|
310
|
-
this._lineBuffer =
|
|
311
|
-
this._lineBuffer.slice(0, this.cursor) +
|
|
312
|
-
this._lineBuffer.slice(this.cursor + 1);
|
|
313
|
-
this.line = this._lineBuffer;
|
|
314
|
-
this._refreshLine();
|
|
315
|
-
}
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
if (key.name === "left") {
|
|
320
|
-
this.cursor = Math.max(0, this.cursor - 1);
|
|
321
|
-
this._refreshLine();
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
if (key.name === "right") {
|
|
325
|
-
this.cursor = Math.min(this._lineBuffer.length, this.cursor + 1);
|
|
326
|
-
this._refreshLine();
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
if (key.name === "home") {
|
|
330
|
-
this.cursor = 0;
|
|
331
|
-
this._refreshLine();
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
if (key.name === "end") {
|
|
335
|
-
this.cursor = this._lineBuffer.length;
|
|
336
|
-
this._refreshLine();
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
if (key.ctrl && key.name === "u") {
|
|
341
|
-
this._lineBuffer = this._lineBuffer.slice(this.cursor);
|
|
342
|
-
this.cursor = 0;
|
|
343
|
-
this.line = this._lineBuffer;
|
|
344
|
-
this._refreshLine();
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
if (key.ctrl && key.name === "k") {
|
|
349
|
-
this._lineBuffer = this._lineBuffer.slice(0, this.cursor);
|
|
350
|
-
this.line = this._lineBuffer;
|
|
351
|
-
this._refreshLine();
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (key.ctrl && key.name === "h") {
|
|
356
|
-
if (this.cursor > 0) {
|
|
357
|
-
this._lineBuffer =
|
|
358
|
-
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
359
|
-
this._lineBuffer.slice(this.cursor);
|
|
360
|
-
this.cursor--;
|
|
361
|
-
this.line = this._lineBuffer;
|
|
362
|
-
this._refreshLine();
|
|
363
|
-
}
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
if (
|
|
368
|
-
char &&
|
|
369
|
-
!key.ctrl &&
|
|
370
|
-
!key.meta &&
|
|
371
|
-
char.length === 1 &&
|
|
372
|
-
char.charCodeAt(0) >= 32
|
|
373
|
-
) {
|
|
374
|
-
this._lineBuffer =
|
|
375
|
-
this._lineBuffer.slice(0, this.cursor) +
|
|
376
|
-
char +
|
|
377
|
-
this._lineBuffer.slice(this.cursor);
|
|
378
|
-
this.cursor++;
|
|
379
|
-
this.line = this._lineBuffer;
|
|
380
|
-
this._refreshLine();
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
Interface.prototype._onData = function _onData(this: any, text: string): void {
|
|
387
|
-
for (const char of text) {
|
|
388
|
-
if (char === "\n" || char === "\r") {
|
|
389
|
-
const line = this._lineBuffer;
|
|
390
|
-
this._lineBuffer = "";
|
|
391
|
-
this.line = line;
|
|
392
|
-
|
|
393
|
-
if (this._pendingQuestions.length > 0) {
|
|
394
|
-
const q = this._pendingQuestions.shift()!;
|
|
395
|
-
q.handler(line);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
this.emit("line", line);
|
|
399
|
-
this.line = "";
|
|
400
|
-
this.cursor = 0;
|
|
401
|
-
} else if (char === "\x7f" || char === "\b") {
|
|
402
|
-
if (this._lineBuffer.length > 0) {
|
|
403
|
-
this._lineBuffer =
|
|
404
|
-
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
405
|
-
this._lineBuffer.slice(this.cursor);
|
|
406
|
-
this.cursor = Math.max(0, this.cursor - 1);
|
|
407
|
-
this.line = this._lineBuffer;
|
|
408
|
-
this._refreshLine();
|
|
409
|
-
}
|
|
410
|
-
} else if (char.charCodeAt(0) >= 32) {
|
|
411
|
-
this._lineBuffer =
|
|
412
|
-
this._lineBuffer.slice(0, this.cursor) +
|
|
413
|
-
char +
|
|
414
|
-
this._lineBuffer.slice(this.cursor);
|
|
415
|
-
this.cursor++;
|
|
416
|
-
this.line = this._lineBuffer;
|
|
417
|
-
this._refreshLine();
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
};
|
|
421
|
-
|
|
422
|
-
Interface.prototype.prompt = function prompt(this: any, preserveCursor?: boolean): void {
|
|
423
|
-
if (this._output && typeof (this._output as any).write === "function") {
|
|
424
|
-
(this._output as any).write(this._promptStr);
|
|
425
|
-
}
|
|
426
|
-
if (!preserveCursor) {
|
|
427
|
-
this.cursor = 0;
|
|
428
|
-
this.line = "";
|
|
429
|
-
this._lineBuffer = "";
|
|
430
|
-
}
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
Interface.prototype.setPrompt = function setPrompt(this: any, text: string): void {
|
|
434
|
-
this._promptStr = text;
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
Interface.prototype.getPrompt = function getPrompt(this: any): string {
|
|
438
|
-
return this._promptStr;
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
Interface.prototype.question = function question(
|
|
442
|
-
this: any,
|
|
443
|
-
query: string,
|
|
444
|
-
optsOrHandler?: unknown,
|
|
445
|
-
handler?: (answer: string) => void,
|
|
446
|
-
): void {
|
|
447
|
-
const actualHandler =
|
|
448
|
-
typeof optsOrHandler === "function"
|
|
449
|
-
? (optsOrHandler as (answer: string) => void)
|
|
450
|
-
: handler;
|
|
451
|
-
if (!actualHandler) return;
|
|
452
|
-
|
|
453
|
-
if (this._output && typeof (this._output as any).write === "function") {
|
|
454
|
-
(this._output as any).write(query);
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
if (this._input && typeof (this._input as any).on === "function") {
|
|
458
|
-
this._pendingQuestions.push({ query, handler: actualHandler });
|
|
459
|
-
} else {
|
|
460
|
-
setTimeout(() => actualHandler(""), 0);
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
Interface.prototype.pause = function pause(this: any): any {
|
|
465
|
-
if (this._input && typeof (this._input as any).pause === "function") {
|
|
466
|
-
(this._input as any).pause();
|
|
467
|
-
}
|
|
468
|
-
this.emit("pause");
|
|
469
|
-
return this;
|
|
470
|
-
};
|
|
471
|
-
|
|
472
|
-
Interface.prototype.resume = function resume(this: any): any {
|
|
473
|
-
if (this._input && typeof (this._input as any).resume === "function") {
|
|
474
|
-
(this._input as any).resume();
|
|
475
|
-
}
|
|
476
|
-
this.emit("resume");
|
|
477
|
-
return this;
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
Interface.prototype.close = function close(this: any): void {
|
|
481
|
-
if (this._closed) return;
|
|
482
|
-
this._closed = true;
|
|
483
|
-
if (this._input && typeof (this._input as any).on === "function") {
|
|
484
|
-
_activeInterfaceCount = Math.max(0, _activeInterfaceCount - 1);
|
|
485
|
-
_elUnref();
|
|
486
|
-
}
|
|
487
|
-
for (const q of this._pendingQuestions) {
|
|
488
|
-
q.handler("");
|
|
489
|
-
}
|
|
490
|
-
this._pendingQuestions.length = 0;
|
|
491
|
-
this.emit("close");
|
|
492
|
-
};
|
|
493
|
-
|
|
494
|
-
Interface.prototype.write = function write(
|
|
495
|
-
this: any,
|
|
496
|
-
data: string | null,
|
|
497
|
-
_key?: {
|
|
498
|
-
ctrl?: boolean;
|
|
499
|
-
name?: string;
|
|
500
|
-
meta?: boolean;
|
|
501
|
-
shift?: boolean;
|
|
502
|
-
sequence?: string;
|
|
503
|
-
},
|
|
504
|
-
): void {
|
|
505
|
-
if (this._closed) return;
|
|
506
|
-
|
|
507
|
-
if (data === null || data === undefined) {
|
|
508
|
-
if (_key) {
|
|
509
|
-
// emit synthetic keypress on input so external listeners (e.g. @clack) see it
|
|
510
|
-
const emitOnInput =
|
|
511
|
-
this._input && typeof (this._input as any).emit === "function";
|
|
512
|
-
|
|
513
|
-
if (_key.ctrl && _key.name === "c") {
|
|
514
|
-
this.close();
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
if (_key.ctrl && _key.name === "h") {
|
|
518
|
-
// Backspace
|
|
519
|
-
if (this.cursor > 0) {
|
|
520
|
-
this._lineBuffer =
|
|
521
|
-
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
522
|
-
this._lineBuffer.slice(this.cursor);
|
|
523
|
-
this.cursor--;
|
|
524
|
-
this.line = this._lineBuffer;
|
|
525
|
-
}
|
|
526
|
-
if (emitOnInput) {
|
|
527
|
-
(this._input as any).emit("keypress", "\x7f", {
|
|
528
|
-
sequence: "\x7f",
|
|
529
|
-
name: "backspace",
|
|
530
|
-
ctrl: false,
|
|
531
|
-
meta: false,
|
|
532
|
-
shift: false,
|
|
533
|
-
});
|
|
534
|
-
}
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
if (_key.ctrl && _key.name === "u") {
|
|
538
|
-
this._lineBuffer = this._lineBuffer.slice(this.cursor);
|
|
539
|
-
this.cursor = 0;
|
|
540
|
-
this.line = this._lineBuffer;
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
if (_key.ctrl && _key.name === "k") {
|
|
544
|
-
this._lineBuffer = this._lineBuffer.slice(0, this.cursor);
|
|
545
|
-
this.line = this._lineBuffer;
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
if (_key.name === "left") {
|
|
549
|
-
this.cursor = Math.max(0, this.cursor - 1);
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
if (_key.name === "right") {
|
|
553
|
-
this.cursor = Math.min(this._lineBuffer.length, this.cursor + 1);
|
|
554
|
-
return;
|
|
555
|
-
}
|
|
556
|
-
if (_key.name === "home") {
|
|
557
|
-
this.cursor = 0;
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
if (_key.name === "end") {
|
|
561
|
-
this.cursor = this._lineBuffer.length;
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
if (_key?.ctrl && _key?.name === "c") {
|
|
569
|
-
this.close();
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
this._onData(data);
|
|
574
|
-
};
|
|
575
|
-
|
|
576
|
-
Interface.prototype.getCursorPos = function getCursorPos(this: any): { rows: number; cols: number } {
|
|
577
|
-
return { rows: 0, cols: this.cursor };
|
|
578
|
-
};
|
|
579
|
-
|
|
580
|
-
Interface.prototype[Symbol.asyncIterator] = async function*(this: any): AsyncGenerator<string, void, undefined> {
|
|
581
|
-
const self = this;
|
|
582
|
-
while (!self._closed) {
|
|
583
|
-
const line = await new Promise<string | null>((resolve) => {
|
|
584
|
-
if (self._closed) {
|
|
585
|
-
resolve(null);
|
|
586
|
-
return;
|
|
587
|
-
}
|
|
588
|
-
self.once("line", (l: string) => resolve(l));
|
|
589
|
-
self.once("close", () => resolve(null));
|
|
590
|
-
});
|
|
591
|
-
if (line === null) break;
|
|
592
|
-
yield line;
|
|
593
|
-
}
|
|
594
|
-
};
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
export function createInterface(
|
|
598
|
-
cfgOrInput?: InterfaceConfig | unknown,
|
|
599
|
-
output?: unknown,
|
|
600
|
-
): Interface {
|
|
601
|
-
if (
|
|
602
|
-
cfgOrInput &&
|
|
603
|
-
typeof cfgOrInput === "object" &&
|
|
604
|
-
!("on" in (cfgOrInput as any)) &&
|
|
605
|
-
!("read" in (cfgOrInput as any))
|
|
606
|
-
) {
|
|
607
|
-
return new Interface(cfgOrInput as InterfaceConfig);
|
|
608
|
-
}
|
|
609
|
-
return new Interface({ input: cfgOrInput, output });
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
export function clearLine(
|
|
614
|
-
stream: unknown,
|
|
615
|
-
dir: number,
|
|
616
|
-
done?: () => void,
|
|
617
|
-
): boolean {
|
|
618
|
-
if (stream && typeof (stream as any).clearLine === "function") {
|
|
619
|
-
return (stream as any).clearLine(dir, done);
|
|
620
|
-
}
|
|
621
|
-
if (done) done();
|
|
622
|
-
return true;
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
export function clearScreenDown(stream: unknown, done?: () => void): boolean {
|
|
626
|
-
if (stream && typeof (stream as any).write === "function") {
|
|
627
|
-
(stream as any).write("\x1b[J");
|
|
628
|
-
}
|
|
629
|
-
if (done) done();
|
|
630
|
-
return true;
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
export function cursorTo(
|
|
634
|
-
stream: unknown,
|
|
635
|
-
x: number,
|
|
636
|
-
yOrDone?: number | (() => void),
|
|
637
|
-
done?: () => void,
|
|
638
|
-
): boolean {
|
|
639
|
-
const cb = typeof yOrDone === "function" ? yOrDone : done;
|
|
640
|
-
if (stream && typeof (stream as any).cursorTo === "function") {
|
|
641
|
-
return (stream as any).cursorTo(
|
|
642
|
-
x,
|
|
643
|
-
typeof yOrDone === "number" ? yOrDone : undefined,
|
|
644
|
-
cb,
|
|
645
|
-
);
|
|
646
|
-
}
|
|
647
|
-
if (cb) cb();
|
|
648
|
-
return true;
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
export function moveCursor(
|
|
652
|
-
stream: unknown,
|
|
653
|
-
dx: number,
|
|
654
|
-
dy: number,
|
|
655
|
-
done?: () => void,
|
|
656
|
-
): boolean {
|
|
657
|
-
if (stream && typeof (stream as any).moveCursor === "function") {
|
|
658
|
-
return (stream as any).moveCursor(dx, dy, done);
|
|
659
|
-
}
|
|
660
|
-
if (done) done();
|
|
661
|
-
return true;
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
export const promises = {
|
|
666
|
-
createInterface(cfg?: InterfaceConfig) {
|
|
667
|
-
const rl = createInterface(cfg);
|
|
668
|
-
return {
|
|
669
|
-
question(query: string): Promise<string> {
|
|
670
|
-
return new Promise((resolve) => rl.question(query, resolve));
|
|
671
|
-
},
|
|
672
|
-
close(): void {
|
|
673
|
-
rl.close();
|
|
674
|
-
},
|
|
675
|
-
async *[Symbol.asyncIterator](): AsyncGenerator<string, void, undefined> {
|
|
676
|
-
yield* rl;
|
|
677
|
-
},
|
|
678
|
-
};
|
|
679
|
-
},
|
|
680
|
-
};
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
export default {
|
|
684
|
-
Interface,
|
|
685
|
-
createInterface,
|
|
686
|
-
clearLine,
|
|
687
|
-
clearScreenDown,
|
|
688
|
-
cursorTo,
|
|
689
|
-
moveCursor,
|
|
690
|
-
emitKeypressEvents,
|
|
691
|
-
promises,
|
|
692
|
-
};
|
|
1
|
+
// readline polyfill -- createInterface, emitKeypressEvents, terminal helpers.
|
|
2
|
+
// tracks line/cursor on every keystroke because @clack reads rl.line directly.
|
|
3
|
+
|
|
4
|
+
import { EventEmitter } from "./events";
|
|
5
|
+
import { ref as _elRef, unref as _elUnref } from "../helpers/event-loop";
|
|
6
|
+
|
|
7
|
+
// child_process wait loop checks this to avoid exiting while waiting for user input
|
|
8
|
+
let _activeInterfaceCount = 0;
|
|
9
|
+
|
|
10
|
+
export function getActiveInterfaceCount(): number {
|
|
11
|
+
return _activeInterfaceCount;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function setActiveInterfaceCount(n: number): void {
|
|
15
|
+
_activeInterfaceCount = n;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function resetActiveInterfaceCount(): void {
|
|
19
|
+
_activeInterfaceCount = 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export interface InterfaceConfig {
|
|
24
|
+
input?: unknown;
|
|
25
|
+
output?: unknown;
|
|
26
|
+
terminal?: boolean;
|
|
27
|
+
prompt?: string;
|
|
28
|
+
historySize?: number;
|
|
29
|
+
completer?: (line: string) => [string[], string] | void;
|
|
30
|
+
crlfDelay?: number;
|
|
31
|
+
escapeCodeTimeout?: number;
|
|
32
|
+
tabSize?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// emitKeypressEvents: parse data events into keypress events
|
|
36
|
+
|
|
37
|
+
const KEYPRESS_DECODER = Symbol("keypressDecoder");
|
|
38
|
+
|
|
39
|
+
// mirrors Node.js internal emitKeys generator
|
|
40
|
+
function parseAndEmitKeypress(
|
|
41
|
+
stream: any,
|
|
42
|
+
char: string,
|
|
43
|
+
escapeBuf: string[],
|
|
44
|
+
): string[] {
|
|
45
|
+
if (escapeBuf.length > 0) {
|
|
46
|
+
escapeBuf.push(char);
|
|
47
|
+
const seq = escapeBuf.join("");
|
|
48
|
+
|
|
49
|
+
if (seq.length >= 3 && seq[1] === "[") {
|
|
50
|
+
// CSI sequence
|
|
51
|
+
const lastChar = seq[seq.length - 1];
|
|
52
|
+
if (/[A-Za-z~]/.test(lastChar)) {
|
|
53
|
+
let name = "";
|
|
54
|
+
if (lastChar === "A") name = "up";
|
|
55
|
+
else if (lastChar === "B") name = "down";
|
|
56
|
+
else if (lastChar === "C") name = "right";
|
|
57
|
+
else if (lastChar === "D") name = "left";
|
|
58
|
+
else if (lastChar === "H") name = "home";
|
|
59
|
+
else if (lastChar === "F") name = "end";
|
|
60
|
+
else if (seq === "\x1b[3~") name = "delete";
|
|
61
|
+
else if (seq === "\x1b[2~") name = "insert";
|
|
62
|
+
else if (seq === "\x1b[5~") name = "pageup";
|
|
63
|
+
else if (seq === "\x1b[6~") name = "pagedown";
|
|
64
|
+
else name = lastChar;
|
|
65
|
+
|
|
66
|
+
stream.emit("keypress", seq, {
|
|
67
|
+
sequence: seq,
|
|
68
|
+
name,
|
|
69
|
+
ctrl: false,
|
|
70
|
+
meta: false,
|
|
71
|
+
shift: false,
|
|
72
|
+
});
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
if (seq.length > 8) {
|
|
76
|
+
stream.emit("keypress", seq, {
|
|
77
|
+
sequence: seq,
|
|
78
|
+
name: "unknown",
|
|
79
|
+
ctrl: false,
|
|
80
|
+
meta: false,
|
|
81
|
+
shift: false,
|
|
82
|
+
});
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
return escapeBuf;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (seq.length >= 3 && seq[1] === "O") {
|
|
89
|
+
const lastChar = seq[seq.length - 1];
|
|
90
|
+
let name = "";
|
|
91
|
+
if (lastChar === "A") name = "up";
|
|
92
|
+
else if (lastChar === "B") name = "down";
|
|
93
|
+
else if (lastChar === "C") name = "right";
|
|
94
|
+
else if (lastChar === "D") name = "left";
|
|
95
|
+
else if (lastChar === "H") name = "home";
|
|
96
|
+
else if (lastChar === "F") name = "end";
|
|
97
|
+
else name = lastChar;
|
|
98
|
+
|
|
99
|
+
stream.emit("keypress", seq, {
|
|
100
|
+
sequence: seq,
|
|
101
|
+
name,
|
|
102
|
+
ctrl: false,
|
|
103
|
+
meta: false,
|
|
104
|
+
shift: false,
|
|
105
|
+
});
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (seq.length >= 2 && seq[1] !== "[" && seq[1] !== "O") {
|
|
110
|
+
stream.emit("keypress", seq, {
|
|
111
|
+
sequence: seq,
|
|
112
|
+
name: "escape",
|
|
113
|
+
ctrl: false,
|
|
114
|
+
meta: true,
|
|
115
|
+
shift: false,
|
|
116
|
+
});
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return escapeBuf;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (char === "\x1b") {
|
|
124
|
+
return [char];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let name = char;
|
|
128
|
+
let ctrl = false;
|
|
129
|
+
if (char === "\r" || char === "\n") name = "return";
|
|
130
|
+
else if (char === "\x7f" || char === "\b") name = "backspace";
|
|
131
|
+
else if (char === "\t") name = "tab";
|
|
132
|
+
else if (char === " ") name = "space";
|
|
133
|
+
else if (char.charCodeAt(0) < 32) {
|
|
134
|
+
// Ctrl+letter
|
|
135
|
+
ctrl = true;
|
|
136
|
+
name = String.fromCharCode(char.charCodeAt(0) + 96);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
stream.emit("keypress", char, {
|
|
140
|
+
sequence: char,
|
|
141
|
+
name,
|
|
142
|
+
ctrl,
|
|
143
|
+
meta: false,
|
|
144
|
+
shift: false,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function emitKeypressEvents(stream: unknown, _iface?: Interface): void {
|
|
151
|
+
if (!stream || typeof (stream as any).on !== "function") return;
|
|
152
|
+
if ((stream as any)[KEYPRESS_DECODER]) return;
|
|
153
|
+
(stream as any)[KEYPRESS_DECODER] = true;
|
|
154
|
+
|
|
155
|
+
let escapeBuf: string[] = [];
|
|
156
|
+
let escapeTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
157
|
+
|
|
158
|
+
(stream as any).on("data", (data: unknown) => {
|
|
159
|
+
if ((stream as any).listenerCount("keypress") <= 0) return;
|
|
160
|
+
const text = typeof data === "string" ? data : String(data);
|
|
161
|
+
|
|
162
|
+
for (const char of text) {
|
|
163
|
+
if (escapeTimeout) {
|
|
164
|
+
clearTimeout(escapeTimeout);
|
|
165
|
+
escapeTimeout = null;
|
|
166
|
+
}
|
|
167
|
+
escapeBuf = parseAndEmitKeypress(stream as any, char, escapeBuf);
|
|
168
|
+
|
|
169
|
+
// flush pending escape after timeout
|
|
170
|
+
if (escapeBuf.length > 0) {
|
|
171
|
+
escapeTimeout = setTimeout(() => {
|
|
172
|
+
if (escapeBuf.length > 0) {
|
|
173
|
+
const seq = escapeBuf.join("");
|
|
174
|
+
(stream as any).emit("keypress", seq, {
|
|
175
|
+
sequence: seq,
|
|
176
|
+
name: "escape",
|
|
177
|
+
ctrl: false,
|
|
178
|
+
meta: false,
|
|
179
|
+
shift: false,
|
|
180
|
+
});
|
|
181
|
+
escapeBuf = [];
|
|
182
|
+
}
|
|
183
|
+
}, 50);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
export interface Interface extends EventEmitter {
|
|
191
|
+
_promptStr: string;
|
|
192
|
+
_input: unknown;
|
|
193
|
+
_output: unknown;
|
|
194
|
+
_closed: boolean;
|
|
195
|
+
_lineBuffer: string;
|
|
196
|
+
_pendingQuestions: Array<{
|
|
197
|
+
query: string;
|
|
198
|
+
handler: (answer: string) => void;
|
|
199
|
+
}>;
|
|
200
|
+
terminal: boolean;
|
|
201
|
+
line: string;
|
|
202
|
+
cursor: number;
|
|
203
|
+
_refreshLine(): void;
|
|
204
|
+
_onKeypress(char: string | undefined, key: any): void;
|
|
205
|
+
_onData(text: string): void;
|
|
206
|
+
prompt(preserveCursor?: boolean): void;
|
|
207
|
+
setPrompt(text: string): void;
|
|
208
|
+
getPrompt(): string;
|
|
209
|
+
question(query: string, optsOrHandler?: unknown, handler?: (answer: string) => void): void;
|
|
210
|
+
pause(): this;
|
|
211
|
+
resume(): this;
|
|
212
|
+
close(): void;
|
|
213
|
+
write(data: string | null, _key?: { ctrl?: boolean; name?: string; meta?: boolean; shift?: boolean; sequence?: string }): void;
|
|
214
|
+
getCursorPos(): { rows: number; cols: number };
|
|
215
|
+
[Symbol.asyncIterator](): AsyncGenerator<string, void, undefined>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
interface InterfaceConstructor {
|
|
219
|
+
new (cfg?: InterfaceConfig): Interface;
|
|
220
|
+
(this: any, cfg?: InterfaceConfig): void;
|
|
221
|
+
prototype: any;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export const Interface = function Interface(this: any, cfg?: InterfaceConfig) {
|
|
225
|
+
if (!this) return;
|
|
226
|
+
EventEmitter.call(this);
|
|
227
|
+
this._promptStr = cfg?.prompt ?? "> ";
|
|
228
|
+
this._input = cfg?.input;
|
|
229
|
+
this._output = cfg?.output;
|
|
230
|
+
this._closed = false;
|
|
231
|
+
this._lineBuffer = "";
|
|
232
|
+
this._pendingQuestions = [];
|
|
233
|
+
this.terminal = cfg?.terminal ?? false;
|
|
234
|
+
this.line = "";
|
|
235
|
+
this.cursor = 0;
|
|
236
|
+
|
|
237
|
+
if (this._input && typeof (this._input as any).on === "function") {
|
|
238
|
+
_activeInterfaceCount++;
|
|
239
|
+
_elRef();
|
|
240
|
+
const inputStream = this._input as EventEmitter;
|
|
241
|
+
const self = this;
|
|
242
|
+
|
|
243
|
+
if (this.terminal) {
|
|
244
|
+
emitKeypressEvents(this._input, this);
|
|
245
|
+
|
|
246
|
+
inputStream.on("keypress", (char: string | undefined, key: any) => {
|
|
247
|
+
if (self._closed) return;
|
|
248
|
+
self._onKeypress(char, key);
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
inputStream.on("data", (data: unknown) => {
|
|
252
|
+
if (self._closed) return;
|
|
253
|
+
const text = typeof data === "string" ? data : String(data);
|
|
254
|
+
self._onData(text);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
inputStream.on("end", () => {
|
|
259
|
+
if (!self._closed) self.close();
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
} as unknown as InterfaceConstructor;
|
|
263
|
+
|
|
264
|
+
Object.setPrototypeOf(Interface.prototype, EventEmitter.prototype);
|
|
265
|
+
|
|
266
|
+
Interface.prototype._refreshLine = function _refreshLine(this: any): void {
|
|
267
|
+
if (this._output && typeof (this._output as any).write === "function") {
|
|
268
|
+
(this._output as any).write(this.line);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
Interface.prototype._onKeypress = function _onKeypress(this: any, char: string | undefined, key: any): void {
|
|
273
|
+
if (!key) return;
|
|
274
|
+
|
|
275
|
+
if (key.ctrl && key.name === "c") {
|
|
276
|
+
this.close();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (key.name === "return") {
|
|
281
|
+
const line = this._lineBuffer;
|
|
282
|
+
this._lineBuffer = "";
|
|
283
|
+
this.line = line;
|
|
284
|
+
|
|
285
|
+
if (this._pendingQuestions.length > 0) {
|
|
286
|
+
const q = this._pendingQuestions.shift()!;
|
|
287
|
+
q.handler(line);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
this.emit("line", line);
|
|
291
|
+
this.line = "";
|
|
292
|
+
this.cursor = 0;
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (key.name === "backspace") {
|
|
297
|
+
if (this.cursor > 0) {
|
|
298
|
+
this._lineBuffer =
|
|
299
|
+
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
300
|
+
this._lineBuffer.slice(this.cursor);
|
|
301
|
+
this.cursor--;
|
|
302
|
+
this.line = this._lineBuffer;
|
|
303
|
+
this._refreshLine();
|
|
304
|
+
}
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (key.name === "delete") {
|
|
309
|
+
if (this.cursor < this._lineBuffer.length) {
|
|
310
|
+
this._lineBuffer =
|
|
311
|
+
this._lineBuffer.slice(0, this.cursor) +
|
|
312
|
+
this._lineBuffer.slice(this.cursor + 1);
|
|
313
|
+
this.line = this._lineBuffer;
|
|
314
|
+
this._refreshLine();
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (key.name === "left") {
|
|
320
|
+
this.cursor = Math.max(0, this.cursor - 1);
|
|
321
|
+
this._refreshLine();
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (key.name === "right") {
|
|
325
|
+
this.cursor = Math.min(this._lineBuffer.length, this.cursor + 1);
|
|
326
|
+
this._refreshLine();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (key.name === "home") {
|
|
330
|
+
this.cursor = 0;
|
|
331
|
+
this._refreshLine();
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (key.name === "end") {
|
|
335
|
+
this.cursor = this._lineBuffer.length;
|
|
336
|
+
this._refreshLine();
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (key.ctrl && key.name === "u") {
|
|
341
|
+
this._lineBuffer = this._lineBuffer.slice(this.cursor);
|
|
342
|
+
this.cursor = 0;
|
|
343
|
+
this.line = this._lineBuffer;
|
|
344
|
+
this._refreshLine();
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (key.ctrl && key.name === "k") {
|
|
349
|
+
this._lineBuffer = this._lineBuffer.slice(0, this.cursor);
|
|
350
|
+
this.line = this._lineBuffer;
|
|
351
|
+
this._refreshLine();
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (key.ctrl && key.name === "h") {
|
|
356
|
+
if (this.cursor > 0) {
|
|
357
|
+
this._lineBuffer =
|
|
358
|
+
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
359
|
+
this._lineBuffer.slice(this.cursor);
|
|
360
|
+
this.cursor--;
|
|
361
|
+
this.line = this._lineBuffer;
|
|
362
|
+
this._refreshLine();
|
|
363
|
+
}
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (
|
|
368
|
+
char &&
|
|
369
|
+
!key.ctrl &&
|
|
370
|
+
!key.meta &&
|
|
371
|
+
char.length === 1 &&
|
|
372
|
+
char.charCodeAt(0) >= 32
|
|
373
|
+
) {
|
|
374
|
+
this._lineBuffer =
|
|
375
|
+
this._lineBuffer.slice(0, this.cursor) +
|
|
376
|
+
char +
|
|
377
|
+
this._lineBuffer.slice(this.cursor);
|
|
378
|
+
this.cursor++;
|
|
379
|
+
this.line = this._lineBuffer;
|
|
380
|
+
this._refreshLine();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
Interface.prototype._onData = function _onData(this: any, text: string): void {
|
|
387
|
+
for (const char of text) {
|
|
388
|
+
if (char === "\n" || char === "\r") {
|
|
389
|
+
const line = this._lineBuffer;
|
|
390
|
+
this._lineBuffer = "";
|
|
391
|
+
this.line = line;
|
|
392
|
+
|
|
393
|
+
if (this._pendingQuestions.length > 0) {
|
|
394
|
+
const q = this._pendingQuestions.shift()!;
|
|
395
|
+
q.handler(line);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
this.emit("line", line);
|
|
399
|
+
this.line = "";
|
|
400
|
+
this.cursor = 0;
|
|
401
|
+
} else if (char === "\x7f" || char === "\b") {
|
|
402
|
+
if (this._lineBuffer.length > 0) {
|
|
403
|
+
this._lineBuffer =
|
|
404
|
+
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
405
|
+
this._lineBuffer.slice(this.cursor);
|
|
406
|
+
this.cursor = Math.max(0, this.cursor - 1);
|
|
407
|
+
this.line = this._lineBuffer;
|
|
408
|
+
this._refreshLine();
|
|
409
|
+
}
|
|
410
|
+
} else if (char.charCodeAt(0) >= 32) {
|
|
411
|
+
this._lineBuffer =
|
|
412
|
+
this._lineBuffer.slice(0, this.cursor) +
|
|
413
|
+
char +
|
|
414
|
+
this._lineBuffer.slice(this.cursor);
|
|
415
|
+
this.cursor++;
|
|
416
|
+
this.line = this._lineBuffer;
|
|
417
|
+
this._refreshLine();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
Interface.prototype.prompt = function prompt(this: any, preserveCursor?: boolean): void {
|
|
423
|
+
if (this._output && typeof (this._output as any).write === "function") {
|
|
424
|
+
(this._output as any).write(this._promptStr);
|
|
425
|
+
}
|
|
426
|
+
if (!preserveCursor) {
|
|
427
|
+
this.cursor = 0;
|
|
428
|
+
this.line = "";
|
|
429
|
+
this._lineBuffer = "";
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
Interface.prototype.setPrompt = function setPrompt(this: any, text: string): void {
|
|
434
|
+
this._promptStr = text;
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
Interface.prototype.getPrompt = function getPrompt(this: any): string {
|
|
438
|
+
return this._promptStr;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
Interface.prototype.question = function question(
|
|
442
|
+
this: any,
|
|
443
|
+
query: string,
|
|
444
|
+
optsOrHandler?: unknown,
|
|
445
|
+
handler?: (answer: string) => void,
|
|
446
|
+
): void {
|
|
447
|
+
const actualHandler =
|
|
448
|
+
typeof optsOrHandler === "function"
|
|
449
|
+
? (optsOrHandler as (answer: string) => void)
|
|
450
|
+
: handler;
|
|
451
|
+
if (!actualHandler) return;
|
|
452
|
+
|
|
453
|
+
if (this._output && typeof (this._output as any).write === "function") {
|
|
454
|
+
(this._output as any).write(query);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (this._input && typeof (this._input as any).on === "function") {
|
|
458
|
+
this._pendingQuestions.push({ query, handler: actualHandler });
|
|
459
|
+
} else {
|
|
460
|
+
setTimeout(() => actualHandler(""), 0);
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
Interface.prototype.pause = function pause(this: any): any {
|
|
465
|
+
if (this._input && typeof (this._input as any).pause === "function") {
|
|
466
|
+
(this._input as any).pause();
|
|
467
|
+
}
|
|
468
|
+
this.emit("pause");
|
|
469
|
+
return this;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
Interface.prototype.resume = function resume(this: any): any {
|
|
473
|
+
if (this._input && typeof (this._input as any).resume === "function") {
|
|
474
|
+
(this._input as any).resume();
|
|
475
|
+
}
|
|
476
|
+
this.emit("resume");
|
|
477
|
+
return this;
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
Interface.prototype.close = function close(this: any): void {
|
|
481
|
+
if (this._closed) return;
|
|
482
|
+
this._closed = true;
|
|
483
|
+
if (this._input && typeof (this._input as any).on === "function") {
|
|
484
|
+
_activeInterfaceCount = Math.max(0, _activeInterfaceCount - 1);
|
|
485
|
+
_elUnref();
|
|
486
|
+
}
|
|
487
|
+
for (const q of this._pendingQuestions) {
|
|
488
|
+
q.handler("");
|
|
489
|
+
}
|
|
490
|
+
this._pendingQuestions.length = 0;
|
|
491
|
+
this.emit("close");
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
Interface.prototype.write = function write(
|
|
495
|
+
this: any,
|
|
496
|
+
data: string | null,
|
|
497
|
+
_key?: {
|
|
498
|
+
ctrl?: boolean;
|
|
499
|
+
name?: string;
|
|
500
|
+
meta?: boolean;
|
|
501
|
+
shift?: boolean;
|
|
502
|
+
sequence?: string;
|
|
503
|
+
},
|
|
504
|
+
): void {
|
|
505
|
+
if (this._closed) return;
|
|
506
|
+
|
|
507
|
+
if (data === null || data === undefined) {
|
|
508
|
+
if (_key) {
|
|
509
|
+
// emit synthetic keypress on input so external listeners (e.g. @clack) see it
|
|
510
|
+
const emitOnInput =
|
|
511
|
+
this._input && typeof (this._input as any).emit === "function";
|
|
512
|
+
|
|
513
|
+
if (_key.ctrl && _key.name === "c") {
|
|
514
|
+
this.close();
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (_key.ctrl && _key.name === "h") {
|
|
518
|
+
// Backspace
|
|
519
|
+
if (this.cursor > 0) {
|
|
520
|
+
this._lineBuffer =
|
|
521
|
+
this._lineBuffer.slice(0, this.cursor - 1) +
|
|
522
|
+
this._lineBuffer.slice(this.cursor);
|
|
523
|
+
this.cursor--;
|
|
524
|
+
this.line = this._lineBuffer;
|
|
525
|
+
}
|
|
526
|
+
if (emitOnInput) {
|
|
527
|
+
(this._input as any).emit("keypress", "\x7f", {
|
|
528
|
+
sequence: "\x7f",
|
|
529
|
+
name: "backspace",
|
|
530
|
+
ctrl: false,
|
|
531
|
+
meta: false,
|
|
532
|
+
shift: false,
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (_key.ctrl && _key.name === "u") {
|
|
538
|
+
this._lineBuffer = this._lineBuffer.slice(this.cursor);
|
|
539
|
+
this.cursor = 0;
|
|
540
|
+
this.line = this._lineBuffer;
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (_key.ctrl && _key.name === "k") {
|
|
544
|
+
this._lineBuffer = this._lineBuffer.slice(0, this.cursor);
|
|
545
|
+
this.line = this._lineBuffer;
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
if (_key.name === "left") {
|
|
549
|
+
this.cursor = Math.max(0, this.cursor - 1);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (_key.name === "right") {
|
|
553
|
+
this.cursor = Math.min(this._lineBuffer.length, this.cursor + 1);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (_key.name === "home") {
|
|
557
|
+
this.cursor = 0;
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (_key.name === "end") {
|
|
561
|
+
this.cursor = this._lineBuffer.length;
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (_key?.ctrl && _key?.name === "c") {
|
|
569
|
+
this.close();
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
this._onData(data);
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
Interface.prototype.getCursorPos = function getCursorPos(this: any): { rows: number; cols: number } {
|
|
577
|
+
return { rows: 0, cols: this.cursor };
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
Interface.prototype[Symbol.asyncIterator] = async function*(this: any): AsyncGenerator<string, void, undefined> {
|
|
581
|
+
const self = this;
|
|
582
|
+
while (!self._closed) {
|
|
583
|
+
const line = await new Promise<string | null>((resolve) => {
|
|
584
|
+
if (self._closed) {
|
|
585
|
+
resolve(null);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
self.once("line", (l: string) => resolve(l));
|
|
589
|
+
self.once("close", () => resolve(null));
|
|
590
|
+
});
|
|
591
|
+
if (line === null) break;
|
|
592
|
+
yield line;
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
export function createInterface(
|
|
598
|
+
cfgOrInput?: InterfaceConfig | unknown,
|
|
599
|
+
output?: unknown,
|
|
600
|
+
): Interface {
|
|
601
|
+
if (
|
|
602
|
+
cfgOrInput &&
|
|
603
|
+
typeof cfgOrInput === "object" &&
|
|
604
|
+
!("on" in (cfgOrInput as any)) &&
|
|
605
|
+
!("read" in (cfgOrInput as any))
|
|
606
|
+
) {
|
|
607
|
+
return new Interface(cfgOrInput as InterfaceConfig);
|
|
608
|
+
}
|
|
609
|
+
return new Interface({ input: cfgOrInput, output });
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
export function clearLine(
|
|
614
|
+
stream: unknown,
|
|
615
|
+
dir: number,
|
|
616
|
+
done?: () => void,
|
|
617
|
+
): boolean {
|
|
618
|
+
if (stream && typeof (stream as any).clearLine === "function") {
|
|
619
|
+
return (stream as any).clearLine(dir, done);
|
|
620
|
+
}
|
|
621
|
+
if (done) done();
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
export function clearScreenDown(stream: unknown, done?: () => void): boolean {
|
|
626
|
+
if (stream && typeof (stream as any).write === "function") {
|
|
627
|
+
(stream as any).write("\x1b[J");
|
|
628
|
+
}
|
|
629
|
+
if (done) done();
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export function cursorTo(
|
|
634
|
+
stream: unknown,
|
|
635
|
+
x: number,
|
|
636
|
+
yOrDone?: number | (() => void),
|
|
637
|
+
done?: () => void,
|
|
638
|
+
): boolean {
|
|
639
|
+
const cb = typeof yOrDone === "function" ? yOrDone : done;
|
|
640
|
+
if (stream && typeof (stream as any).cursorTo === "function") {
|
|
641
|
+
return (stream as any).cursorTo(
|
|
642
|
+
x,
|
|
643
|
+
typeof yOrDone === "number" ? yOrDone : undefined,
|
|
644
|
+
cb,
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
if (cb) cb();
|
|
648
|
+
return true;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
export function moveCursor(
|
|
652
|
+
stream: unknown,
|
|
653
|
+
dx: number,
|
|
654
|
+
dy: number,
|
|
655
|
+
done?: () => void,
|
|
656
|
+
): boolean {
|
|
657
|
+
if (stream && typeof (stream as any).moveCursor === "function") {
|
|
658
|
+
return (stream as any).moveCursor(dx, dy, done);
|
|
659
|
+
}
|
|
660
|
+
if (done) done();
|
|
661
|
+
return true;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
export const promises = {
|
|
666
|
+
createInterface(cfg?: InterfaceConfig) {
|
|
667
|
+
const rl = createInterface(cfg);
|
|
668
|
+
return {
|
|
669
|
+
question(query: string): Promise<string> {
|
|
670
|
+
return new Promise((resolve) => rl.question(query, resolve));
|
|
671
|
+
},
|
|
672
|
+
close(): void {
|
|
673
|
+
rl.close();
|
|
674
|
+
},
|
|
675
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<string, void, undefined> {
|
|
676
|
+
yield* rl;
|
|
677
|
+
},
|
|
678
|
+
};
|
|
679
|
+
},
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
export default {
|
|
684
|
+
Interface,
|
|
685
|
+
createInterface,
|
|
686
|
+
clearLine,
|
|
687
|
+
clearScreenDown,
|
|
688
|
+
cursorTo,
|
|
689
|
+
moveCursor,
|
|
690
|
+
emitKeypressEvents,
|
|
691
|
+
promises,
|
|
692
|
+
};
|