repl.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /// <reference types="node" />
  2. import { Service, CreateOptions } from './index';
  3. export interface ReplService {
  4. readonly state: EvalState;
  5. /**
  6. * Bind this REPL to a ts-node compiler service. A compiler service must be bound before `eval`-ing code or starting the REPL
  7. */
  8. setService(service: Service): void;
  9. /**
  10. * Append code to the virtual <repl> source file, compile it to JavaScript, throw semantic errors if the typechecker is enabled,
  11. * and execute it.
  12. *
  13. * Note: typically, you will want to call `start()` instead of using this method.
  14. *
  15. * @param code string of TypeScript.
  16. */
  17. evalCode(code: string): any;
  18. /**
  19. * `eval` implementation compatible with node's REPL API
  20. *
  21. * Can be used in advanced scenarios if you want to manually create your own
  22. * node REPL instance and delegate eval to this `ReplService`.
  23. *
  24. * Example:
  25. *
  26. * import {start} from 'repl';
  27. * const replService: tsNode.ReplService = ...; // assuming you have already created a ts-node ReplService
  28. * const nodeRepl = start({eval: replService.eval});
  29. */
  30. nodeEval(code: string, context: any, _filename: string, callback: (err: Error | null, result?: any) => any): void;
  31. evalAwarePartialHost: EvalAwarePartialHost;
  32. /** Start a node REPL */
  33. start(): void;
  34. /**
  35. * Start a node REPL, evaling a string of TypeScript before it starts.
  36. * @deprecated
  37. */
  38. start(code: string): void;
  39. }
  40. /** @category REPL */
  41. export interface CreateReplOptions {
  42. service?: Service;
  43. state?: EvalState;
  44. stdin?: NodeJS.ReadableStream;
  45. stdout?: NodeJS.WritableStream;
  46. stderr?: NodeJS.WritableStream;
  47. }
  48. /**
  49. * Create a ts-node REPL instance.
  50. *
  51. * Pay close attention to the example below. Today, the API requires a few lines
  52. * of boilerplate to correctly bind the `ReplService` to the ts-node `Service` and
  53. * vice-versa.
  54. *
  55. * Usage example:
  56. *
  57. * const repl = tsNode.createRepl();
  58. * const service = tsNode.create({...repl.evalAwarePartialHost});
  59. * repl.setService(service);
  60. * repl.start();
  61. *
  62. * @category REPL
  63. */
  64. export declare function createRepl(options?: CreateReplOptions): ReplService;
  65. /**
  66. * Eval state management. Stores virtual `[eval].ts` file
  67. */
  68. export declare class EvalState {
  69. path: string;
  70. __tsNodeEvalStateBrand: unknown;
  71. constructor(path: string);
  72. }
  73. /**
  74. * Filesystem host functions which are aware of the "virtual" `[eval].ts`, `<repl>`, or `[stdin].ts` file used to compile REPL inputs.
  75. * Must be passed to `create()` to create a ts-node compiler service which can compile REPL inputs.
  76. */
  77. export declare type EvalAwarePartialHost = Pick<CreateOptions, 'readFile' | 'fileExists'>;
  78. export declare function createEvalAwarePartialHost(state: EvalState, composeWith?: EvalAwarePartialHost): EvalAwarePartialHost;