TanklesXL / glint

Gleam command-line argument parsing with flags and automated help text generation.
https://hex.pm/packages/glint
Apache License 2.0
62 stars 9 forks source link

`node:process` not available on browser #55

Open ghivert opened 2 months ago

ghivert commented 2 months ago

Hi! Thanks for glint, the package is really nice!

I'm wondering about embedding of a gleam runtime in frontend. Currently, when targeting JS, the compiler will output a import * as process from 'node:process' in glint. This means that even if you don't use any functions from glint, the code will crash in browser. Do you think there's a way to avoid this? 🙂

To give a little more context, my idea would be to present only one package, that could be used as a CLI or programmatically, whether in Node/Deno/Bun or in browser. I.e. a universal package, able to be used everywhere.


For example, I'm wondering if we could modify the exit function (which is the only one that requires node:process) to something like:

export function exit(code) {
  import('node:process').then(process => process.exit(code))
  return new gleam.Error('process.exit')
}

In Node & other runtimes, it would not change anything (because process.exit would be called everytime), while in browser it will return an Error in Gleam.

TanklesXL commented 2 months ago

Oh that's interesting, thanks for bringing this up!

I have been thinking about the exit behaviours and I wonder if I should have glint return a result or exit code rather than exiting on its own, that would let the user determine what behaviour best fits for them and then removes any target-specific code from glint

TanklesXL commented 2 months ago

How does the typing of that new exit function work? I would need to modify it so that glint returns a result I think?

ghivert commented 2 months ago

After more thoughts, I think the best would be to simply return a Result(a, Error) and let the user handle the value as you suggest.

In the CLI, we can probably just use the exit function I wrote and return null instead of gleam.Error. So the typing will be the same. Node always make sure that running Promise finishes before shutting down the process. So it will ensures process.exit will be called.