-
Notifications
You must be signed in to change notification settings - Fork 192
/
super-tabs.sh
executable file
·203 lines (165 loc) · 4.83 KB
/
super-tabs.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
WORK_DIR=${TMP:-$DIR}
WS_DIR="${WORK_DIR}/.super-tabs-tmp"
EXAMPLE_PROJECT_DIR_DEFAULT="$(cd "${DIR}/../" >/dev/null 2>&1 && pwd)/ionic-super-tabs-example"
EXAMPLE_PROJECT_DIR=${EXAMPLE_PROJECT_DIR:-${EXAMPLE_PROJECT_DIR_DEFAULT}}
trap '_cleanWorkspace' EXIT
trap 'exit' SIGINT SIGTERM
declare -a _cmd_setup=("setup" "Installs node modules for all packages")
declare -a _cmd_build=("build" "Builds core and angular projects" '--only <core|angular>')
declare -a _cmd_copy=("copy" "Copies built packages as NPM modules to \`${EXAMPLE_PROJECT_DIR}\`")
declare -a _cmd_dev=("dev" 'Runs build and then copy')
declare -a _cmd_publish=("publish" 'Runs build and publishes package to npm' '--npm-args <additional npm arguments>')
_formatArgs() {
_args=$(printf "%s" "$2")
for arg in "${@:3}"; do
_args=$(printf "%s\n\t\t\e[37m%s\e[0m" "${_args}" "${arg}")
done
printf "%s" "${_args}"
}
_printUsage() {
declare -a args=(
"$(basename "${0}")"
"${_cmd_setup[0]}" "$(_formatArgs "${_cmd_setup[@]}")"
"${_cmd_build[0]}" "$(_formatArgs "${_cmd_build[@]}")"
"${_cmd_copy[0]}" "$(_formatArgs "${_cmd_copy[@]}")"
"${_cmd_dev[0]}" "$(_formatArgs "${_cmd_dev[@]}")"
"${_cmd_publish[0]}" "$(_formatArgs "${_cmd_publish[@]}")"
)
printf "\e[1m\e[94m................................................................
.............. ...................
.............. Ionic Super Tabs Helper ...................
.............. ...................
................................................................
\e[0m
\e[1m\e[36mUsage\e[0m: %s <command> [options]
\e[1m\e[36mCommands:\e[0m
* \e[1m%10s:\e[0m %s
* \e[1m%10s:\e[0m %s
* \e[1m%10s:\e[0m %s
* \e[1m%10s:\e[0m %s
* \e[1m%10s:\e[0m %s
" "${args[@]}"
}
errExit() {
echo -e "\e[1m\e[91m${*}\e[0m"
exit 1
}
_exec() {
# s=$(date +%s%N | cut -b1-13)
"$@" || errExit "Failed to execute command"
# e=$(date +%s%N | cut -b1-13)
# d=$((e - s))
# _logv "executed ${*} in ${d}ms"
}
_execSilent() {
"$@" &>/dev/null
}
_setupWorkspace() {
_execSilent cd "${DIR}"
_cleanWorkspace
mkdir -p "${WS_DIR}" >/dev/null || errExit 'unable to setup workspace'
}
_cleanWorkspace() {
if [ -d "${WS_DIR}" ]; then
rm -rf "${WS_DIR}" >/dev/null || errExit 'unable to clean workspace'
fi
}
_logv() {
echo -e "\e[33m${*}\e[0m"
}
_log() {
echo -e "\e[36m${*}\e[0m"
}
_build() {
local _only
for ((i = 1; i <= $#; i++)); do
case "${!i}" in
"--only")
local _next="$((i + 1))"
_only="${!_next}"
;;
esac
done
case "${_only}" in
"angular")
_log "Building angular module"
_exec npx lerna run build --scope=@ionic-super-tabs/angular --stream --no-progress --loglevel=silent
;;
"core")
_log "Building core module"
_exec npx lerna run build --scope=@ionic-super-tabs/core --stream --no-progress --loglevel=silent
;;
"react")
_log "Building react module"
_exec npx lerna run build --scope=@ionic-super-tabs/react --stream --no-progress --loglevel=silent
;;
*)
_log "Building all modules"
_exec npx lerna run build --concurrency=1 --stream --no-progress --loglevel=silent
;;
esac
}
_copy() {
if [ ! -d "${EXAMPLE_PROJECT_DIR}" ]; then
errExit "Example project does not exists in ${EXAMPLE_PROJECT_DIR}"
fi
_log "Copying built modules to example app"
cd core || exit 1
_logv "Packing core..."
core_tgz=$(npm pack 2>&1 | tail -1)
mv "${core_tgz}" "${WS_DIR}"
cd ../angular/dist || exit 1
_logv "Packing angular..."
ng_tgz=$(npm pack 2>&1 | tail -1)
mv "${ng_tgz}" "${WS_DIR}"
cd ../../
local extractDest="${EXAMPLE_PROJECT_DIR}/node_modules/@ionic-super-tabs/"
_logv "Extracting packages to ${extractDest}"
tar xf "${WS_DIR}/${core_tgz}" -C "${extractDest}"
cp -a "${extractDest}package/." "${extractDest}/core/"
rm -rf "${extractDest}package"
tar xf "${WS_DIR}/${ng_tgz}" -C "${extractDest}"
cp -a "${extractDest}package/." "${extractDest}/angular/"
rm -rf "${extractDest}package"
_log "Good luck!"
}
_publish() {
_log "Publishing all packages to npm"
cd "${DIR}/core" || exit 1
npm publish --access public || exit 1
cd "${DIR}/angular/dist" || exit 1
npm publish --access public || exit 1
cd "${DIR}/react" || exit 1
npm publish --access public || exit 1
}
_setupWorkspace
case $1 in
"setup")
_exec npx lerna bootstrap
;;
"build")
_build "${@:2}"
;;
"copy")
_copy "${@:2}"
;;
"dev")
_build --only core
_build --only angular
_copy
;;
"publish")
_publish "${@:2}"
;;
"docs")
find "${DIR}/core/src" -type f -name '*.md' | awk 'match($0, /([a-z-]+)\/readme.md/,m) { print m[1] }' | xargs -I@ cp "${DIR}/core/src/@/readme.md" "${DIR}/docs/@.md"
;;
"exec")
cd "${DIR}" && _exec "${@:2}"
;;
*)
_printUsage
;;
esac