-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
build.ps1
508 lines (461 loc) Β· 13.5 KB
/
build.ps1
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#This script is NOT autogenerated and needs manual updates.
#It is not recommanded to use it, because windows and node dont get along very well
# Use VT if Powershell version is > 5.1
if ($Host.Version.major -gt 5) {
$normal = "[0m"
$bold = "[1m"
$blue = "[34m"
$green = "[32m"
}
else {
$normal = $style.default
#there is no bold so we use inverted
$bold = $style.negative
$blue = $( tput -T "$TERM" setaf 4 )
$green = $( tput -T "$TERM" setaf 2 )
}
function enable_debugging {
if ($DEBUG) {
Set-PSDebug -Trace 2
}
}
function disable_debugging {
if ($DEBUG) {
Set-PSDebug -Trace 0
}
}
function now {
$timestamp = [System.DateTimeOffset]::Now.ToUnixTimeMilliseconds()
return $timestamp
}
function ms_elapsed {
param (
[long]$startTimestamp
)
$timestampNow = now
$elapsedMilliseconds = ($timestampNow - $startTimestamp) / 1000
return "$elapsedMilliseconds ms"
}
function print_start_message {
param (
[string]$message
)
Write-Host "$bold $blue ### $message ###$normal"
}
function print_end_message {
param (
[string]$message,
[long]$startTime
)
$elapsedTime = ms_elapsed $startTime
Write-Host "$bold $blue === $message : $elapsedTime === $normal"
}
# default values of variables set from params
$_RUN_STEP_NODE = $false
$_RUN_STEP_YARN_SETUP = $false
$_RUN_STEP_YARN_SYMLINK = $false
$_RUN_STEP_VERSIONS = $false
$_RUN_STEP_YARN_INSTALL = $false
$_RUN_STEP_BROWSERSLIST = $false
$_RUN_STEP_ENV = $false
$_RUN_STEP_UPDATE_EMOJIS = $false
$_RUN_STEP_FORMAT = $false
$_RUN_STEP_ESLINT = $false
$_RUN_STEP_TSC = $false
$_RUN_STEP_USERSCRIPT = $false
$_RUN_STEP_BUILDSCRIPT = $false
$_RUN_STEP_PREBUILD = $false
$_RUN_STEP_WEBPACK = $false
$_RUN_STEP_DOCS = $false
$_RUN_STEP_GIT_DIFF = $false
$_RUN_STEP_SERVE = $false
$MODE = "development"
$DEBUG = $false
foreach ($arg in $args) {
switch ($arg) {
"--node" {
$_RUN_STEP_NODE = $true
}
"--yarn_setup" {
$_RUN_STEP_YARN_SETUP = $true
}
"--yarn_symlink" {
$_RUN_STEP_YARN_SYMLINK = $true
}
"--versions" {
$_RUN_STEP_VERSIONS = $true
}
"--yarn_install" {
$_RUN_STEP_YARN_INSTALL = $true
}
"--browserslist" {
$_RUN_STEP_BROWSERSLIST = $true
}
"--env" {
$_RUN_STEP_ENV = $true
}
"--update_emojis" {
$_RUN_STEP_UPDATE_EMOJIS = $true
}
"--format" {
$_RUN_STEP_FORMAT = $true
}
"--eslint" {
$_RUN_STEP_ESLINT = $true
}
"--tsc" {
$_RUN_STEP_TSC = $true
}
"--userscript" {
$_RUN_STEP_USERSCRIPT = $true
}
"--buildscript" {
$_RUN_STEP_BUILDSCRIPT = $true
}
"--prebuild" {
$_RUN_STEP_PREBUILD = $true
}
"--webpack" {
$_RUN_STEP_WEBPACK = $true
}
"--docs" {
$_RUN_STEP_DOCS = $true
}
"--git_diff" {
$_RUN_STEP_GIT_DIFF = $true
}
"--serve" {
$_RUN_STEP_SERVE = $true
}
"--dependencies" {
$_RUN_STEP_YARN_SETUP = $true
$_RUN_STEP_VERSIONS = $true
$_RUN_STEP_YARN_INSTALL = $true
$_RUN_STEP_BROWSERSLIST = $true
}
"--quick" {
$_RUN_STEP_ENV = $true
$_RUN_STEP_FORMAT = $true
$_RUN_STEP_ESLINT = $true
$_RUN_STEP_TSC = $true
$_RUN_STEP_WEBPACK = $true
}
"--local" {
$_RUN_STEP_YARN_SETUP = $true
$_RUN_STEP_VERSIONS = $true
$_RUN_STEP_PREBUILD = $true
$_RUN_STEP_YARN_INSTALL = $true
$_RUN_STEP_ENV = $true
$_RUN_STEP_TSC = $true
$_RUN_STEP_USERSCRIPT = $true
$_RUN_STEP_WEBPACK = $true
$_RUN_STEP_SERVE = $true
}
"--pre-commit" {
$_RUN_STEP_FORMAT = $true
$_RUN_STEP_ESLINT = $true
$_RUN_STEP_TSC = $true
}
"--full" {
$_RUN_STEP_NODE = $true
$_RUN_STEP_YARN_SETUP = $true
$_RUN_STEP_YARN_SYMLINK = $true
$_RUN_STEP_VERSIONS = $true
$_RUN_STEP_YARN_INSTALL = $true
$_RUN_STEP_BROWSERSLIST = $true
$_RUN_STEP_ENV = $true
$_RUN_STEP_UPDATE_EMOJIS = $true
$_RUN_STEP_FORMAT = $true
$_RUN_STEP_ESLINT = $true
$_RUN_STEP_TSC = $true
$_RUN_STEP_USERSCRIPT = $true
$_RUN_STEP_BUILDSCRIPT = $true
$_RUN_STEP_PREBUILD = $true
$_RUN_STEP_WEBPACK = $true
$_RUN_STEP_DOCS = $true
$_RUN_STEP_GIT_DIFF = $true
}
"-p" {
$MODE = "production"
}
"--production" {
$MODE = "production"
}
"--debug" {
$Debug = $true
}
"--port" {
$port = $_PORT
}
default {
Write-Host "Unknown option: $arg"
exit 1
}
}
}
# expose the set port (or default port) as environment variable for local server
if ($_RUN_STEP_SERVE) {
if (!(Test-Path variable:port)) {
Write-Host "Default Port"
$env:LSSM_PORT = '36551' # because 536551 is LSSM in base 29 but port numbers are 16-bit only so we omit the leading 5
}
else {
$env:LSSM_PORT = $port
}
}
$total_start_time = now
$NODE_VERSION = (Get-Content -Path ./package.json | ConvertFrom-Json).engines.node
$YARN_VERSION = (Get-Content -Path ./package.json | ConvertFrom-Json).packageManager.split("@")[1]
if (git rev-parse --is-inside-work-tree) {
$GIT_REPO = $true
}
if ($GIT_REPO) {
$GIT_BRANCH = git branch --show-current
# Set ref to latest commit hash if HEAD is detached otherwise use branch name
if ($GIT_BRANCH.length -gt 0) {
$REF = git rev-parse --short HEAD
}
else {
$REF = (git show-ref --heads --abbrev $GIT_BRANCH).split(" ")[1]
}
}
else {
$REF = "dev"
}
# [β¬οΈ] Setup Node.js
if ($_RUN_STEP_NODE) {
$start_time = now
print_start_message "[β¬οΈ] Setup Node.js"
#Since windows is so easy with paths, we just check whether the correct version is installed and abort if not
Get-Command "node" | out-null
if (!$?) {
Write-Host "Can't find a valid node.js installation. Please check that node.exe is in the `$PATH variable and try again."
exit 1
}
else {
if (([version]$NODE_VERSION).Major -lt ((Get-Command "node").Version.Major)) {
Write-Host "Your node version is does not match the required version. Please update node before proceeding"
exit 1
}
}
print_end_message "[β¬οΈ] Setup Node.js" $start_time
}
# [β¬] setup yarn
if ($_RUN_STEP_YARN_SETUP) {
$start_time = now
print_start_message "[β¬] setup yarn"
enable_debugging
corepack enable
yarn set version $YARN_VERSION
disable_debugging
print_end_message "[β¬] setup yarn" $start_time
}
# [π] symlink yarn executable
if ($_RUN_STEP_YARN_SYMLINK) {
$start_time = now
print_start_message "[π] symlink yarn executable"
enable_debugging
$yarnPath = Get-ChildItem -Path .\.yarn\releases\ -Filter 'yarn-*.cjs' | Select-Object -First 1
if ($null -ne $yarnPath) {
New-Item -ItemType SymbolicLink -Path .\yarn -Value $yarnPath.FullName -Force | Out-Null
}
disable_debugging
print_end_message "[π] symlink yarn executable" $start_time
}
# [βΉ] print versions (node, yarn, git)
if ($_RUN_STEP_VERSIONS) {
$start_time = now
print_start_message "[βΉ] print versions (node, yarn, git)"
enable_debugging
Write-Host "node: $( node -v ) β yarn: $( yarn -v ) β git: $( git --version )"
disable_debugging
print_end_message "[βΉ] print versions (node, yarn, git)" $start_time
}
# [π±] yarn install
if ($_RUN_STEP_YARN_INSTALL) {
$start_time = now
print_start_message "[π±] yarn install"
enable_debugging
yarn install --immutable
disable_debugging
print_end_message "[π±] yarn install" $start_time
}
# [β¬] update browserslist
if ($_RUN_STEP_BROWSERSLIST) {
$start_time = now
print_start_message "[β¬] update browserslist"
enable_debugging
npx -y browserslist@latest --update-db
disable_debugging
print_end_message "[β¬] update browserslist" $start_time
}
# [π³] set env variables
if ($_RUN_STEP_ENV) {
$start_time = now
print_start_message "[π³] set env variables"
enable_debugging
$ref = $REF
$BRANCH = "dummy"
if ($ref -eq "refs/heads/master") {
$BRANCH = "stable"
}
elseif ($ref -eq "refs/heads/dev") {
$BRANCH = "beta"
}
elseif ($ref -like "refs/heads/*") {
$BRANCH = $ref -replace '^refs/heads/', ''
$BRANCH = $ref -replace '/', '-'
}
elseif ($ref -like "refs/pull/*") {
$BRANCH = $BRANCH -replace "refs/pull/", "pr"
$BRANCH = $BRANCH -replace "/merge", ""
$BRANCH = $ref -replace '/', '-'
}
$NODE_PATH = (Get-Item .).FullName+"\node_modules\.bin"
$env:Path += ";$NODE_PATH"
disable_debugging
print_end_message "[π³] set env variables" $start_time
}
# [β¬] update emojis
if ($_RUN_STEP_UPDATE_EMOJIS) {
$start_time = now
print_start_message "[β¬] update emojis"
enable_debugging
yarn ts-node scripts/utils/fetchEmojis.ts
disable_debugging
print_end_message "[β¬] update emojis" $start_time
}
# [π¨] format files not covered by ESLint
if ($_RUN_STEP_FORMAT) {
$start_time = now
print_start_message "[π¨] format files not covered by ESLint"
enable_debugging
yarn ts-node scripts/format.ts
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π¨] format files not covered by ESLint" $start_time
}
# [π¨] run ESLint
if ($_RUN_STEP_ESLINT) {
$start_time = now
print_start_message "[π¨] run ESLint"
enable_debugging
yarn eslint ./docs/.vuepress/ ./static/ ./prebuild/ ./build/ ./src/ ./scripts/ ./typings/ --ext .js, .ts, .vue, .md --no-error-on-unmatched-pattern --exit-on-fatal-error --report-unused-disable-directives --cache --cache-strategy content --fix
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π¨] run ESLint" $start_time
}
# [π¨] check TypeScript
if ($_RUN_STEP_TSC) {
$start_time = now
print_start_message "[π¨] check TypeScript"
enable_debugging
yarn tsc -b --pretty "./"
if (!$?) {
exit 1
}
yarn ts-node scripts/buildUserscript.ts
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π¨] check TypeScript" $start_time
}
# [π] build userscript
if ($_RUN_STEP_USERSCRIPT) {
$start_time = now
print_start_message "[π] build userscript"
enable_debugging
yarn tsc --pretty --project "src/tsconfig.userscript.json"
if (!$?) {
exit 1
}
yarn ts-node scripts/buildUserscript.ts
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π] build userscript" $start_time
}
# [π] build buildscript
if ($_RUN_STEP_BUILDSCRIPT) {
$start_time = now
print_start_message "[π] build buildscript"
enable_debugging
yarn ts-node scripts/createBuildScript.ts
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π] build buildscript" $start_time
}
# [π§] run prebuild
if ($_RUN_STEP_PREBUILD) {
$start_time = now
print_start_message "[π§] run prebuild"
enable_debugging
yarn ts-node prebuild/index.ts "$MODE" "$BRANCH" "π¦ branch label"
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π§] run prebuild" $start_time
}
# [π·] webpack
if ($_RUN_STEP_WEBPACK) {
$start_time = now
print_start_message "[π·] webpack"
enable_debugging
yarn ts-node build/index.ts --esModuleInterop "$MODE" "$BRANCH" "π¦ branch label"
if (!$?) {
exit 1
}
disable_debugging
print_end_message "[π·] webpack" $start_time
}
# [π] build docs
if ($_RUN_STEP_DOCS) {
$start_time = now
print_start_message "[π] build docs"
enable_debugging
<##Everthing else crashes on my machine
$rootProjectPath = Get-Location
Set-Location .\docs\.vuepress
.\node_modules\.bin\vuepress build $rootProjectPath\docs
if (!$?)
{
exit 1
}
Set-Location $rootProjectPath
New-Item -Path "./dist/docs" -ItemType Directory -Force
Remove-Item -Path "./dist/docs/*" -Recurse -Force
Copy-Item -Path "./docs/.vuepress/dist/*" -Destination "./dist/docs" -Recurse#>
Write-Host "Due to a incompatibilty with the package manager on windows, it is currently not possible to build the docs"
Write-Host "We're working on it. Sorry :("
disable_debugging
print_end_message "[π] build docs" $start_time
}
# [βΉοΈ] git diff
if ($_RUN_STEP_GIT_DIFF -and $GIT_REPO) {
$start_time = now
print_start_message "[βΉοΈ] git diff"
enable_debugging
git --no-pager diff --color-words
disable_debugging
print_end_message "[βΉοΈ] git diff" $start_time
}
# Start test server
if ($_RUN_STEP_SERVE) {
$start_time = now
print_start_message "Start test server"
enable_debugging
ws -d .\dist\ --https --port="$env:LSSM_PORT" --hostname localhost
disable_debugging
print_end_message "Start test server" $start_time
}
print_end_message "Total" "$total_start_time"
exit 0