nest-commander icon indicating copy to clipboard operation
nest-commander copied to clipboard

Return values

Open lamuertepeluda opened this issue 1 year ago • 4 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

I need some way of returning values from a command (e.g. strings). But I see commands only return void.

Describe the solution you'd like

Return values from executed command

E.g.

MY_VAL=`node dist/cli.ts my-command --some-options` # suppose it returns 'hello!'
echo $MY_VAL # return 'hello'

I see the signature of the cmd.run interface is Promise<void>

Is there any way of returning values?

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Bash scripting, process forking

lamuertepeluda avatar Apr 22 '24 16:04 lamuertepeluda

Technically, the output of process.stdout is what would get assigned to that variable. So a simple process.stdout.write('Whatever') would set $MY_VAL to 'Whatever'

jmcdo29 avatar Apr 22 '24 18:04 jmcdo29

Technically, the output of process.stdout is what would get assigned to that variable. So a simple process.stdout.write('Whatever') would set $MY_VAL to 'Whatever'

Thanks for the workaround. Effective although it feels a bit hacky. Maybe finding a way of returning a value, e.g. via a callback or promise, would make it just more clean IMHO

lamuertepeluda avatar Apr 23 '24 07:04 lamuertepeluda

If the run were allowed to return a value, the underlying service runner would do that same thing of process.stdout.write(result). It's not really hacky, it's just how return values in shell scripting works. I'll think about allowing it to return, but at the very least I could write something into the documentation about it

jmcdo29 avatar Apr 23 '24 15:04 jmcdo29

If the run were allowed to return a value, the underlying service runner would do that same thing of process.stdout.write(result). It's not really hacky, it's just how return values in shell scripting works. I'll think about allowing it to return, but at the very least I could write something into the documentation about it

Yes, I think it's a good idea to update the docs. I also noticed that if you use a logger, the process.stdout.write(result) will include the logger output (except if this is redirected somewhere else).

lamuertepeluda avatar Apr 23 '24 22:04 lamuertepeluda