Skip to content

Instantly share code, notes, and snippets.

@kapetan
Created January 19, 2015 15:23
Show Gist options
  • Save kapetan/6083e01b8baec5cb9d2a to your computer and use it in GitHub Desktop.
Save kapetan/6083e01b8baec5cb9d2a to your computer and use it in GitHub Desktop.
npm cache
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