jorgebucaran / nvm.fish

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

reinstall-packages-from? #134

Closed deepfriedmind closed 3 years ago

deepfriedmind commented 3 years ago

When updating to the latest node in nvm, I'm used to doing nvm install node --reinstall-packages-from=node. Is there any way to achieve the same result in nvm.fish?

jorgebucaran commented 3 years ago

No, there's no way to do that out of the box. But what does that flag do exactly?

deepfriedmind commented 3 years ago

It migrates global packages from the current install to the new one.

See: https://github.com/nvm-sh/nvm#migrating-global-packages-while-installing

jorgebucaran commented 3 years ago

This will do the trick.

set --local pkgs (string split / --fields=10 -- $nvm_data/$nvm_current_version/lib/node_modules/*)
nvm install latest && npm install --global $pkgs

I'm not sure I want to add a flag just to do that. What do you think?

ljharb commented 3 years ago

That won't handle linked packages, which nvm does handle, nor will it preserve version numbers (i think).

deepfriedmind commented 3 years ago

This will do the trick.

set --local pkgs (string split / --fields=10 -- $nvm_data/$nvm_current_version/lib/node_modules/*)
nvm install latest && npm install --global $pkgs

I'm not sure I want to add a flag just to do that. What do you think?

string split doesn't have a fields option so the command fails.

I'm currently doing something similar however:

set -l npmPackages (npm list -g --parseable --depth=0 | sed '1d' | awk '{gsub(/\/.*\//,"",$1); print}')
nvm install latest && npm install -g $npmPackages

Unfortunately, this workaround will re-install all packages even if no new node version was installed.

jorgebucaran commented 3 years ago

My bad, --fields is only available in Fish 3.2.

For 3.0 or 3.1 we can use string match instead:

set --local pkgs (string match --regex -- '[^/]+$' $nvm_data/$nvm_current_version/lib/node_modules/*)
nvm install latest && npm install --global $pkgs

Preserving version numbers is slower since we need to use npm:

set --local pkgs (npm list --global --depth=0 | string match --regex -- "\w+@[^\s]+\$")
nvm install latest && npm install --global $pkgs

Handling linked packages is much more involved:

  1. Copy $nvm_current_version to local variable
  2. Grab the path from foobar@1.2.1 -> /home/jb/foobar
  3. nvm install VERSION
  4. Copy symlink to the current lib/node_modules
deepfriedmind commented 3 years ago
set --local pkgs (npm list --global --depth=0 | string match --regex -- "\w+@[^\s]+\$")

That regex breaks on -. "[\w,-]+@[^\s]+\$" works.

Anyhoo, it would be nice to have the same functionality as reinstall-packages-from but I'm fine with working around it. Thanks for your time.

ujwal-setlur commented 3 years ago

any updates on this? This would be really useful.

jorgebucaran commented 3 years ago

Sorry, I won't be working on this so I'm closing. See this comment for a workaround.

Feel free to send me a PR if you have a working solution, but I can't promise that too will get merged. We'll see.