rescript-nodejs
rescript-nodejs copied to clipboard
How to use Process module?
Unless I am not seeing something very obvious, why is everything in Process module typed (t, ...) => ...?
Something as seemingly trivial as NodeJs.Process.exit(0) just wouldn't work.
This library was written before records mapped to JS objects, so it’s a little more cumbersome than modern ReScript. Fixing that is on my list to do, but that’s a big job.
There is an instance of type t declared at the top of Process. So if you open NodeJS.Process, you can do process->exit(0).
As for why exit takes a t at all? The underlying function does too, nodejs just has an overload to apply to the default process.
we could look at adding a second mapping that applies to the default - that’s very easy to do.
Thank you. I prefer not to open modules, so I ended up with
NodeJs.Process.exitWithCode(NodeJs.Process.process, 0)
which may or may not look a littlte too much for the task :(
What would the alternative mapping look like?
I can understand not wanting to open entire modules coming from JS, but ReScript works a bit differently. If you look at the compiled JS it's importing entire modules regardless and only using what it needs to 🤷♂️
An alternate binding would be:
@scope("process") external exitDefault: unit => unit = "exit"
If this was added to the Process module here (or if you made one in your own project), Process.exitDefault() will then emit
process.exit();
Or, to answer your actual question (sorry about that)
@scope("process") external exitDefaultWithCode: int => unit = "exit"
would then give you Process.exitDefaultWithCode(0).