Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find package files stored in yarn offline mirror that are no longer referenced/resolved in yarn.lock file
# There is no way right now to find packages included in an offline mirror folder that are
# not actually referenced by the current yarn.lock file. This script will compare the files
# in your offline mirror folder and find any that are not in the current set resolved by yarn.
# The closest is https://yarnpkg.com/en/docs/prune-offline-mirror but this only effects
# specific future activities, like calling 'yarn upgrade'.
# example usage: cd some_repo; clean_yarn_mirror.sh ./npm_offline_cache
echo "Scanning files in $1..."
tgz=($(grep -ho -e 'resolved .*\.tgz' yarn.lock | cut -d" " -f2))
git=($(grep -ho -e 'resolved .*\.git-.*#' yarn.lock | cut -d" " -f2 | sed 's/#//'))
files=$(ls $1)
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
for f in $files; do
containsElement "$f" "${tgz[@]}"
istgz=$?
containsElement "$f" "${git[@]}"
isgit=$?
if [ $istgz -eq 0 ]
then
: # echo "$f is listed as a tgz"
elif [ $isgit -eq 0 ]
then
: # echo "$f is listed as a git"
else
echo "MATCH: $f is not mentioned in yarn.lock file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment