Skip to content

Instantly share code, notes, and snippets.

@sigriston
Created October 4, 2017 12:47
Show Gist options
  • Save sigriston/717c1229dc2247641d9ffe730a04c09d to your computer and use it in GitHub Desktop.
Save sigriston/717c1229dc2247641d9ffe730a04c09d to your computer and use it in GitHub Desktop.
npm-save-modules
#!/bin/bash
if [[ -z "$1" ]]; then
echo ERROR: no arguments passed!
echo Usage: "$0 [save | restore]"
exit 1
fi
if [[ ! -f ./package.json ]]; then
echo ERROR: package.json not found!
exit 2
fi
SHAREDIR="${HOME}/.local/share/npm-save-modules"
mkdir -p "$SHAREDIR"
SHASUM="$(shasum -a 256 ./package.json | egrep -o '^[0-9a-f]+')"
DESTDIR="${SHAREDIR}/${SHASUM}"
if [[ "$1" = "save" ]]; then
if [[ ! -d ./node_modules ]]; then
echo ERROR: node_modules not found!
exit 3
fi
rsync -a node_modules "$DESTDIR"
echo DONE!
elif [[ "$1" = "restore" ]]; then
if [[ -d ./node_modules ]]; then
echo ERROR: node_modules already exists!
exit 4
fi
if [[ ! -d "$DESTDIR" ]]; then
echo ERROR: saved modules not found for shasum: "$SHASUM"
exit 5
fi
rsync -a "$DESTDIR/" .
echo DONE!
else
echo ERROR: option "$1" invalid!
echo Usage: "$0 [save | restore]"
exit 6
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment