UNPKG

17.9 kBTypeScriptView Raw
1declare module "repl" {
2 import { Interface, Completer, AsyncCompleter } from "readline";
3 import { Context } from "vm";
4 import { InspectOptions } from "util";
5
6 interface ReplOptions {
7 /**
8 * The input prompt to display.
9 * Default: `"> "`
10 */
11 prompt?: string;
12 /**
13 * The `Readable` stream from which REPL input will be read.
14 * Default: `process.stdin`
15 */
16 input?: NodeJS.ReadableStream;
17 /**
18 * The `Writable` stream to which REPL output will be written.
19 * Default: `process.stdout`
20 */
21 output?: NodeJS.WritableStream;
22 /**
23 * If `true`, specifies that the output should be treated as a TTY terminal, and have
24 * ANSI/VT100 escape codes written to it.
25 * Default: checking the value of the `isTTY` property on the output stream upon
26 * instantiation.
27 */
28 terminal?: boolean;
29 /**
30 * The function to be used when evaluating each given line of input.
31 * Default: an async wrapper for the JavaScript `eval()` function. An `eval` function can
32 * error with `repl.Recoverable` to indicate the input was incomplete and prompt for
33 * additional lines.
34 *
35 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_default_evaluation
36 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_custom_evaluation_functions
37 */
38 eval?: REPLEval;
39 /**
40 * Defines if the repl prints output previews or not.
41 * @default `true` Always `false` in case `terminal` is falsy.
42 */
43 preview?: boolean;
44 /**
45 * If `true`, specifies that the default `writer` function should include ANSI color
46 * styling to REPL output. If a custom `writer` function is provided then this has no
47 * effect.
48 * Default: the REPL instance's `terminal` value.
49 */
50 useColors?: boolean;
51 /**
52 * If `true`, specifies that the default evaluation function will use the JavaScript
53 * `global` as the context as opposed to creating a new separate context for the REPL
54 * instance. The node CLI REPL sets this value to `true`.
55 * Default: `false`.
56 */
57 useGlobal?: boolean;
58 /**
59 * If `true`, specifies that the default writer will not output the return value of a
60 * command if it evaluates to `undefined`.
61 * Default: `false`.
62 */
63 ignoreUndefined?: boolean;
64 /**
65 * The function to invoke to format the output of each command before writing to `output`.
66 * Default: a wrapper for `util.inspect`.
67 *
68 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_customizing_repl_output
69 */
70 writer?: REPLWriter;
71 /**
72 * An optional function used for custom Tab auto completion.
73 *
74 * @see https://nodejs.org/dist/latest-v11.x/docs/api/readline.html#readline_use_of_the_completer_function
75 */
76 completer?: Completer | AsyncCompleter;
77 /**
78 * A flag that specifies whether the default evaluator executes all JavaScript commands in
79 * strict mode or default (sloppy) mode.
80 * Accepted values are:
81 * - `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
82 * - `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to
83 * prefacing every repl statement with `'use strict'`.
84 */
85 replMode?: typeof REPL_MODE_SLOPPY | typeof REPL_MODE_STRICT;
86 /**
87 * Stop evaluating the current piece of code when `SIGINT` is received, i.e. `Ctrl+C` is
88 * pressed. This cannot be used together with a custom `eval` function.
89 * Default: `false`.
90 */
91 breakEvalOnSigint?: boolean;
92 }
93
94 type REPLEval = (this: REPLServer, evalCmd: string, context: Context, file: string, cb: (err: Error | null, result: any) => void) => void;
95 type REPLWriter = (this: REPLServer, obj: any) => string;
96
97 /**
98 * This is the default "writer" value, if none is passed in the REPL options,
99 * and it can be overridden by custom print functions.
100 */
101 const writer: REPLWriter & { options: InspectOptions };
102
103 type REPLCommandAction = (this: REPLServer, text: string) => void;
104
105 interface REPLCommand {
106 /**
107 * Help text to be displayed when `.help` is entered.
108 */
109 help?: string;
110 /**
111 * The function to execute, optionally accepting a single string argument.
112 */
113 action: REPLCommandAction;
114 }
115
116 /**
117 * Provides a customizable Read-Eval-Print-Loop (REPL).
118 *
119 * Instances of `repl.REPLServer` will accept individual lines of user input, evaluate those
120 * according to a user-defined evaluation function, then output the result. Input and output
121 * may be from `stdin` and `stdout`, respectively, or may be connected to any Node.js `stream`.
122 *
123 * Instances of `repl.REPLServer` support automatic completion of inputs, simplistic Emacs-style
124 * line editing, multi-line inputs, ANSI-styled output, saving and restoring current REPL session
125 * state, error recovery, and customizable evaluation functions.
126 *
127 * Instances of `repl.REPLServer` are created using the `repl.start()` method and _should not_
128 * be created directly using the JavaScript `new` keyword.
129 *
130 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_repl
131 */
132 class REPLServer extends Interface {
133 /**
134 * The `vm.Context` provided to the `eval` function to be used for JavaScript
135 * evaluation.
136 */
137 readonly context: Context;
138 /**
139 * The `Readable` stream from which REPL input will be read.
140 */
141 readonly inputStream: NodeJS.ReadableStream;
142 /**
143 * The `Writable` stream to which REPL output will be written.
144 */
145 readonly outputStream: NodeJS.WritableStream;
146 /**
147 * The commands registered via `replServer.defineCommand()`.
148 */
149 readonly commands: { readonly [name: string]: REPLCommand | undefined };
150 /**
151 * A value indicating whether the REPL is currently in "editor mode".
152 *
153 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_commands_and_special_keys
154 */
155 readonly editorMode: boolean;
156 /**
157 * A value indicating whether the `_` variable has been assigned.
158 *
159 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
160 */
161 readonly underscoreAssigned: boolean;
162 /**
163 * The last evaluation result from the REPL (assigned to the `_` variable inside of the REPL).
164 *
165 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
166 */
167 readonly last: any;
168 /**
169 * A value indicating whether the `_error` variable has been assigned.
170 *
171 * @since v9.8.0
172 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
173 */
174 readonly underscoreErrAssigned: boolean;
175 /**
176 * The last error raised inside the REPL (assigned to the `_error` variable inside of the REPL).
177 *
178 * @since v9.8.0
179 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
180 */
181 readonly lastError: any;
182 /**
183 * Specified in the REPL options, this is the function to be used when evaluating each
184 * given line of input. If not specified in the REPL options, this is an async wrapper
185 * for the JavaScript `eval()` function.
186 */
187 readonly eval: REPLEval;
188 /**
189 * Specified in the REPL options, this is a value indicating whether the default
190 * `writer` function should include ANSI color styling to REPL output.
191 */
192 readonly useColors: boolean;
193 /**
194 * Specified in the REPL options, this is a value indicating whether the default `eval`
195 * function will use the JavaScript `global` as the context as opposed to creating a new
196 * separate context for the REPL instance.
197 */
198 readonly useGlobal: boolean;
199 /**
200 * Specified in the REPL options, this is a value indicating whether the default `writer`
201 * function should output the result of a command if it evaluates to `undefined`.
202 */
203 readonly ignoreUndefined: boolean;
204 /**
205 * Specified in the REPL options, this is the function to invoke to format the output of
206 * each command before writing to `outputStream`. If not specified in the REPL options,
207 * this will be a wrapper for `util.inspect`.
208 */
209 readonly writer: REPLWriter;
210 /**
211 * Specified in the REPL options, this is the function to use for custom Tab auto-completion.
212 */
213 readonly completer: Completer | AsyncCompleter;
214 /**
215 * Specified in the REPL options, this is a flag that specifies whether the default `eval`
216 * function should execute all JavaScript commands in strict mode or default (sloppy) mode.
217 * Possible values are:
218 * - `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
219 * - `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to
220 * prefacing every repl statement with `'use strict'`.
221 */
222 readonly replMode: typeof REPL_MODE_SLOPPY | typeof REPL_MODE_STRICT;
223
224 /**
225 * NOTE: According to the documentation:
226 *
227 * > Instances of `repl.REPLServer` are created using the `repl.start()` method and
228 * > _should not_ be created directly using the JavaScript `new` keyword.
229 *
230 * `REPLServer` cannot be subclassed due to implementation specifics in NodeJS.
231 *
232 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_class_replserver
233 */
234 private constructor();
235
236 /**
237 * Used to add new `.`-prefixed commands to the REPL instance. Such commands are invoked
238 * by typing a `.` followed by the `keyword`.
239 *
240 * @param keyword The command keyword (_without_ a leading `.` character).
241 * @param cmd The function to invoke when the command is processed.
242 *
243 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_replserver_definecommand_keyword_cmd
244 */
245 defineCommand(keyword: string, cmd: REPLCommandAction | REPLCommand): void;
246 /**
247 * Readies the REPL instance for input from the user, printing the configured `prompt` to a
248 * new line in the `output` and resuming the `input` to accept new input.
249 *
250 * When multi-line input is being entered, an ellipsis is printed rather than the 'prompt'.
251 *
252 * This method is primarily intended to be called from within the action function for
253 * commands registered using the `replServer.defineCommand()` method.
254 *
255 * @param preserveCursor When `true`, the cursor placement will not be reset to `0`.
256 */
257 displayPrompt(preserveCursor?: boolean): void;
258 /**
259 * Clears any command that has been buffered but not yet executed.
260 *
261 * This method is primarily intended to be called from within the action function for
262 * commands registered using the `replServer.defineCommand()` method.
263 *
264 * @since v9.0.0
265 */
266 clearBufferedCommand(): void;
267
268 /**
269 * Initializes a history log file for the REPL instance. When executing the
270 * Node.js binary and using the command line REPL, a history file is initialized
271 * by default. However, this is not the case when creating a REPL
272 * programmatically. Use this method to initialize a history log file when working
273 * with REPL instances programmatically.
274 * @param path The path to the history file
275 */
276 setupHistory(path: string, cb: (err: Error | null, repl: this) => void): void;
277
278 /**
279 * events.EventEmitter
280 * 1. close - inherited from `readline.Interface`
281 * 2. line - inherited from `readline.Interface`
282 * 3. pause - inherited from `readline.Interface`
283 * 4. resume - inherited from `readline.Interface`
284 * 5. SIGCONT - inherited from `readline.Interface`
285 * 6. SIGINT - inherited from `readline.Interface`
286 * 7. SIGTSTP - inherited from `readline.Interface`
287 * 8. exit
288 * 9. reset
289 */
290
291 addListener(event: string, listener: (...args: any[]) => void): this;
292 addListener(event: "close", listener: () => void): this;
293 addListener(event: "line", listener: (input: string) => void): this;
294 addListener(event: "pause", listener: () => void): this;
295 addListener(event: "resume", listener: () => void): this;
296 addListener(event: "SIGCONT", listener: () => void): this;
297 addListener(event: "SIGINT", listener: () => void): this;
298 addListener(event: "SIGTSTP", listener: () => void): this;
299 addListener(event: "exit", listener: () => void): this;
300 addListener(event: "reset", listener: (context: Context) => void): this;
301
302 emit(event: string | symbol, ...args: any[]): boolean;
303 emit(event: "close"): boolean;
304 emit(event: "line", input: string): boolean;
305 emit(event: "pause"): boolean;
306 emit(event: "resume"): boolean;
307 emit(event: "SIGCONT"): boolean;
308 emit(event: "SIGINT"): boolean;
309 emit(event: "SIGTSTP"): boolean;
310 emit(event: "exit"): boolean;
311 emit(event: "reset", context: Context): boolean;
312
313 on(event: string, listener: (...args: any[]) => void): this;
314 on(event: "close", listener: () => void): this;
315 on(event: "line", listener: (input: string) => void): this;
316 on(event: "pause", listener: () => void): this;
317 on(event: "resume", listener: () => void): this;
318 on(event: "SIGCONT", listener: () => void): this;
319 on(event: "SIGINT", listener: () => void): this;
320 on(event: "SIGTSTP", listener: () => void): this;
321 on(event: "exit", listener: () => void): this;
322 on(event: "reset", listener: (context: Context) => void): this;
323
324 once(event: string, listener: (...args: any[]) => void): this;
325 once(event: "close", listener: () => void): this;
326 once(event: "line", listener: (input: string) => void): this;
327 once(event: "pause", listener: () => void): this;
328 once(event: "resume", listener: () => void): this;
329 once(event: "SIGCONT", listener: () => void): this;
330 once(event: "SIGINT", listener: () => void): this;
331 once(event: "SIGTSTP", listener: () => void): this;
332 once(event: "exit", listener: () => void): this;
333 once(event: "reset", listener: (context: Context) => void): this;
334
335 prependListener(event: string, listener: (...args: any[]) => void): this;
336 prependListener(event: "close", listener: () => void): this;
337 prependListener(event: "line", listener: (input: string) => void): this;
338 prependListener(event: "pause", listener: () => void): this;
339 prependListener(event: "resume", listener: () => void): this;
340 prependListener(event: "SIGCONT", listener: () => void): this;
341 prependListener(event: "SIGINT", listener: () => void): this;
342 prependListener(event: "SIGTSTP", listener: () => void): this;
343 prependListener(event: "exit", listener: () => void): this;
344 prependListener(event: "reset", listener: (context: Context) => void): this;
345
346 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
347 prependOnceListener(event: "close", listener: () => void): this;
348 prependOnceListener(event: "line", listener: (input: string) => void): this;
349 prependOnceListener(event: "pause", listener: () => void): this;
350 prependOnceListener(event: "resume", listener: () => void): this;
351 prependOnceListener(event: "SIGCONT", listener: () => void): this;
352 prependOnceListener(event: "SIGINT", listener: () => void): this;
353 prependOnceListener(event: "SIGTSTP", listener: () => void): this;
354 prependOnceListener(event: "exit", listener: () => void): this;
355 prependOnceListener(event: "reset", listener: (context: Context) => void): this;
356 }
357
358 /**
359 * A flag passed in the REPL options. Evaluates expressions in sloppy mode.
360 */
361 const REPL_MODE_SLOPPY: unique symbol;
362
363 /**
364 * A flag passed in the REPL options. Evaluates expressions in strict mode.
365 * This is equivalent to prefacing every repl statement with `'use strict'`.
366 */
367 const REPL_MODE_STRICT: unique symbol;
368
369 /**
370 * Creates and starts a `repl.REPLServer` instance.
371 *
372 * @param options The options for the `REPLServer`. If `options` is a string, then it specifies
373 * the input prompt.
374 */
375 function start(options?: string | ReplOptions): REPLServer;
376
377 /**
378 * Indicates a recoverable error that a `REPLServer` can use to support multi-line input.
379 *
380 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_recoverable_errors
381 */
382 class Recoverable extends SyntaxError {
383 err: Error;
384
385 constructor(err: Error);
386 }
387}