-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild-nodejs.sh
executable file
·55 lines (43 loc) · 1.24 KB
/
build-nodejs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
# Usage: ./build.sh <directory-to-package> <ZIP-file-to-create>
#
# Defaults:
# src/ -> build.zip
set -o errexit
set -o pipefail
set -o nounset
#set -o xtrace
SRC_DIR="${1:-src}"; SRC_DIR="${SRC_DIR%%/}"
OUT_FILE="${2:-build.zip}"
HASH_FILE="${3:-}"
make_absolute() {
if [ -z "${1}" ]; then
return
fi
case "${1}" in
/*) echo "${1}";; # already absolute path
*) echo "${PWD}/${1}";;
esac
}
OUT_FILE="$( make_absolute "${OUT_FILE}" )"
HASH_FILE="$( make_absolute "${HASH_FILE}" )"
# make sure the file is empty. Zip will *add* if the file exists
rm -f "${OUT_FILE}"
BUILD_DIR=`mktemp -d 2>/dev/null || mktemp -d -t 'build'` # Linux & BSD-compatible
cp -a "${SRC_DIR}/" "${BUILD_DIR}"
(
cd "${BUILD_DIR}"
rm -rf node_modules || true
npm install --production
# Optionally clean up caches
if [ -n "${HASH_FILE}" ]; then
git init -q
echo "package.json" > .gitignore # npm stores absolute paths in there
git add --all
git commit --no-gpg-sign -qm 'whatever'
git cat-file -p HEAD | grep '^tree' | awk '{print $2}' > "${HASH_FILE}"
echo "Content hash: $(< "${HASH_FILE}" )"
rm -rf .git
fi
zip "${OUT_FILE}" -r .
)