Created
January 19, 2015 15:23
-
-
Save kapetan/6083e01b8baec5cb9d2a to your computer and use it in GitHub Desktop.
npm cache
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
function get_cache_dir() { | |
local cache_id=$(cat package.json | grep '"name"' | sed 's/"name"//; s/[^"]*"//; s/"[^"]*//') | |
if [ "$cache_id" == "" ]; then | |
cache_id=__misc__ | |
fi | |
echo "/tmp/npm-cache/$cache_id" | |
} | |
function get_hash() { | |
echo $(shasum package.json | shasum | sed 's/[ \t]*-[ \t]*//') | |
} | |
function read_hash() { | |
echo $(cat "$1/hash" 2> /dev/null) | |
} | |
function build() { | |
local cache_dir=$(get_cache_dir) | |
mkdir -p "$cache_dir" | |
local new_hash=$(get_hash) | |
local old_hash=$(read_hash "$cache_dir") | |
if [ "$new_hash" == "$old_hash" ]; then | |
cp -r "$cache_dir/" node_modules | |
else | |
npm install || exit $? | |
rm -rf "$cache_dir" | |
cp -r "node_modules/" "$cache_dir" | |
echo "$new_hash" > "$cache_dir/hash" | |
fi | |
} | |
build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment