Skip to content

Instantly share code, notes, and snippets.

@ngsctt
Last active February 5, 2020 10:22
Show Gist options
  • Save ngsctt/db05499ff634ea0f4ef5006492426b03 to your computer and use it in GitHub Desktop.
Save ngsctt/db05499ff634ea0f4ef5006492426b03 to your computer and use it in GitHub Desktop.
A simple shell script to convert existing NPM-installed modules to PNPM modules

What

A simple shell script to convert existing NPM-installed modules to PNPM modules

Why

NPM results in a lot of modules being repeatedly installed on the machine, whereas PNPM caches them locally, saving space for pointless duplicates

How

source convert_to_pnpm.sh
convert_to_pnpm $PATH_TO_ROOT_FOLDER

convert_to_pnpm will use find to iterate over all the subfolders under $PATH_TO_ROOT_FOLDER, finding any node_modules folders without a pnpm-lock.yaml, and then it will:

  1. Delete the node_modules folder, and
  2. Run pnpm install in the parent folder.

If the parent folder contains node_modules but doesn't have a package.json, convert_to_pnpm will skip that folder and print a warning.

Licence

(C) Nathan Scott 2020, released under the standard terms of the MIT Licence

convert_to_pnpm () (
export RED_BG="\e[48;5;9m"
export YELLOW_BG="\e[48;5;11m"
export BLUE_BG="\e[48;5;14m"
export BLACK_FG="\e[38;5;0m"
export NORMAL="\e[0m"
find $1 -type d -name "node_modules" -prune \
-execdir [ ! -f "pnpm-lock.yaml" ] \; \
-execdir $SHELL -c '() {[ -f "package.json" ] && return 0 || echo -e "${YELLOW_BG}${BLACK_FG} WARN ${NORMAL} skipping `pwd`, no package.json" && return 1}' \; \
-execdir $SHELL -c 'echo -e "${BLUE_BG}${BLACK_FG} INFO ${NORMAL} converting `pwd`..."' \; \
-execdir rm -rf {} \; \
-execdir pnpm install \;
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment