repl.d.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. evalCode(code: string): void;
  10. /**
  11. * `eval` implementation compatible with node's REPL API
  12. */
  13. nodeEval(code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any): void;
  14. evalAwarePartialHost: EvalAwarePartialHost;
  15. /** Start a node REPL */
  16. start(code?: string): void;
  17. }
  18. export interface CreateReplOptions {
  19. service?: Service;
  20. state?: EvalState;
  21. stdin?: NodeJS.ReadableStream;
  22. stdout?: NodeJS.WritableStream;
  23. stderr?: NodeJS.WritableStream;
  24. }
  25. export declare function createRepl(options?: CreateReplOptions): ReplService;
  26. /**
  27. * Eval state management. Stores virtual `[eval].ts` file
  28. */
  29. export declare class EvalState {
  30. path: string;
  31. __tsNodeEvalStateBrand: unknown;
  32. constructor(path: string);
  33. }
  34. /**
  35. * Filesystem host functions which are aware of the "virtual" [eval].ts file used to compile REPL inputs.
  36. * Must be passed to `create()` to create a ts-node compiler service which can compile REPL inputs.
  37. */
  38. export declare type EvalAwarePartialHost = Pick<CreateOptions, 'readFile' | 'fileExists'>;
  39. export declare function createEvalAwarePartialHost(state: EvalState): EvalAwarePartialHost;