Last active
August 21, 2019 00:54
-
-
Save retrohacker/02d358938b965d57daf4baddb7130570 to your computer and use it in GitHub Desktop.
Using patch files for storing the npm registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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