UNPKG

2.89 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Service, CreateOptions } from './index';
3export 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 */
41export 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 */
64export declare function createRepl(options?: CreateReplOptions): ReplService;
65/**
66 * Eval state management. Stores virtual `[eval].ts` file
67 */
68export 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 */
77export declare type EvalAwarePartialHost = Pick<CreateOptions, 'readFile' | 'fileExists'>;
78export declare function createEvalAwarePartialHost(state: EvalState, composeWith?: EvalAwarePartialHost): EvalAwarePartialHost;