11#! /bin/bash
22
33NODE_VERSION=$( < " ${SCRIPT_DIR} /../../../NODE_VERSION" )
4- NPM=" ${SCRIPT_DIR} /node/bin/npm"
54
65# Prints the arguments and their descriptions to the console
76function print_usage() {
@@ -35,7 +34,7 @@ function print_usage() {
3534 if [[ -d " ${SCRIPT_DIR} /../../dist/" ]]; then
3635 pushd " ${SCRIPT_DIR} /../.."
3736 echo " Server options:"
38- " ${NPM} " run start -- --help
37+ npm run start -- --help
3938 popd
4039 fi
4140 exit 1
@@ -76,40 +75,21 @@ function parse_args() {
7675}
7776
7877function check_version() { # current_version #min_version
79- # check if same string
80- if [ -z " $2 " ] || [ " $1 " = " $2 " ]; then
78+ local current=" $1 "
79+ local minimum=" $2 "
80+
81+ # Check if no minimum or both are the same
82+ if [ -z " $minimum " ] || [ " $current " = " $minimum " ]; then
8183 return 0
8284 fi
8385
84- local i current minimum
85-
86- IFS=" ." read -r -a current <<< $1
87- IFS=" ." read -r -a minimum <<< $2
88-
89- # fill empty fields in current with zeros
90- for (( i= ${# current[@]} ; i< ${# minimum[@]} ; i++ ))
91- do
92- current[i]=0
93- done
94-
95- for (( i= 0 ; i< ${# current[@]} ; i++ ))
96- do
97- if [[ -z ${minimum[i]} ]]; then
98- # fill empty fields in minimum with zeros
99- minimum[i]=0
100- fi
86+ local ordered=$( printf " %s\n%s\n" " $minimum " " $current " | sort -V | head -n1)
10187
102- if (( 10 #${current[i]} > 10 #${minimum[i]} )) ; then
103- return 1
104- fi
105-
106- if (( 10 #${current[i]} < 10 #${minimum[i]} )) ; then
107- return 2
108- fi
109- done
110-
111- # if got this far string is the same once we added missing 0
112- return 0
88+ if [ " $ordered " = " $minimum " ]; then
89+ return 1
90+ else
91+ return 2
92+ fi
11393}
11494
11595function check_and_install() { # dep_name #get_version_string #version_min #install_command
@@ -168,15 +148,9 @@ function setup_node() {
168148 # navigate to project root
169149 pushd " ${SCRIPT_DIR} /../../.." > /dev/null
170150
171- # setup the path so we're using our node. required when calling npm
172- PATH=" ${SCRIPT_DIR} /node/bin:$PATH "
173-
174- node_version=" "
175- if [[ -f " ${SCRIPT_DIR} /node/bin/node" ]]; then
176- node_version=$( " ${SCRIPT_DIR} /node/bin/node" --version)
177- fi
151+ local node_version=$( " node" --version)
178152
179- node_url=" "
153+ local node_url=" "
180154 if [ " $( uname) " == " Darwin" ]; then
181155 arch=$( uname -m)
182156 if [[ $arch == x86_64* ]]; then
@@ -190,16 +164,27 @@ function setup_node() {
190164 else
191165 node_url=" https://nodejs.org/dist/$NODE_VERSION /node-$NODE_VERSION -linux-x64.tar.gz"
192166 fi
193- check_and_install " ${SCRIPT_DIR} /node/bin/node" " $node_version " " $NODE_VERSION " " curl $node_url --output node.tar.xz
167+
168+ check_and_install " node" " $node_version " " $NODE_VERSION " " curl $node_url --output node.tar.xz
194169 && tar -xf node.tar.xz
195170 && rm node.tar.xz
196171 && mv node-v*-*-* \" ${SCRIPT_DIR} /node\" "
197172
198- if [ $? -eq 1 ] || [ " $INSTALL_DEPS " == " 1" ]; then
173+ if [ $? -eq 1 ]; then
174+ # installed node, point PATH to it
175+ PATH=" ${SCRIPT_DIR} /node/bin:$PATH "
176+ fi
177+
178+ # if node_modules doesnt exist or the package-lock file is newer than node_modules, install deps
179+ if [ ! -d node_modules ] || [ ../package-lock.json -nt node_modules ] || [ " $INSTALL_DEPS " == " 1" ]; then
199180 echo " Installing dependencies..."
200- " ${NPM} " install
181+ npm install
201182 fi
202183
184+ # log node version for audits
185+ echo " Using node version: $( node --version) "
186+ echo " Using NPM version: $( npm --version) "
187+
203188 popd > /dev/null
204189}
205190
@@ -209,14 +194,14 @@ function setup_libraries() {
209194 if [ ! -d " ${SCRIPT_DIR} /../../../Common/dist/" ] || [ " $BUILD_LIBRARIES " == " 1" ]; then
210195 pushd " ${SCRIPT_DIR} /../../../Common" > /dev/null
211196 echo " Building common library."
212- " ${NPM} " run build:cjs
197+ npm run build:cjs
213198 popd > /dev/null
214199 fi
215200
216201 if [ ! -d " ${SCRIPT_DIR} /../../../Signalling/dist/" ] || [ " $BUILD_LIBRARIES " == " 1" ]; then
217202 pushd " ${SCRIPT_DIR} /../../../Signalling" > /dev/null
218203 echo " Building signalling library."
219- " ${NPM} " run build:cjs
204+ npm run build:cjs
220205 popd > /dev/null
221206 fi
222207
@@ -242,13 +227,13 @@ function setup_frontend() {
242227 echo " Building Typescript Frontend."
243228 # Using our bundled NodeJS, build the web frontend files
244229 pushd " ${SCRIPT_DIR} /../../../Frontend/library" > /dev/null
245- " ${NPM} " run build:cjs
230+ npm run build:cjs
246231 popd > /dev/null
247232 pushd " ${SCRIPT_DIR} /../../../Frontend/ui-library" > /dev/null
248- " ${NPM} " run build:cjs
233+ npm run build:cjs
249234 popd > /dev/null
250235 pushd " ${SCRIPT_DIR} /../../../Frontend/implementations/typescript" > /dev/null
251- " ${NPM} " run build:dev
236+ npm run build:dev
252237 popd > /dev/null
253238 else
254239 echo ' Skipping building Frontend because files already exist. Please run with "--build" to force a rebuild'
@@ -372,27 +357,25 @@ function start_process() {
372357
373358# Assumes the following are set
374359# SCRIPT_DIR = The path to the platform_scripts
375- # NPM = The npm command path
376360function build_wilbur() {
377361 if [ ! -d " ${SCRIPT_DIR} /../../dist" ] || [ " $BUILD_WILBUR " == " 1" ] ; then
378362 pushd " ${SCRIPT_DIR} /../.." > /dev/null
379363 echo Building wilbur
380- " ${NPM} " run build
364+ npm run build
381365 popd > /dev/null
382366 fi
383367}
384368
385369# Assumes the following are set
386370# SCRIPT_DIR = The path to the platform_scripts
387- # NPM = The npm command path
388371# SERVER_ARGS The arguments to be passed to the server
389372function start_wilbur() {
390373 pushd " ${SCRIPT_DIR} /../../../SignallingWebServer" > /dev/null
391374
392375 echo " Starting wilbur signalling server use ctrl-c to exit"
393376 echo " ----------------------------------"
394377
395- start_process " sudo PATH= \" $PATH \" \" $NPM \" start -- ${SERVER_ARGS} "
378+ start_process " sudo env \" PATH= $PATH \" npm start -- ${SERVER_ARGS} "
396379
397380 popd > /dev/null
398381}
0 commit comments