Skip to content

Instantly share code, notes, and snippets.

@sangdth
Last active September 28, 2022 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangdth/54de24d3303fa748e4932a3f0f61b774 to your computer and use it in GitHub Desktop.
Save sangdth/54de24d3303fa748e4932a3f0f61b774 to your computer and use it in GitHub Desktop.
Auto install current global npm packages after fnm install

The fnm still does not support -reinstall-packages-from like nvm, so I wrote this script for me while waiting.

#!/bin/sh

# Remember to alias fnm='fnm2' for convinient

# TODO: How to make the (which fmn) work here instead of hard coded path?
/opt/homebrew/bin/fnm $*

FNM_STATUS_CODE=$?

if [[ "$1" == "install" && "$FNM_STATUS_CODE" == "0" ]]; then
  # TODO: Check for the --reinstall-global from $* then check for content in default-packages
  # CAREFUL: on Mac OS, the paths with --parseable has space in "Application Support",
  # thus make the `awk print` with 3, it might be different from other OS.
  CURRENT_GLOBAL_PKGS=$(npm ls -g --parseable | sed 's/node_modules\// /g' | awk '{print $3}' | xargs)

  # Use this one to install global packages from default-packages list
  # CURRENT_GLOBAL_PKGS=$(cat ~/dotfiles/default-packages | xargs)

  /opt/homebrew/bin/fnm use $2

  echo "Updating npm"
  npm i -g npm

  echo "Installing default global packages"
  npm i -g $CURRENT_GLOBAL_PKGS

  # Bonus: Enable corepack
  # echo "Enable corepack"
  # corepack enable
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment