jorgebucaran / nvm.fish

The Node.js version manager you'll adore, crafted just for Fish
https://git.io/nvm.fish
MIT License
2.03k stars 68 forks source link

Add nvm exec and nvm run commands #207

Open asportnoy opened 1 year ago

asportnoy commented 1 year ago

Like the official nvm command, add a nvm exec subcommand which allows you to execute something one-off with a different version. For example, nvm exec 16 node index.js. There's also a nvm run command which works like exec except you don't need to specify node (exec can also be used with npm, npx, etc)

jorgebucaran commented 1 year ago

Can you tell me more about why you're using nvm exec instead of wrapping nvm use and running your command inside a Fish function? I'm not super familiar with nvm exec.

asportnoy commented 1 year ago

I have a global custom command which requires node 18, but I have local projects that require node 16, so I need nvm exec to make sure I can always run that custom command. I ended up making my own function for it (https://gist.github.com/asportnoy/d3834347aee5e81390b9671305a4536c) but it should just be implemented into this package. Feel free to use my code if it helps.

jorgebucaran commented 1 year ago

So, this just switches to the specified version, runs the command, and switches back. Unless the version isn't installed. Have you run into any issues with your function?

thernstig commented 1 year ago

If these commands were added, what would be the reason for both? I am all for the minimalistic approach with having as few commands as possible.

@asportnoy is there a reason you have not put that command (.js file) in your PATH and then added a Shebang?

#!/home/userA/.local/share/nvm/v18.16.0/bin/node

// js content here

Then just execute it like somecommand since it exists on your path. To me, that would be much simpler than having to cd to the directory and then run the much longer nvm exec 18 node index.js. Or if you still want to cd, then use a .nvmrc file.

jorgebucaran commented 1 year ago

Thank you for clarifying, @thernstig. I actually didn't realize we were discussing two commands! It seems that their similar names had me confused.

asportnoy commented 1 year ago

So, this just switches to the specified version, runs the command, and switches back. Unless the version isn't installed. Have you run into any issues with your function?

@jorgebucaran kinda. It doesn't actually switch, it just gets the path to the executable with the correct node version and runs it. You can look at the code above to see how I implemented it.

asportnoy commented 1 year ago

If these commands were added, what would be the reason for both? I am all for the minimalistic approach with having as few commands as possible.

@asportnoy is there a reason you have not put that command (.js file) in your PATH and then added a Shebang?


#!/home/userA/.local/share/nvm/v18.16.0/bin/node

// js content here

Then just execute it like somecommand since it exists on your path. To me, that would be much simpler than having to cd to the directory and then run the much longer nvm exec 18 node index.js. Or if you still want to cd, then use a .nvmrc file.

That is an option for this specific scenario not it's not as clean IMO. Plus, you can't use it for npm or npx, which you can with nvm exec

jorgebucaran commented 1 year ago

Implementing either nvm exec or nvm run would do, correct?

asportnoy commented 1 year ago

If you were to only add one, it should definitely be nvm exec. nvm run is basically just a shortcut to calling nvm exec node $argv, whereas exec can also do npm and npx.

jorgebucaran commented 1 year ago

Absolutely! Send me PR and we can polish it together?