(
options?: ReplOptions & { code?: string; forceToBeModule?: boolean }
)
| 323 | |
| 324 | // Note: `code` argument is deprecated |
| 325 | function startInternal( |
| 326 | options?: ReplOptions & { code?: string; forceToBeModule?: boolean } |
| 327 | ) { |
| 328 | const { code, forceToBeModule = true, ...optionsOverride } = options ?? {}; |
| 329 | // TODO assert that `service` is set; remove all `service!` non-null assertions |
| 330 | |
| 331 | // Eval incoming code before the REPL starts. |
| 332 | // Note: deprecated |
| 333 | if (code) { |
| 334 | try { |
| 335 | evalCode(`${code}\n`); |
| 336 | } catch (err) { |
| 337 | _console.error(err); |
| 338 | // Note: should not be killing the process here, but this codepath is deprecated anyway |
| 339 | process.exit(1); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // In case the typescript compiler hasn't compiled anything yet, |
| 344 | // make it run though compilation at least one time before |
| 345 | // the REPL starts for a snappier user experience on startup. |
| 346 | service?.compile('', state.path); |
| 347 | |
| 348 | const repl = nodeReplStart({ |
| 349 | prompt: '> ', |
| 350 | input: replService.stdin, |
| 351 | output: replService.stdout, |
| 352 | // Mimicking node's REPL implementation: https://github.com/nodejs/node/blob/168b22ba073ee1cbf8d0bcb4ded7ff3099335d04/lib/internal/repl.js#L28-L30 |
| 353 | terminal: |
| 354 | (stdout as tty.WriteStream).isTTY && |
| 355 | !parseInt(env.NODE_NO_READLINE!, 10), |
| 356 | eval: nodeEval, |
| 357 | useGlobal: true, |
| 358 | ...optionsOverride, |
| 359 | }); |
| 360 | |
| 361 | nodeReplServer = repl; |
| 362 | context = repl.context; |
| 363 | |
| 364 | // Bookmark the point where we should reset the REPL state. |
| 365 | const resetEval = appendToEvalState(state, ''); |
| 366 | |
| 367 | function reset() { |
| 368 | resetEval(); |
| 369 | |
| 370 | // Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`. |
| 371 | runInContext('exports = module.exports', state.path, context); |
| 372 | if (forceToBeModule) { |
| 373 | state.input += 'export {};void 0;\n'; |
| 374 | } |
| 375 | |
| 376 | // Declare node builtins. |
| 377 | // Skip the same builtins as `addBuiltinLibsToObject`: |
| 378 | // those starting with _ |
| 379 | // those containing / |
| 380 | // those that already exist as globals |
| 381 | // Intentionally suppress type errors in case @types/node does not declare any of them, and because |
| 382 | // `declare import` is technically invalid syntax. |
no test coverage detected
searching dependent graphs…