forked from cfpb/consumerfinance.gov
/
frontend.sh
executable file
·149 lines (126 loc) · 4.15 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh
# ==========================================================================
# 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 default command-line environment flag, if user didn't supply one.
if [ -z "$1" ]; then
cli_flag='development'
else
cli_flag=$1
fi
# Warn if unsupported command-line flag was used.
if [ "$cli_flag" != "development" ] &&
[ "$cli_flag" != "test" ] &&
[ "$cli_flag" != "production" ]; then
echo "\033[33;33mWARNING: '$cli_flag' flag not found, reverting to development environment.\033[0m"
cli_flag='development'
fi
# Notify of environment that user is in.
if [ "$cli_flag" = "development" ]; then
echo 'Frontend:\033[33;33m development environment\033[0m.'
elif [ "$cli_flag" = "test" ]; then
echo 'Frontend:\033[33;1m test environment\033[0m.'
elif [ "$cli_flag" = "production" ]; then
echo 'Frontend:\033[32;1m production environment\033[0m.'
fi
ENVVAR_SAMPLE=.env_SAMPLE
ENVVAR=.env
NODE_DIR=node_modules
BOWER_DIR=bower_components
# Copy sample environment variables, if not already done.
if [ ! -f $ENVVAR ]; then
echo 'Setting default environment variables...'
cp -a $ENVVAR_SAMPLE $ENVVAR
fi
if [ -f .bowerrc ]; then
# Get the "directory" line from .bowerrc
BOWER_DIR=$(grep "directory" .bowerrc | cut -d '"' -f 4)
fi
echo 'Environment variables:' $ENVVAR
echo 'npm components directory:' $NODE_DIR
echo 'Bower components directory:' $BOWER_DIR
}
# Clean project dependencies.
clean(){
# If the node and bower directories already exist,
# clear them so we know we're working with a clean
# slate of the dependencies listed in package.json
# and bower.json.
if [ -d $NODE_DIR ] || [ -d $BOWER_DIR ]; then
echo 'Removing project dependency directories...'
rm -rf $NODE_DIR
rm -rf $BOWER_DIR
fi
echo 'Project dependencies have been removed.'
}
# Install project dependencies.
install(){
echo 'Installing project dependencies...'
# Copy globally-installed packages.
# Protractor - JavaScript acceptance testing.
if [ "$cli_flag" = "development" ] ||
[ "$cli_flag" = "test" ]; then
# Before installing dependencies,
# create variables for globally-installed ones.
local is_installed_protractor=$(is_installed protractor)
npm install -d --loglevel warn
# Copy globally-installed packages.
# Protractor = JavaScript acceptance testing framework.
if [ $is_installed_protractor = 0 ]; then
echo 'Installing Protractor dependencies locally...'
./$NODE_DIR/protractor/bin/webdriver-manager update
else
echo 'Global Protractor installed. Copying global install locally...'
protractor_symlink=$(command -v protractor)
protractor_binary=$(readlink $protractor_symlink)
protractor_full_path=$(dirname $protractor_symlink)/$(dirname $protractor_binary)/../../protractor
if [ ! -d $protractor_full_path/selenium ]; then
echo '\033[33;31mERROR: Please run `webdriver-manager update` and try again!\033[0m'
exit
fi
mkdir -p ./$NODE_DIR/protractor
cp -r $protractor_full_path ./$NODE_DIR/
fi
else
npm install --production --loglevel warn
fi
bower install --config.interactive=false
}
# Run tasks to build the project for distribution.
build(){
echo 'Building project...'
gulp clean
gulp build
if [ "$cli_flag" = "production" ]; then
gulp scripts:ondemand
fi
}
# Returns 1 if a global command-line program installed, else 0.
# For example, echo "node: $(is_installed node)".
is_installed(){
# Set to 1 initially.
local return_=1
# Set to 0 if program is not found.
type $1 >/dev/null 2>&1 || { local return_=0; }
echo "$return_"
}
if [ "$1" == "init" ] || [ "$1" == "build" ]; then
if [ "$1" == "init" ]; then
init ""
clean
install
else
build
fi
else
init "$1"
clean
install
build
fi