Ts-node: Extending ts-node

Created on 16 Mar 2020  Â·  3Comments  Â·  Source: TypeStrong/ts-node

Desired Behavior

Allow complete control over the creation of a new REPL in code, including the ability to inject a new custom context.

Is this request related to a problem?

There are many instances where one may want to extend the behavior of ts-node by creating their own REPL that wraps it and injects a custom context. One such case is creating a REPL for a framework such as AdonisJS, which is what I'm trying to do. The problem currently is, as far as I can tell, there is no way to inject a custom context outside of using the --require flag, but that just requires a file.

It would be nice to, in my own code, be able to do something like this:

import { REPL, Context } from 'ts-node'

const myContext = new Context({
    myGlobal: require('./some-file')
})

const repl =  new REPL(myContext)
repl.run()

Alternatives you've considered

Forking and modifying ts-node or a similar project to fit my needs.

enhancement help wanted

All 3 comments

Seems reasonable to me. This would mean refactoring startRepl to be a public API. The guts of it would move from bin.ts to index.ts, probably wrapped us as a createRepl function.
https://github.com/TypeStrong/ts-node/blob/master/src/bin.ts#L327-L330

import { createRepl, register } from 'ts-node';
const service = register(... options ...);
const repl = createRepl({
    service,
    context: myContext
});

Alternatively, we expose only the eval implementation via a createReplEval function.
https://github.com/TypeStrong/ts-node/blob/master/src/bin.ts#L350-L353

That way you fully control construction of the REPL, simply passing it a ts-node eval implementation.

Not something I have bandwidth to implement, but I'd personally accept a PR as long as it didn't impose additional burden on maintainers. Seems like it wouldn't since it's not adding new features, just a tiny bit more public API surface.

Glad you're open to the idea. If I get some free time I'll open a PR.

I would also have use for this in order to reproduce something like a rails console for the ts repo I currently work in. I'm right where y'all are in terms of bandwidth, so who knows whether I'll have the time to build this ¯_(ツ)_/¯. At any rate, I wanted to say @watzon, you're not alone 🙂

Was this page helpful?
0 / 5 - 0 ratings