-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathcompile_node
More file actions
executable file
·209 lines (161 loc) · 6.68 KB
/
Copy pathcompile_node
File metadata and controls
executable file
·209 lines (161 loc) · 6.68 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env bash
# USAGE: bin/compile_node <build-dir> <cache-dir> <env-dir>
####### Configure environment
set -e # fail fast
set -o pipefail # don't ignore exit codes when piping output
# set -x # enable debugging
# Configure directories
build_dir=$1
cache_dir=$2
env_dir=$3
app_source=$build_dir/app_src
bp_dir=$(cd $(dirname $0); cd ..; pwd)
heroku_dir=$build_dir/.heroku
mkdir -p $heroku_dir/node
# Load some convenience functions like status(), echo(), and indent()
source $bp_dir/bin/common.sh
# Avoid GIT_DIR leak from previous build steps
unset GIT_DIR
# Provide hook to deal with errors
trap build_failed ERR
# Load config vars into environment; start with defaults
export NPM_CONFIG_PRODUCTION=true
export NODE_MODULES_CACHE=true
export_env_dir $env_dir
####### Determine current state
# What's the requested semver range for node?
#node_engine=$(package_json ".engines.node")
# Node version is locked for now: newer ones don't work with Meteor.
node_engine="0.10.41"
node_previous=$(file_contents "$cache_dir/node/node-version")
# What's the requested semver range for npm?
npm_engine=$(package_json ".engines.npm")
npm_previous=$(file_contents "$cache_dir/node/npm-version")
# How does this app start?
if test -f $app_source/Procfile; then start_method="Procfile"
elif [[ $(package_json ".scripts.start") != "" ]]; then start_method="npm start"
elif [ -f $app_source/server.js ]; then start_method="server.js"
else start_method=""
fi
# What's the source-of-truth for node_modules?
if test -d $app_source/node_modules; then modules_source="prebuilt"
elif test -f $app_source/npm-shrinkwrap.json; then modules_source="npm-shrinkwrap.json"
elif test -f $app_source/package.json; then modules_source="package.json"
else modules_source=""
fi
# What does our cache look like?
test -d $cache_dir/node/node_modules && modules_cached=true || modules_cached=false
####### Provide debugging info and feedback
echo ""
info "Node engine: ${node_engine:-unspecified}"
info "Npm engine: ${npm_engine:-unspecified}"
info "Start mechanism: ${start_method:-none}"
info "node_modules source: ${modules_source:-none}"
info "node_modules cached: $modules_cached"
echo ""
printenv | grep ^NPM_CONFIG_ | indent
info "NODE_MODULES_CACHE=$NODE_MODULES_CACHE"
source $bp_dir/bin/warnings.sh
####### Vendor in binaries
head "Installing binaries"
# Resolve non-specific node versions using semver.herokuapp.com
if ! [[ "$node_engine" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
info "Resolving node version ${node_engine:-(latest stable)} via semver.io..."
node_engine=$(curl --silent --get --data-urlencode "range=${node_engine}" https://semver.herokuapp.com/node/resolve)
fi
# Download node from Heroku's S3 mirror of nodejs.org/dist
info "Downloading and installing node $node_engine..."
node_url="http://s3pository.heroku.com/node/v$node_engine/node-v$node_engine-linux-x64.tar.gz"
curl $node_url -s -o - | tar xzf - -C /tmp
# Move node (and npm) into .heroku/node and make them executable
mv /tmp/node-v$node_engine-linux-x64/* $heroku_dir/node
chmod +x $heroku_dir/node/bin/*
PATH=$heroku_dir/node/bin:$PATH
# Optionally bootstrap a different npm version
if [ "$npm_engine" != "" ]; then
if ! [[ "$npm_engine" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
info "Resolving npm version ${npm_engine} via semver.io..."
npm_engine=$(curl --silent --get --data-urlencode "range=${npm_engine}" https://semver.herokuapp.com/npm/resolve)
fi
info "Downloading and installing npm $npm_engine (replacing version `npm --version`)..."
npm install --quiet -g npm@$npm_engine > /dev/null 2>&1 | indent
fi
# Run subsequent commands from the build directory
cd $app_source
####### Build the project's dependencies
head "Building dependencies"
# Did we bust the cache?
if ! $modules_cached; then
use_cache=false
elif ! $NODE_MODULES_CACHE; then
info "Cache disabled with NODE_MODULES_CACHE"
use_cache=false
elif [ "$node_previous" != "" ] && [ "$node_engine" != "$node_previous" ]; then
info "Node version changed ($node_previous => $node_engine); invalidating cache"
use_cache=false
elif [ "$npm_previous" != "" ] && [ "$npm_engine" != "$npm_previous" ]; then
info "Npm version changed ($npm_previous => $npm_engine); invalidating cache"
use_cache=false
else
use_cache=true
fi
if [ "$modules_source" == "" ]; then
info "Skipping dependencies (no source for node_modules)"
elif [ $modules_source == "prebuilt" ]; then
info "Rebuilding any native modules for this architecture"
npm rebuild 2>&1 | indent
info "Installing any new modules"
npm install --quiet --userconfig $app_source/.npmrc 2>&1 | indent
elif $use_cache; then
info "Restoring node modules from cache"
cp -r $cache_dir/node/node_modules $app_source/
info "Pruning unused dependencies"
npm prune 2>&1 | indent
info "Installing any new modules"
npm install --quiet --userconfig $app_source/.npmrc 2>&1 | indent
else
info "Installing node modules"
touch $app_source/.npmrc
npm install --quiet --userconfig $app_source/.npmrc 2>&1 | indent
fi
####### Create a Procfile if possible
head "Checking startup method"
if [ "$start_method" == "Procfile" ]; then
info "Found Procfile"
elif [ "$start_method" == "npm start" ]; then
info "No Procfile; Adding 'web: npm start' to new Procfile"
echo "web: npm start" > $app_source/Procfile
elif [ "$start_method" == "server.js" ]; then
info "No Procfile; Adding 'web: node server.js' to new Procfile"
echo "web: node server.js" > $app_source/Procfile
fi
####### Create the runtime environment (profile.d)
head "Finalizing build"
# Runtime & Multi-buildpack exports
info "Creating runtime environment"
mkdir -p $build_dir/.profile.d
echo "export PATH=\"\$HOME/.heroku/node/bin:\$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" > $build_dir/.profile.d/nodejs.sh
echo "export NODE_HOME=\"\$HOME/.heroku/node\"" >> $build_dir/.profile.d/nodejs.sh
info "Exporting binary paths"
echo "export PATH=\"$heroku_dir/node/bin:$build_dir/node_modules/.bin:\$PATH\"" > $bp_dir/export
echo "export NODE_HOME=\"$heroku_dir/node\"" >> $bp_dir/export
####### Clean up
info "Cleaning up build artifacts"
# Clean up after npm
rm -rf "$build_dir/.node-gyp"
rm -rf "$build_dir/.npm"
# Clear the cache
rm -rf $cache_dir/node_modules # (for apps still on the older caching strategy)
rm -rf $cache_dir/node
####### Build successful! Store results in cache
# Create the cache
mkdir -p $cache_dir/node
echo $node_engine > $cache_dir/node/node-version
echo $npm_engine > $cache_dir/node/npm-version
if test -d $app_source/node_modules; then
info "Caching node_modules for future builds"
cp -r $app_source/node_modules $cache_dir/node
fi
# Show the final dependency tree
info "Build successful!"
(npm ls --depth=0 || true) 2>/dev/null | indent