1
1
#! /bin/bash
2
2
# bin/compile <build-dir> <cache-dir> <env-dir>
3
3
4
- set -e
4
+ # ## Configure environment
5
+ set -o errexit
6
+ set -o pipefail
5
7
6
8
# ## Configure directories
7
9
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:- } "
11
13
12
- BP_DIR=$( cd $( dirname ${0:- } ) ; cd ..; pwd)
14
+ readonly BP_DIR=$( cd " $( dirname " ${0:- } " ) " ; cd ..; pwd)
13
15
14
16
# ## Load dependencies
15
17
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
19
24
20
25
# ## Runtime environment
21
26
@@ -25,31 +30,33 @@ export_env_dir "$ENV_DIR" | indent
25
30
26
31
# ## Check initial state
27
32
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
29
35
30
36
# ## Set up development
31
37
32
- info " Set up development"
38
+ header " Set up development"
33
39
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_} "
36
46
37
47
export NPM_CONFIG_PRODUCTION=false
38
48
export NODE_ENV=' development'
39
49
40
50
# ## Restore /node_modules/
41
51
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"
44
57
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 "
53
60
54
61
# install dependencies...
55
62
if $YARN ; then
@@ -58,48 +65,40 @@ restore_dependencies() {
58
65
npm_node_modules " $BUILD_DIR " | indent
59
66
fi
60
67
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 "
69
70
}
70
71
71
- header " Restore dependencies..."
72
72
restore_dependencies
73
73
74
74
# ## Build-step
75
75
76
- build_step () {
76
+ function build_step() {
77
+ header " build step..."
78
+
77
79
cd " $BUILD_DIR "
78
80
79
- info " set NODE_ENV=Production "
80
- export NODE_ENV=' production'
81
+ info " set NODE_ENV=production "
82
+ export NODE_ENV=production
81
83
82
84
# execute build step
83
85
info " npm run build"
84
86
npm run build
85
87
86
88
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 "
95
93
}
96
94
97
- header " Execute build step..."
98
95
build_step
99
96
100
97
# ## Restore environmet
101
98
102
99
info " Restore environment"
103
100
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!"
0 commit comments