Skip to content

Instantly share code, notes, and snippets.

@retrohacker
Last active August 21, 2019 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retrohacker/02d358938b965d57daf4baddb7130570 to your computer and use it in GitHub Desktop.
Save retrohacker/02d358938b965d57daf4baddb7130570 to your computer and use it in GitHub Desktop.
Using patch files for storing the npm registry
#!/bin/bash
# Remove anything that may have been left over by a previous run
rm -rf patch orig new
mkdir patch
for TARBALL in `ls - | node semver-sort.js`
do
tar -xzf "./-/${TARBALL}"
# If this is the first tarball we are looking at, we have
# nothing to diff against
if [ ! -e orig ]
then
cp -r package "patch/${TARBALL%.tgz}"
mv package orig
continue
fi
# Generate a diff
mv package new
diff -ruN orig new > "patch/${TARBALL}.patch"
rm -rf orig
mv new orig
grep -irE '^Binary files .* differ$' "patch/${TARBALL}.patch" > /dev/null
if [ "$?" -eq "0" ]
then
echo "patch/${TARBALL}.patch contains binary files, not able to compress!"
cp -r orig "patch/${TARBALL%.tgz}"
rm "patch/${TARBALL}.patch"
continue
fi
# If the patch file is larger than the uncompressed package, just
# use the package!
TSIZE=`du -s ./orig | cut -f 1`
PSIZE=`du -s ./patch/${TARBALL}.patch | cut -f 1`
if [ "${PSIZE}" -ge "${TSIZE}" ]
then
cp -r orig "patch/${TARBALL%.tgz}"
echo `du -hs ./patch/${TARBALL%.tgz}`
rm "patch/${TARBALL}.patch"
else
echo `du -hs ./patch/${TARBALL}.patch`
fi
done
echo "Generating final patch tarball"
# Compress the entire patch directory as one big file
tar -c ./patch | gzip > patch.tar.gz
# Remove all state
rm -rf orig patch package
# Print out the space savings
du -hs '-' 'patch.tar.gz'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment