Files
4333c92734 Make ts-node eval public for node REPL consumption (#1121)
* Add createReplEval function

* Add a test

* Fix a couple of typos

* Actually test the evaluator

* Try removing spec

* Use service creation pattern

* Move REPL code to its own file

* - Reorder repl.ts declarations to match the original order from bin.ts
- promote createReplService to top of the file since it will hopefully be the
main entrypoint to any REPL functionality

* Expand ReplService API; use new API in bin.ts, which further decouples bin.ts from repl.ts

* Add brief, internal docs for REPL API

* Add support for DI of alternative stdio streams into REPL

* export REPL from index.ts

* remove unnecessary export

* Add test for new REPL API

* API surface, naming, docs tweaks

* tweak identifiers

* fix name

* Tweak repl API test to match REPL CLI test, allowing it to pass on Windows

Co-authored-by: Andrew Bradley <[email protected]>
2020-12-03 01:33:39 -05:00

748 B

How to create your own ts-node powered REPL

  • Create ts-node REPL service which includes EvalState
  • Create ts-node compiler service using EvalState-aware readFile and fileExists implementations from REPL
  • Bind REPL service to compiler service (chicken-and-egg problem necessitates late binding)
  • Either:
    • call REPL method start() to start a REPL
    • create your own node repl but pass it REPL service's nodeEval() function
import * as tsnode from 'ts-node';
const repl = tsnode.createRepl();
const service = tsnode.register({
    ... options,
    ...repl.evalAwarePartialHost
});
repl.setService(service);

// Start it
repl.start();

// or
const nodeRepl = require('repl').start({
    ...options,
    eval: repl.nodeEval
});