forked from acrewdson/cfgov-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.sh
executable file
·125 lines (106 loc) · 3.54 KB
/
frontend.sh
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
#!/bin/bash
# ==========================================================================
# Setup script for installing project dependencies.
# NOTE: Run this script while in the project root directory.
# It will not run correctly when run from another directory.
# ==========================================================================
# Set script to exit on any errors.
set -e
# Initialize project dependency directories.
init() {
# Set NODE_ENV variable.
# Set default command-line environment flag, if user didn't supply one.
NODE_ENV=$1
# Warn if unsupported command-line flag was used.
if [ "$NODE_ENV" != "development" ] &&
[ "$NODE_ENV" != "production" ]; then
supplied_cli_flag=$NODE_ENV
NODE_ENV='development'
echo "WARNING: '$supplied_cli_flag' flag not found, reverting to $NODE_ENV environment."
fi
# Notify of environment that user is in.
echo "Front-end environment: $NODE_ENV"
# Set the NODE_ENV for this session.
export NODE_ENV=$NODE_ENV
}
# Clean project dependencies.
clean() {
# If the node directory already exists,
# clear it so we know we're working with a clean
# slate of the dependencies listed in package.json.
if [ -d node_modules ]; then
echo "Removing project dependency directories…"
rm -rf node_modules
echo "Project dependencies have been removed."
fi
}
# Install project dependencies.
install() {
echo "Installing front-end dependencies…"
if [ "$NODE_ENV" = "development" ]; then
npm install -d --loglevel warn
# Protractor = JavaScript acceptance testing framework.
echo "Installing Protractor dependencies locally…"
# We skip Gecko here (--gecko false) because webdriver pulls its release
# directly from a GitHub.com URL which enforces rate-limiting. This can
# cause installation failures when running automated testing. Currently
# we don't rely on Gecko for testing.
./node_modules/protractor/bin/webdriver-manager update --gecko false --standalone false
else
npm install --production --loglevel warn --no-optional
fi
}
# Calculate checksum value.
calc_checksum() {
DEP_CHECKSUM=$(cat package*.json cfgov/unprocessed/apps/**/package*.json | shasum -a 256)
}
# Add a checksum file
write_checksum() {
echo -n "$DEP_CHECKSUM" > node_modules/CHECKSUM
echo "Wrote node_modules/CHECKSUM $DEP_CHECKSUM"
}
# Write file that says the node environment that we're in.
# We can read this on next run to see if the checksum should be bashed.
write_node_env() {
echo -n "${NODE_ENV}" > "node_modules/NODE_ENV"
echo "Wrote node_modules/NODE_ENV $NODE_ENV"
}
# Analyze setup and see if we need to install npm dependencies.
should_rebuild() {
[ ! -f node_modules/NODE_ENV ] ||
[ ! -f node_modules/CHECKSUM ] ||
[ "$NODE_ENV" != "$(cat node_modules/NODE_ENV)" ] ||
[ "$DEP_CHECKSUM" != "$(cat node_modules/CHECKSUM)" ]
}
# If the node directory exists, node_modules/CHECKSUM exists, and
# the contents DO NOT match the checksum of package.json, clear
# node_modules so we know we're working with a clean slate of the
# dependencies listed in package.json.
clean_and_install() {
calc_checksum
if should_rebuild; then
clean
install
calc_checksum
write_checksum
write_node_env
else
echo "Dependencies are up to date."
fi
}
# Run tasks to build the project for distribution.
build() {
echo "Building project…"
npm run gulp build
}
# Execute requested (or all) functions.
if [ "$1" == "init" ]; then
init ""
clean_and_install
elif [ "$1" == "build" ]; then
build
else
init "$1"
clean_and_install
build
fi