TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.84k stars 535 forks source link

Feature request: startup script for interactive repl #1861

Open tslocke opened 2 years ago

tslocke commented 2 years ago

Desired Behaviour

When starting an interactive repl, check for the presence of something like .ts-node-repl.ts in the working directory. If present, run the script at startup. The behaviour should be exactly as if those same lines were typed at the repl prompt. In particular, any bindings created should be available in the repl.

Is this request related to a problem?

When working on a project (as opposed to a single file script), Clojure and Rails have taught be the tremendous benefits of having a fully featured repl readily available.

ts-node gives me this for Typescript projects, but the convenience leaves something to be desired. Whenever I fire up the repl it will be because I want to play around with some modules/functions from my project, and it tends to be the same modules over and over. e.g. in a web app backend, I am constantly importing the database module.

It would be super useful to have a startup script that imports all the common things I'm likely to want to get at. It's also a good place to define useful repl helper functions. The script would differ on a per-project basis, hence checking for it in $CWD. Even better would be to recurse up the directory hierarchy up to $HOME, running any script found (in top-down order, e.g. $HOME/.ts-node-repl.ts first). That way I could have both shared helpers and project helpers.

Alternatives you've considered

Creating a shell alias, something like alias tsrepl='ts-node -r tsconfig-paths/register --transpileOnly ./.ts-node-repl.ts

In that case, the script would be a bit different - it would fire up a ts-node repl using the API. That could be a perfectly good solution, but so far any attempt has ended up with a repl that (unlike the default one) doesn't have top-level await enabled, which is a much greater inconvenience than the problem I was trying to solve : )

I would be happy with a solution to the top-level await issue, but I do think this would be a great feature to have in the core library.

cspotcode commented 2 years ago

Does node have any equivalent functionality? This is a chunk of added complexity, so ideally node or a third-party library already provides this, and we simply maintain compatibility.

I suspect you'll have more luck proposing that we adjust our API to allow a third-party tool to implement this functionality on top of ts-node. But we can discuss.