Created
October 17, 2018 12:35
-
-
Save mistadikay/3703a7168f921484c1f63f54de6ed224 to your computer and use it in GitHub Desktop.
Speedup node modules installation if cache exists on s3
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
#!/usr/bin/env bash | |
NPM_TARBALL=node_modules.tgz | |
NPM_MD5SUM_FILE=package-lock.md5sum | |
NPM_CACHE_DIR=${HOME}/.cache/YOUR_PROJECT/npmtarball | |
S3_DIR=s3://YOUR_BUCKET/YOUR_PROJECT | |
function shouldInstallFromCache() { | |
printf "\nChecking the cache\n" | |
[[ ! -e $NPM_CACHE_DIR ]] && mkdir -p $NPM_CACHE_DIR | |
cd $NPM_CACHE_DIR | |
if aws s3 cp ${S3_DIR}/${NPM_MD5SUM_FILE} ./${NPM_MD5SUM_FILE}; then | |
printf "Downloaded cached package-lock.json\n" | |
else | |
touch ${NPM_MD5SUM_FILE} | |
printf "No package-lock.json cached yet\n" | |
fi | |
cd - | |
md5 package-lock.json >${NPM_MD5SUM_FILE} | |
if diff ${NPM_CACHE_DIR}/${NPM_MD5SUM_FILE} ${NPM_MD5SUM_FILE}; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function installFromCache() { | |
aws s3 cp ${S3_DIR}/${NPM_TARBALL} ${NPM_CACHE_DIR}/${NPM_TARBALL} | |
tar xzf ${NPM_CACHE_DIR}/${NPM_TARBALL} | |
} | |
function updateCache() { | |
tar -zcf ${NPM_TARBALL} node_modules | |
mv ${NPM_TARBALL} ${NPM_CACHE_DIR} | |
mv ${NPM_MD5SUM_FILE} ${NPM_CACHE_DIR} | |
cd $NPM_CACHE_DIR | |
aws s3 cp ./$NPM_TARBALL $S3_DIR/$NPM_TARBALL | |
aws s3 cp ./$NPM_MD5SUM_FILE $S3_DIR/$NPM_MD5SUM_FILE | |
cd - | |
} | |
function md5() { | |
md5sum $1 | cut -d\ -f 1 | |
} | |
function installModules() { | |
if shouldInstallFromCache; then | |
printf "\nInstalling from cache\n" | |
installFromCache | |
else | |
printf "\nInstalling from scratch\n" | |
npm install | |
printf "\nUpdating the cache\n" | |
updateCache | |
fi | |
} | |
installModules |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment