jorgebucaran / nvm.fish

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

Question: Make `nvm use` usable for my own auto-switching? #186

Closed lydell closed 2 years ago

lydell commented 2 years ago

Hi!

First, thanks for nvm.fish! It’s so fast and easy to use.

Note: I made this as a PR rather than as an issue because it’s easier to show some things using code. I don’t expect this to be merged in its current (ugly, incomplete) form!

I read https://github.com/jorgebucaran/nvm.fish/issues/132 about the – now closed – suggestion about automatic version switching.

I get that it can be problematic. But I feel that for me, it can be more worth having automatic switching than perfection. So I started playing around with doing automatic switching myself:

# Note: Not completely thought through code yet!

# Run at startup.
nvm use

# Run when the current directory changes.
function foo --on-variable PWD
  nvm use
end

However, there are a few problems with nvm use (with no arguments):

I threw together some quick and bad modifications to nvm.fish to try things out. It looks promising!

What do you think? Would you be interested in having an nvm use optimized for this use case?

Alternatively, any tips on how to use nvm effectively? Currently, I “fight” a lot with forgetting to switch version all the time. How do you do it? Would you recommend some other tool if this isn’t ”the nvm.fish philosophy”? (I’ve tried nodenv in the past, which has automatic switching, but it was so clunky to use.)

lydell commented 2 years ago

Forgot to search pull requests in addition to issues 🤦 Found this one right now: https://github.com/jorgebucaran/nvm.fish/pull/181

jorgebucaran commented 2 years ago

It is too slow... It prints stuff...

👌. We either need a plumbing nvm use command or support this out of the box via a feature flag. Since there has been sustained support for this feature over the years, I'll look into taking a stab at this myself. I'll pull @edouard-lopez's #181 changes first then add my tweaks.

It fails if no .nvmrc or .node-version is found. I want it to switch to the default version in that case – which is system for me.

Just curious, but why isn't your system Node always active?

lydell commented 2 years ago

Since there has been sustained support for this feature over the years, I'll look into taking a stab at this myself

Yay! I’m excited 😄 🎉

Just curious, but why isn't your system Node always active?

I usually have the latest LTS installed via brew, and that’s the one I use generally. But then I have some older projects that need a specific older Node.js version (for the time being). In those I like to stick a .node-version file. When entering one of those, I’d like to automatically switch to that Node.js version. When leaving the project folder (cd away from it), I’d like to go back to the system Node.js that I usually use again, rather than stay on that older version just because I happened to be in that project folder before. Some things I thought about now:

jorgebucaran commented 2 years ago

How about we introduce a --silent flag that does the job silently, printing nothing. Then consider an official auto-switching feature on a different discussion? 🤓

lydell commented 2 years ago

nvm use --silent + me creating ~/.node-version sounds like it should be all I need to implement automatic switching on my own, yes!

Edit: Oh wait, I’ll probably get errors when I (seldomly) go to, say, /usr. Are the errors silenced too? Or should I create /.node-version maybe? 🤔

jorgebucaran commented 2 years ago

Yes, but you can redirect to /dev/null in this case since errors won't slow you down as _nvm_node_info does.

jorgebucaran commented 2 years ago

Added a --silent option flag that will suppress stdout (it works for nvm install and nvm uninstall too).

Please let me know if this should be all you need to implement automatic switching.

lydell commented 2 years ago

I added this to ~/.config/fish/config.fish:

function __nvm_auto --on-variable PWD
  nvm use --silent 2>/dev/null
end
__nvm_auto

And created ~/.node-version with my desired default version.

nvm use --silent seems to take like 9 ms.

It seems to work perfectly so far!* 🎉 Thank you!

jorgebucaran commented 2 years ago

9ms should do it. One could even get a bit fancier and put __nvm_auto behind a feature flag (https://github.com/jorgebucaran/nvm.fish/pull/181#discussion_r885909453). 🤓

denvis commented 2 years ago

super fast and reliable so far! thx

jorgebucaran commented 2 years ago

Question: we nvm use $nvm_default_version in new shells, but what if there is an .nvmrc file in that directory? Which one should have precedence: nvm_default_version or a local .nvmrc/.node-version file?

cc @lydell @denvis @edouard-lopez

lydell commented 2 years ago

a local .nvmrc/.node-version file should always win IMO. That’s how my current setup works. In other words, I want no difference between cd foo/ and starting out in foo/ from the start.

jorgebucaran commented 2 years ago

What happens when you move to a directory without a .nvmrc (after you've auto-switched versions)? Should you auto-switch to the previous one? Assume such a directory is not a subdirectory of the one that had the .nvmrc file.

lydell commented 2 years ago

Then I would like the default version. I’ve created ~/.node-version with my desired default to simulate that.

jorgebucaran commented 2 years ago

Thank you, that makes sense and it's easy to do in userland too.