Skip to content

Instantly share code, notes, and snippets.

@anhnguyen1618
Created September 27, 2017 19:50
Show Gist options
  • Save anhnguyen1618/e150fff958c5986ecbe94a1b42196ea9 to your computer and use it in GitHub Desktop.
Save anhnguyen1618/e150fff958c5986ecbe94a1b42196ea9 to your computer and use it in GitHub Desktop.
if [ "$MODE" == "development" ] || [ "$MODE" == "singletest" ]; then
CONFIG_OUTFILE="/app/angular/src/assets/config.json"
# Clean cached version, if any
if [ -f "$CONFIG_OUTFILE" ]; then
rm -f $CONFIG_OUTFILE
fi
else
CONFIG_OUTFILE="/app/angular/dist/assets/config.json"
fi
function wait_for_selenium () {
echo "Waiting for selenium startup (selenium-chrome:4444)"
while ! nc -z selenium-chrome 4444; do
sleep 0.5
done
echo "Connection to selenium succeeded"
}
function wait_for_nodejs () {
echo "Waiting for nodejs server startup (localhost:10080)"
while ! nc -z localhost 10080; do
sleep 0.5
done
echo "Connection to nodejs succeeded"
}
# Writes config file from environment variables which have given prefix
ARRAY=()
echo "{" > $CONFIG_OUTFILE
while IFS='=' read -r name value ; do
if [[ $name == *'STARA'* ]]; then
variable=" \"$name\": \"$value\","
ARRAY+=$variable
fi
done < <(env)
# If more than 1 variable, remove the last entry's comma
if [ ${#ARRAY[@]} > 1 ]; then
last_variable=${ARRAY[-1]}
last_variable=${last_variable::-1}
ARRAY[${#ARRAY[@]}-1]=$last_variable
fi
printf "%s\n" "${ARRAY[@]}" >> $CONFIG_OUTFILE
echo "}" >> $CONFIG_OUTFILE
# Startup
if [ "$MODE" == "development" ]; then
# Development mode
cd angular
# Get packages hash, and stored hash (in file)
hash_file_location="/app/angular/.angular-package.hash"
package_hash=`cat package.json yarn.lock |md5sum|xargs`
if [ -f $hash_file_location ]; then stored_hash=$(cat ${hash_file_location} |xargs); else stored_hash=$package_hash; fi
# Check that node_modules is present
if [ ! -d node_modules ] || [ "$(find node_modules |wc -l)" == "1" ]; then
echo "No node_modules present"
if [ -d node_modules ] ; then rmdir node_modules; fi
if [ -f /app/.angular_node_modules.tar.gz ]; then
echo "Unpacking from archive..."
tar -zxf /app/.angular_node_modules.tar.gz
echo "Running yarn install (to update, if local changes)"
sudo -u app yarn install
echo "Done!"
else
echo "No node_modules.tar.gz found, installing.."
sudo -u app yarn install
fi
elif [ "$package_hash" != "$stored_hash" ]; then
echo "Detected package.json or yarn.lock change, running yarn install"
# Install modules
if [ ! -d node_modules ] ; then
mkdir node_modules
fi
chown app:app -R node_modules
# Run as user app (security)
sudo -u app yarn install
fi
echo $package_hash > $hash_file_location
# Start angular-cli
sudo -E -u app ./node_modules/.bin/ng serve --host 0.0.0.0 --port 10080 --live-reload-port 10081
elif [ "$MODE" == "singletest" ]; then
cd angular
echo "Starting tests... (this might take 1-2mins)"
wait_for_selenium
sudo -E -u app ./node_modules/.bin/ng test --single-run --progress false
elif [ "$MODE" == "robottest" ]; then
echo "Starting node server"
cd /app/node
sudo -E -u app npm start &
cd /app/angular
wait_for_nodejs
wait_for_selenium
# run robot tests, and capture exit code to err variable
robot --outputdir e2e/output/ --include QUICK_RUN e2e/tests/*.robot
err=$?
sudo chmod a+w e2e/output/ -R
# exit with robot tests exit code
exit ${err}
elif [ "$MODE" == "singletest_with_coverage" ]; then
cd angular
echo "Starting tests... (this might take 1-2mins)"
wait_for_selenium
sudo -E -u app ./node_modules/.bin/ng test --single-run --code-coverage --progress false
sudo chmod a+w coverage/ -R
elif [ "$MODE" == "raml2html" ]; then
cd angular
echo "Starting raml2html"
sudo ./node_modules/raml2html/bin/raml2html -i src/docs/rest-api/v1/rest-api.raml -o src/assets/docs/rest-api.html
sudo chown app:app src/assets/docs/rest-api.html
elif [ "$MODE" == "lint" ]; then
cd angular
echo "Starting linter"
sudo -E -u app ./node_modules/.bin/ng lint
elif [ "$MODE" == "lint_scss" ]; then
cd angular
echo "Starting scss linter"
sudo -E -u app ./node_modules/.bin/sass-lint -v -q
else
# Production mode, start node
cd node
sudo -E -u app npm start
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment