-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.bash
executable file
·277 lines (244 loc) · 7.85 KB
/
build.bash
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
# Defined as the current working directory.
current_directory=$(pwd)
# Define the nme directory.
nme_directory="$current_directory/node_modules/.bin"
if [[ -n "${NMEDIR}" ]]; then
nme_directory="${NMEDIR}"
fi
# Function for formatted echo.
function formatted_echo() {
echo "> $1"
}
# Node Module Execute (nme) function.
# The module executable location can be changed with the NMEDIR environment variable.
function nme() {
if [ -f "$nme_directory/$1" ]; then
formatted_echo "Executing: $*"
"$nme_directory/"$*
else
formatted_echo "FATAL!: The executable '$nme_directory/$1' was not found!"
formatted_echo "Note: The location can be changed using the NMEDIR environment variable."
exit
fi
}
# Function to echo the current step.
# First paramter should be name, second should be 'start' or 'end'.
function echo_step() {
# Function to echo a line.
function echo_line() {
echo ">==============================================<"
}
# Log the start/end message accordingly.
if [ "$2" = "start" ]; then
#echo_line
formatted_echo "Step $1 is executing...."
#echo_line
elif [ "$2" = "end" ]; then
#echo_line
formatted_echo "Step $1 has finished executing."
#echo_line
fi
}
# Function to delete a directory.
function delete_directory() {
# Check for the directroy, execute accordingly.
if [ -d "$1" ]; then
formatted_echo "Deleting the '$1' directory..."
rm -r "$1"
else
formatted_echo "The directory '$1' does not exists, skipping deletion..."
fi
}
# Function to build the project.
function build() {
# Reset directory.
cd "$current_directory" || exit
# Echo the step.
echo_step "build" "start"
# Delete the build folder.
delete_directory 'build'
# Build CommonJs.
formatted_echo "Building CommonJS..."
nme tsc --module commonjs --outDir build/cjs/ --declaration false --declarationMap false --esModuleInterop true --noEmitOnError true
echo "{\"type\": \"commonjs\"}" >build/cjs/package.json
# Build ESM and Types.
formatted_echo "Building ESM and Type Definitions..."
nme tsc --module es2022 --outDir build/esm/ --declarationDir build/types/ --declaration true --declarationMap true --noEmitOnError true
echo "{\"type\": \"module\"}" >build/esm/package.json
# Build the CLI.
formatted_echo "Building CLI ..."
cd ./src/cli/ || exit
nme tsc --outDir ../../build/cli/ --declaration false --declarationMap false --noEmitOnError true
echo "{\"type\": \"module\"}" >../../build/cli/package.json
# Echo the end of the step.
echo_step "build" "end"
}
# Function to format.
function format() {
# Reset directory.
cd "$current_directory" || exit
# Format.
# https://github.com/prettier/prettier/issues/15438
echo_step "format" "start"
formatted_echo "Formatting..."
if [ -d "build" ]; then
formatted_echo "Build directory found! Formatting..."
nme prettier ./build --write --ignore-path .prettierignore --cache
fi
if [ -d "docs" ]; then
formatted_echo "Docs directory found! Formatting..."
nme prettier ./docs --write --ignore-path .prettierignore --cache
fi
nme prettier ./ --write --cache
echo_step "format" "end"
}
# Function to test.
function test() {
# Reset directory.
cd "$current_directory" || exit
# Run the selcted tests.
echo_step "test" "start"
if [ "$1" = "other" ]; then
formatted_echo "Running other (ESM) test..."
node test/other.js --trace-uncaught --trace-warnings --trace-exit
elif [ "$1" = "other-cjs" ]; then
formatted_echo "Running other (CommonJs) test..."
node test/other.cjs --trace-uncaught --trace-warnings --trace-exit
else
formatted_echo "Running tests..."
node test/index.js --trace-uncaught --trace-warnings --trace-exit
fi
echo_step "test" "end"
}
# Function to run eslint.
function eslint() {
# Reset directory.
cd "$current_directory" || exit
# Run eslint.
echo_step "eslint" "start"
formatted_echo "Running eslint..."
nme eslint src/
echo_step "eslint" "end"
}
# Function to generate docs.
function docs() {
# Reset directory.
cd "$current_directory" || exit
# Generate docs.
echo_step "docs" "start"
formatted_echo "Generating docs..."
delete_directory "docs"
nme typedoc --hideGenerator --githubPages false
node tools/gen-web.cjs
echo_step "docs" "end"
}
# Function to link the package.
function link() {
# Reset directory.
cd "$current_directory" || exit
# Link the package.
echo_step "link" "start"
formatted_echo "Linking package..."
npm link ./ --force
npm link sxcu.api --force
echo_step "link" "end"
}
# Fix linked package permissions.
function fix_perms() {
# Reset directory.
cd "$current_directory" || exit
# Fix linked script permissions.
echo_step "fix perms" "start"
formatted_echo "Fixing linked script's permissions..."
chmod +x node_modules/.bin/sxcu || echo "Failed to fix permissions."
echo_step "fix perms" "end"
}
# Function for running the docs-server.
function docs_server() {
# Reset directory.
cd "$current_directory" || exit
# Run the docs server.
echo_step "docs server" "start"
formatted_echo "Starting docs server..."
node tools/docs-server.js
echo_step "docs server" "end"
}
# Function for cleaning build/doc etc folders.
function clean() {
# Reset directory.
cd "$current_directory" || exit
# Clean
echo_step "clean" "start"
delete_directory "build"
delete_directory "docs"
echo_step "clean" "end"
}
# List of commands.
command_list="help, build, format, test [other, other-cjs], eslint, docs, link, fix-perms, compile, docs-server, clean, quick-compile"
# Function to parse command.
# First paramter should be the name of the command.
# Second paramater is optional, used in commands such as test.
function parse_command() {
if [ "$1" = "build" ]; then
build
elif [ "$1" = "format" ]; then
format
elif [ "$1" = "test" ]; then
test "$2"
elif [ "$1" = "eslint" ]; then
eslint
elif [ "$1" = "docs" ]; then
docs
elif [ "$1" = "link" ]; then
link
elif [ "$1" = "fix-perms" ]; then
fix_perms
elif [ "$1" = "compile" ]; then
build
docs
format
eslint
link
fix_perms
elif [ "$1" = "quick-compile" ]; then
build
link
fix_perms
elif [ "$1" = "docs-server" ]; then
docs_server
elif [ "$1" = "clean" ]; then
clean
elif [ "$1" = "help" ]; then
formatted_echo "::[---> HELP ~ sxcu.api/build.bash <---]::"
formatted_echo
formatted_echo "Commands: $command_list"
formatted_echo "Square brackets indicate the avalible second paramaters for that command, if any."
formatted_echo
formatted_echo "NOTE: If you do not wish to enter the REPL, just use the command directly. (Ex; bash build.bash help)"
formatted_echo
formatted_echo "If you need any help, create an issue! https://github.com/jacobhumston/sxcu.api/issues"
else
formatted_echo "Unknown command '$1'."
formatted_echo "Commands: $command_list"
fi
}
if [ "$1" = "" ]; then
while true; do
clear
formatted_echo "::[---> REPL ~ sxcu.api/build.bash <---]::"
formatted_echo
formatted_echo "Welcome to the sxcu.api build REPL!"
formatted_echo "For a list of commands, enter 'help'. Enter 'exit' to exit."
read -r -p "$ " arg1 arg2
clear
if [ "$arg1" == "exit" ]; then
exit
else
parse_command "$arg1" "$arg2"
fi
read -p "> Press any key to continue..." -r -n 1
done
else
parse_command "$1" "$2"
fi