Skip to content

Commit d688c89

Browse files
authored
update/scripts (#4)
update buildpack scripts (strict environment configuration) * update dependencies script + functions for handling subprojects dependencies * update compile script - code clean up * update bin scripts.. * update lib script.. * code consistency - environment script
1 parent e20a7e6 commit d688c89

File tree

5 files changed

+96
-61
lines changed

5 files changed

+96
-61
lines changed

bin/compile

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
#!/bin/bash
22
# bin/compile <build-dir> <cache-dir> <env-dir>
33

4-
set -e
4+
### Configure environment
5+
set -o errexit
6+
set -o pipefail
57

68
### Configure directories
79

8-
BUILD_DIR=${1:-}
9-
CACHE_DIR=${2:-}
10-
ENV_DIR=${3:-}
10+
readonly BUILD_DIR="${1:-}"
11+
readonly CACHE_DIR="${2:-}"
12+
readonly ENV_DIR="${3:-}"
1113

12-
BP_DIR=$(cd $(dirname ${0:-}); cd ..; pwd)
14+
readonly BP_DIR=$(cd "$(dirname "${0:-}")"; cd ..; pwd)
1315

1416
### Load dependencies
1517

16-
source $BP_DIR/lib/output.sh
17-
source $BP_DIR/lib/environment.sh
18-
source $BP_DIR/lib/dependencies.sh
18+
# shellcheck source=$BP_DIR/lib/output.sh
19+
source "$BP_DIR"/lib/output.sh
20+
# shellcheck source=$BP_DIR/lib/environment.sh
21+
source "$BP_DIR"/lib/environment.sh
22+
# shellcheck source=$BP_DIR/lib/dependencies.sh
23+
source "$BP_DIR"/lib/dependencies.sh
1924

2025
### Runtime environment
2126

@@ -25,31 +30,33 @@ export_env_dir "$ENV_DIR" | indent
2530

2631
### Check initial state
2732

28-
[ -f "$BUILD_DIR/yarn.lock" ] && YARN=true || YARN=false
33+
[[ -d "$BUILD_DIR/node_modules" ]] && PREBUILD=true || PREBUILD=false
34+
[[ -f "$BUILD_DIR/yarn.lock" ]] && YARN=true || YARN=false
2935

3036
### Set up development
3137

32-
info "Set up development"
38+
header "Set up development"
3339

34-
_NPM_CONFIG_PRODUCTION_=${NPM_CONFIG_PRODUCTION:-true}
35-
_NODE_ENV_=${NODE_ENV:-'production'}
40+
_NPM_CONFIG_PRODUCTION_="${NPM_CONFIG_PRODUCTION:-true}"
41+
_NODE_ENV_="${NODE_ENV:-production}"
42+
43+
info "previous config:"
44+
info "NPM_CONFIG_PRODUCTION = ${_NPM_CONFIG_PRODUCTION_}"
45+
info "NODE_ENV = ${_NODE_ENV_}"
3646

3747
export NPM_CONFIG_PRODUCTION=false
3848
export NODE_ENV='development'
3949

4050
### Restore /node_modules/
4151

42-
restore_dependencies() {
43-
[ -d "$BUILD_DIR/node_modules" ] && mv "$BUILD_DIR/node_modules" "$BUILD_DIR/.node_modules"
52+
function restore_dependencies() {
53+
header "restore dependencies..."
54+
55+
# save root project dependencies
56+
$PREBUILD && mv "$BUILD_DIR/node_modules" "$BUILD_DIR/.node_modules"
4457

45-
# check subdirectories for existing sub-projects
46-
for dir in "$BUILD_DIR/"*; do
47-
if [[ -d "$dir" && -f "$dir/package.json" ]]; then
48-
if [[ -d "$dir/node_modules" ]]; then
49-
mv "$dir/node_modules" "$dir/.node_modules"
50-
fi
51-
fi
52-
done
58+
# check subdirectories (first level) -> save dependencies copy for subprojects
59+
save_subprojects_dependencies "$BUILD_DIR"
5360

5461
# install dependencies...
5562
if $YARN; then
@@ -58,48 +65,40 @@ restore_dependencies() {
5865
npm_node_modules "$BUILD_DIR" | indent
5966
fi
6067

61-
# check for sub-projects...
62-
for dir in "$BUILD_DIR/"*; do
63-
if [[ -d "$dir" && -f "$dir/package.json" ]]; then
64-
if [[ ! -d "$dir/node_modules" ]]; then
65-
mv "$dir/.node_modules" "$dir/node_modules"
66-
fi
67-
fi
68-
done
68+
# check for dependencies in subprojects...
69+
check_subproject_dependencies "$BUILD_DIR"
6970
}
7071

71-
header "Restore dependencies..."
7272
restore_dependencies
7373

7474
### Build-step
7575

76-
build_step() {
76+
function build_step() {
77+
header "build step..."
78+
7779
cd "$BUILD_DIR"
7880

79-
info "set NODE_ENV=Production"
80-
export NODE_ENV='production'
81+
info "set NODE_ENV=production"
82+
export NODE_ENV=production
8183

8284
# execute build step
8385
info "npm run build"
8486
npm run build
8587

8688
rm -r "$BUILD_DIR/node_modules" 2>/dev/null || true
87-
[ -d "$BUILD_DIR/.node_modules" ] && mv "$BUILD_DIR/.node_modules" "$BUILD_DIR/node_modules"
88-
89-
for dir in "$BUILD_DIR/"*; do
90-
if [[ -d "$dir" && -d "$dir/.node_modules" ]]; then
91-
rm -r "$dir/node_modules"
92-
mv "$dir/.node_modules" "$dir/node_modules"
93-
fi
94-
done
89+
[[ -d "$BUILD_DIR/.node_modules" ]] && mv "$BUILD_DIR/.node_modules" "$BUILD_DIR/node_modules"
90+
91+
# restore subprojects dependencies to initial state
92+
restore_subprojects "$BUILD_DIR"
9593
}
9694

97-
header "Execute build step..."
9895
build_step
9996

10097
### Restore environmet
10198

10299
info "Restore environment"
103100

104-
export NPM_CONFIG_PRODUCTION=$_NPM_CONFIG_PRODUCTION_
105-
export NODE_ENV=$_NODE_ENV_
101+
export NPM_CONFIG_PRODUCTION="$_NPM_CONFIG_PRODUCTION_"
102+
export NODE_ENV="$_NODE_ENV_"
103+
104+
header "build succeeded!"

bin/detect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
# bin/detect <build-dir>
33

4-
if [[ $(cat "$1/package.json" | grep "\"build\"") ]]; then
4+
if [[ $(cat "$1/package.json" | grep '"build"') ]]; then
55
echo "heroku-bp-node-build"
66
exit 0
77
else

lib/dependencies.sh

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
11
#!/bin/bash
22

3-
yarn_node_modules() {
4-
local build_dir=${1:-}
5-
echo "install dependencies with yarn"
3+
function yarn_node_modules() {
4+
local build_dir="${1:-}"
5+
echo "yarn install"
66
cd "$build_dir"
77
yarn install --pure-lockfile --ignore-engines 2>&1
88
}
99

10-
npm_node_modules() {
11-
local build_dir=${1:-}
10+
function npm_node_modules() {
11+
local build_dir="${1:-}"
1212
if [ -e "$build_dir"/package.json ]; then
13-
echo "install dependencies with npm"
13+
echo "npm install"
1414
cd "$build_dir"
1515
npm install --unsafe-perm --userconfig "$build_dir"/.npmrc 2>&1
1616
else
1717
echo "Skipping (no package.json)"
1818
fi
1919
}
20+
21+
function save_subprojects_dependencies() {
22+
local build_dir="${1:-}"
23+
24+
for file in "$build_dir/"*; do
25+
if [[ -d "$file" && -f "$file/package.json" ]]; then
26+
if [[ -d "$file/node_modules" ]]; then
27+
mv "$file/node_modules" "$file/.node_modules"
28+
fi
29+
fi
30+
done
31+
}
32+
33+
function check_subproject_dependencies() {
34+
local build_dir="${1:-}"
35+
36+
for file in "$build_dir/"*; do
37+
if [[ -d "$file" && -d "$file/.node_modules" ]]; then
38+
if [[ ! -d "$file/node_modules" ]]; then
39+
mv "$file/.node_modules" "$file/node_modules"
40+
fi
41+
fi
42+
done
43+
}
44+
45+
function restore_subprojects() {
46+
local build_dir="${1:-}"
47+
48+
for file in "$build_dir/"*; do
49+
if [[ -d "$file" && -d "$file/.node_modules" ]]; then
50+
rm -r "$file/node_modules" 2>/dev/null || true
51+
mv "$file/.node_modules" "$file/node_modules"
52+
fi
53+
done
54+
}

lib/environment.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/bin/bash
22

3-
export_env_dir() {
4-
local env_dir=$1
5-
local whitelist_regex=${2:-''}
6-
local blacklist_regex=${3:-'^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$'}
3+
function export_env_dir() {
4+
local env_dir="$1"
5+
local whitelist_regex="${2:-''}"
6+
local blacklist_regex="${3:-'^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH|LANG)$'}"
7+
78
if [[ -d "$env_dir" ]]; then
8-
for e in $(ls $env_dir); do
9+
for e in $(ls "$env_dir"); do
910
echo "$e=$(cat $env_dir/$e)"
1011
echo "$e" | grep -E "$whitelist_regex" | grep -qvE "$blacklist_regex" && export "$e=$(cat $env_dir/$e)"
1112
:

lib/output.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/bin/bash
22

3-
info() {
3+
function info() {
44
echo " $*" || true
55
}
66

7-
header() {
7+
function header() {
88
echo "" || true
99
echo "-----> $*" || true
1010
}
1111

12-
indent() {
12+
function indent() {
1313
sed -u 's/^/ /'
1414
}
1515

16-
print() {
16+
function print() {
1717
echo "$*" | indent
1818
}

0 commit comments

Comments
 (0)