-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.sh
228 lines (185 loc) · 4.19 KB
/
run.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/binbash
# The program should be run in the root directory of the project
set -e
NPM="pnpm"
source ".github/basic-env.sh"
# functions: is_abs_path, fmt_abs_path
# var: CUR_DIR (absolute path)
# env: NPM, WEBSITE_DIR (absolute path)
opt_setup=false
opt_watch=false
opt_apply=true
opt_only_apply=false
opt_clean=false
ARGS=()
# Print functions
ANSI_CYAN="$(echo -en '\033[0;36m')"
ANSI_MAGENTA="$(echo -en '\033[0;35m')"
ANSI_RED="$(echo -en '\033[0;31m')"
ANSI_RESET="$(echo -en '\033[0m')"
prevent_next_print_newline=true
PRINT_PREFIX=""
function add_print_prefix() {
PRINT_PREFIX+="- "
}
function pop_print_prefix() {
PRINT_PREFIX="${PRINT_PREFIX::-2}"
}
function prevent_next_newline() {
prevent_next_print_newline=true
}
function print() {
if [ -z "$1" ]; then
[ "$prevent_next_print_newline" == false ] && echo
else
echo "${ANSI_CYAN}$PRINT_PREFIX$1${ANSI_RESET}"
fi
prevent_next_print_newline=false
}
# Ensure pnpm
if ! "$NPM" help 2>/dev/null | grep -q "Usage: pnpm" ; then
print "${ANSI_RED}Error: ${ANSI_RESET}pnpm is required to run this script"
print
prevent_next_newline
exit 1
fi
# Arguments parsing
while [ "$#" -gt 0 ]; do
case "$1" in
--setup)
opt_setup=true ;;
--skip-apply)
opt_apply=false ;;
--only-apply)
opt_only_apply=true
ARGS=()
break ;;
--watch)
opt_watch=true ;;
--clean)
opt_clean=true ;;
--outDir)
ARGS+=("$1")
shift
# if no argument, skip
if [ -z "$1" ]; then
shift
continue
fi
# if starts with "-", it's an option
if [[ "$1" == -* ]]; then
ARGS+=("$1")
continue
fi
# modify the path
if is_abs_path "$1"; then
ARGS+=("$1")
else
ARGS+=("$CUR_DIR/$1")
fi ;;
--outDir=*)
out_dir="${1#*=}"
# modify the path
if ! is_abs_path "$out_dir"; then
out_dir="$CUR_DIR/$out_dir"
fi
ARGS+=("--outDir=\"$out_dir\"")
echo "Output directory: $out_dir"
unset out_dir ;;
*)
ARGS+=("$1") ;;
esac
shift
done
# Functions
function npm_program() {
"$NPM" --prefix "$CUR_DIR" "$@"
}
function npm_website() {
"$NPM" --prefix "$WEBSITE_DIR" "$@"
}
function apply() {
print && print "Applying your modifications"
add_print_prefix
bash "$CUR_DIR/apply.sh"
pop_print_prefix
}
function clean() {
print && print "Cleaning up"
add_print_prefix
npm_website run clean
cd "$WEBSITE_DIR"
git reset --hard
cd "$CUR_DIR"
pop_print_prefix
}
function setup() {
print && print "Setting up the website"
add_print_prefix
print "Preparing the submodules"
git submodule update --init
if [ "$opt_apply" == true ]; then
prevent_next_newline
add_print_prefix
apply
pop_print_prefix
fi
print "Installing dependencies"
npm_website install
pop_print_prefix
}
# Main
# auto setup if node_modules is missing
if [ ! -d "$WEBSITE_DIR/node_modules" ]; then
opt_setup=true
fi
# empty arguments
if [ "${#ARGS[@]}" -eq 0 ]; then
if [ "$opt_setup" == true ]; then
setup
exit 0
elif [ "$opt_only_apply" == true ]; then
apply
exit 0
elif [ "$opt_clean" == true ]; then
clean
exit 0
else
npm_website run
exit 0
fi
fi
if [ "$opt_setup" == true ]; then
# --setup
setup
elif [ "$opt_apply" == true ]; then
# --apply (only when `--setup` is not set, for `setup` already runs `apply`)
apply
fi
# --watch
if [ "$opt_watch" == true ]; then
print && print "Watching for file changes"
add_print_prefix
cd "$CUR_DIR" && node .github/watch.mjs "$WEBSITE_DIR" &
pop_print_prefix
fi
# website command
print && print "Start running the website command"
NPM_DISPLAY="$NPM"
[[ "$NPM" == *" "* ]] && NPM_DISPLAY="$(echo "$NPM" | sed 's/"/\\"/g' | sed 's/^/"/' | sed 's/$/"/')"
ARG_DISPLAY=""
for arg in "${ARGS[@]}"; do
if [[ "$arg" == *" "* ]] || [[ "$arg" == *"'"* ]] || [[ "$arg" == *'"'* ]]; then
ARG_DISPLAY+=" \"$(echo "$arg" | sed 's/"/\\"/g')\""
else
ARG_DISPLAY+=" $arg"
fi
done
print "${ANSI_MAGENTA}> $NPM_DISPLAY run${ARG_DISPLAY}${ANSI_RESET}"
add_print_prefix
npm_website run "${ARGS[@]}"
pop_print_prefix
# --clean
if [ "$opt_clean" == true ]; then
clean
fi