|
1 | | -import REPL, { REPLEval, ReplOptions, REPLServer } from 'repl'; |
2 | | -import vm from 'vm'; |
3 | | -import { createServer, Server } from 'net'; |
4 | 1 | import { makeModule } from '@/context'; |
| 2 | +import { makeREPL, REPLConfigType } from '@/_lib/repl'; |
5 | 3 |
|
6 | | -type REPLConfig = { |
7 | | - appName: string; |
8 | | - cli: boolean; |
9 | | - repl: { |
10 | | - port: number; |
11 | | - }; |
12 | | -}; |
13 | | - |
14 | | -const repl = makeModule( |
15 | | - 'repl', |
16 | | - async ({ |
17 | | - app: { onReady, terminate }, |
18 | | - container, |
19 | | - config: { |
20 | | - appName, |
21 | | - cli, |
22 | | - environment, |
23 | | - repl: { port }, |
24 | | - }, |
25 | | - logger, |
26 | | - }) => { |
27 | | - const promisableEval: REPLEval = (cmd, context, filename, callback) => { |
28 | | - const result = vm.runInContext(cmd, context); |
29 | | - |
30 | | - if (isPromise(result)) { |
31 | | - return result.then((v) => callback(null, v)).catch((e) => callback(e, null)); |
32 | | - } |
33 | | - |
34 | | - return callback(null, result); |
35 | | - }; |
36 | | - |
37 | | - const isPromise = (value) => value && typeof value.then === 'function' && typeof value.catch === 'function'; |
38 | | - |
39 | | - const createREPL = ( |
40 | | - config: Partial<ReplOptions> = { input: process.stdin, output: process.stdout } |
41 | | - ): REPLServer => { |
42 | | - const repl = REPL.start({ |
43 | | - eval: promisableEval, |
44 | | - prompt: `${appName}$ `, |
45 | | - ignoreUndefined: true, |
46 | | - ...config, |
47 | | - }); |
48 | | - |
49 | | - Object.assign(repl.context, { registry: container.cradle, container }); |
| 4 | +type REPLConfig = REPLConfigType<{ appName: string; cli: boolean; repl: { port: number } }>; |
50 | 5 |
|
51 | | - return repl; |
52 | | - }; |
| 6 | +const repl = makeModule('repl', async ({ app: { onReady, terminate }, container, config, logger }) => { |
| 7 | + const repl = makeREPL({ container, config, logger }); |
53 | 8 |
|
54 | | - let server: Server; |
| 9 | + onReady(async () => { |
| 10 | + await repl.start({ terminate }); |
| 11 | + }); |
55 | 12 |
|
56 | | - const startREPL = async () => { |
57 | | - if (cli) { |
58 | | - const repl = createREPL(); |
59 | | - |
60 | | - repl.on('close', terminate); |
61 | | - } else if (!['production', 'test'].includes(environment)) { |
62 | | - server = createServer((socket) => { |
63 | | - const repl = createREPL({ |
64 | | - input: socket, |
65 | | - output: socket, |
66 | | - terminal: true, |
67 | | - }); |
68 | | - |
69 | | - repl.on('close', () => { |
70 | | - socket.end(); |
71 | | - }); |
72 | | - |
73 | | - socket.on('error', (err) => { |
74 | | - logger.error('[REPL] Connection error'); |
75 | | - logger.error(err); |
76 | | - socket.end(); |
77 | | - }); |
78 | | - }).listen(port); |
79 | | - } |
80 | | - }; |
81 | | - |
82 | | - onReady(startREPL); |
83 | | - |
84 | | - return async () => { |
85 | | - if (server && server.listening) { |
86 | | - await new Promise<void>((resolve, reject) => |
87 | | - server.close((err) => { |
88 | | - if (err) return reject(err); |
89 | | - resolve(); |
90 | | - }) |
91 | | - ); |
92 | | - } |
93 | | - }; |
94 | | - } |
95 | | -); |
| 13 | + return async () => { |
| 14 | + await repl.close(); |
| 15 | + }; |
| 16 | +}); |
96 | 17 |
|
97 | 18 | export { repl }; |
98 | 19 | export type { REPLConfig }; |
0 commit comments