Skip to content

Instantly share code, notes, and snippets.

@ankjevel
Last active April 18, 2024 08:31
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ankjevel/00360558b1bd92c6892444e5400309a6 to your computer and use it in GitHub Desktop.
Save ankjevel/00360558b1bd92c6892444e5400309a6 to your computer and use it in GitHub Desktop.
clear npm cache (when using nvm); run with `nvm list | clear-cache`
#!/usr/bin/env bash
function clear-cache {
versions=()
while read -r line; do
clean="$(echo ${line} | sed "s,$(printf '\033')\\[[0-9;]*[a-zA-Z],,g")"
valid=$(echo "${clean}" | grep '^[^a-zA-Z]' | grep -v 'system' | sed 's/->//' | sed 's/\s.*v//')
if [[ ! -z $valid ]]; then
versions+=(${valid})
fi
done <<< "$(cat)"
for version in "${versions[@]}"; do
nvm use ${version}
npm cache clear -f
done
}
# run with:
# nvm list | clear-cache
@ankjevel
Copy link
Author

to run:

# make executable
chmod +x ./clear-cache.sh
# load function
. ./clear-cache.sh
# run function
nvm list | clear-cache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment