-
Notifications
You must be signed in to change notification settings - Fork 57
/
assets.sh
executable file
·165 lines (137 loc) · 5.49 KB
/
assets.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
SUPPORTED_ENVIRONMENTS=("production" "sandbox" "stage")
SUPPORTED_MODULES=("library" "components" "render")
# default value if none supplied
environment="production,sandbox,stage"
module="library,components,render"
devTouchpoint=false
while getopts ":v:e:m:t:s:d" flag
do
case "$flag" in
v) version=$OPTARG;;
e) environment=$OPTARG;;
m) module=$OPTARG;;
t) tag=$OPTARG;;
s) testEnv=$OPTARG;;
d) devTouchpoint=true;;
esac
done
if [ -z "$version" ]; then
# Extract the version from the package.json
version=$(cat ./package.json | awk '/version/' | sed "s/[^0-9.]//g")
fi
if [ ! -z "$tag" ]; then
if [[ ! $tag =~ ^[a-zA-Z0-9_]+$ ]]; then
printf "Stage tag must only contain alpha-numeric and underscore characters\n\n"
exit 1
fi
if [ "${#tag}" -gt 30 ]; then
printf "Stage tag must be 30 characters or less (${#tag})\n\n"
exit 1
fi
# Underscores not valid semver
version=$version-$(echo $tag | sed "s/_/-/g" | sed -E "s/-([0-9]+)$/.\1/")
# Check if the CDN tag has already been used before spending time on the webpack build
tagStatus=$(web status $tag 2>&1)
if [[ $tagStatus =~ "staged" ]]; then
printf "Stage tag already exists and must be unique ($tag)\n\n"
exit 1
fi
fi
if [[ ! -z "$testEnv" ]]; then
# Ensure proper prefix
testEnv="$(echo $testEnv | sed -E 's/(https?:\/\/)?(www.)?([^\/]+)(.+)?/\3/')"
fi
if [[ ! -z "$tag" || ! -z "$testEnv" ]]; then
# Only stage is supported for Test Environments and Stage Tags
environment="stage"
fi
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_-]+(\.[0-9]+)?)?$ ]]; then
printf "\nInvalid version provided ($version). Please chack the format (ex: 1.0.0-beta.0)\n\n"
exit 1
fi
rm -rf ./dist
IFS=',' # Set the delimiter for splitting into an array
read -a envarr <<< "$environment"
read -a modulearr <<< "$module"
printf "v$version\n"
printf "\nBuilding the following environments:"
# Filter out unsupported environment keywords or typos
for env in "${envarr[@]}"
do
if printf '%s\n' "${SUPPORTED_ENVIRONMENTS[@]}" | grep -q -e "^$env$"; then
[[ "$env" = "stage" && ! -z "$testEnv" ]] && printf "\n - $env ($testEnv)" || printf "\n - $env"
filteredEnvArr+=("$env")
fi
done
printf "\n"
printf "\nBuilding the following modules:"
# Filter out unsupported environment keywords or typos
for mod in "${modulearr[@]}"
do
if printf '%s\n' "${SUPPORTED_MODULES[@]}" | grep -q -e "^$mod$"; then
printf "\n - $mod"
filteredModule+=",$mod"
fi
done
printf "\n\n"
filteredModule="${filteredModule:1}"
# Optional webpack args
[[ ! -z "$tag" ]] && optionalArgs+=("--env" "STAGE_TAG=$tag")
[[ ! -z "$testEnv" ]] && optionalArgs+=("--env" "TEST_ENV=https://www.$testEnv")
[[ $devTouchpoint = true ]] && optionalArgs+=("--env" "DEV_TOUCHPOINT")
# Build assets for each environment
for env in "${filteredEnvArr[@]}"
do
npm run --silent build:"$env" -- --env VERSION="$version" --env MODULE="$filteredModule" ${optionalArgs[@]} &> /dev/null
if [ "$env" = "production" ]; then dir="js"; else dir="$env"; fi
# Remove the license file from the asset bundle
rm ./dist/*.LICENSE.txt &> /dev/null
# Create the necessary directories
mkdir -p ./dist/bizcomponents/"$dir"/versioned
mv ./dist/*.{js,map} ./dist/bizcomponents/"$dir"
cd ./dist/bizcomponents/"$dir"
# Create versioned copies of every asset
for fullfile in ./*.js
do
filename=$(basename $fullfile .js)
echo "/* version: $version */" > ./temp.txt
# Remove license file line
sed '/LICENSE.txt/d' ./"$filename".js >> ./temp.txt && mv ./temp.txt ./"$filename".js
# Copy file minus the source map url on the last line
sed \$d ./"$filename".js > ./versioned/"$filename"@"$version".js
# Append the source map url to the file
echo "//# sourceMappingURL=$filename@$version.js.map" >> ./versioned/"$filename"@"$version".js
# Copy source map to versioned copy
cp ./"$filename".js.map ./versioned/"$filename"@"$version".js.map
done
cd ../../..
done
# Deploy to stage CDN
if [ ! -z "$tag" ]; then
if ! command -v web &> /dev/null; then
printf "\nPlease install the 'web' cli tool using 'npm i -g @paypalcorp/web' to deploy the stage tag version\n\n"
exit 1
fi
# Manually replace the globals.js variables so that it applies to the SDK bundler
sed -i.bak "s/env.STAGE_TAG/'$tag'/" ./globals.js
sed -i.bak "s/env.VERSION/'$version'/" ./globals.js
[[ ! -z "$testEnv" ]] && sed -i.bak "s/env.TEST_ENV/'https:\/\/www.$testEnv'/" ./globals.js
[[ $devTouchpoint = true ]] && sed -i '' "s/env.DEV_TOUCHPOINT/true/" ./globals.js
# Pack the library module similar to publishing the module to npm
npm pack --quiet > /dev/null
mv ./*.tgz ./dist/bizcomponents/stage/package.tgz
# Reset the manual variables
sed -i.bak "s/'$tag'/env.STAGE_TAG/" ./globals.js
sed -i.bak "s/'$version'/env.VERSION/" ./globals.js
[[ ! -z "$testEnv" ]] && sed -i.bak "s/'https:\/\/www.$testEnv'/env.TEST_ENV/" ./globals.js
[[ $devTouchpoint = true ]] && sed -i '' "s/__DEV_TOUCHPOINT__: true/__DEV_TOUCHPOINT__: env.DEV_TOUCHPOINT/" ./globals.js
# remove temporary file if it exists
rm globals.js.bak &> /dev/null
printf "web stage --tag $tag\n"
web stage --tag "$tag"
# Reset modified dist files
git checkout -- dist
# Remove new dist files
git clean -fd dist > /dev/null
fi