rescript-nodejs icon indicating copy to clipboard operation
rescript-nodejs copied to clipboard

How to use Process module?

Open punund opened this issue 1 year ago • 4 comments

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.

punund avatar Mar 15 '24 22:03 punund

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.

TheSpyder avatar Mar 15 '24 22:03 TheSpyder

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?

punund avatar Mar 16 '24 08:03 punund

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();

TheSpyder avatar Mar 16 '24 08:03 TheSpyder

Or, to answer your actual question (sorry about that)

@scope("process") external exitDefaultWithCode: int => unit = "exit"

would then give you Process.exitDefaultWithCode(0).

TheSpyder avatar Mar 16 '24 08:03 TheSpyder