-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.ps1
503 lines (394 loc) · 12 KB
/
execute.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
<#
.SYNOPSIS
Custom commands for the project
.DESCRIPTION
Executes custom ommands for ease of use
.PARAMETER Help
Outputs the detailed help info of the command
.PARAMETER Examples
Only works if `-Help` switch is on.
Includes examples in the help info.
.PARAMETER Server
Starts the development server if other flags passed and restarts if there are file changes.
.PARAMETER Normal
Works only if `-Server` is on.
Starts the server normally.
.PARAMETER Prod
Works only if `-Server` is on.
Starts the server for production purposes.
.PARAMETER Routes
Works only if `-Server` is on.
Lists the routes available in the server.
.PARAMETER Push
Pushes the current branch to remote.
.PARAMETER Pull
Pulls the all branches from remote and prunes remotely-deleted branches.
.PARAMETER Remote
Only works if one of `-Push` or `-Pull` switches is on.
Specifies which remote to push or pull.
.PARAMETER Test
Switch to runs tests.
.PARAMETER SuiteName
Only required if `-Test` switch is on.
It contains the name of test suite to run.
.PARAMETER Compile
Only works if `-Test` and `-SuiteName` switch is on.
Compiles all the possible paths of the test files to a single test.
.PARAMETER Path
Only works if `-Test` switch is on.
Limits the files to test base from regular expression.
.PARAMETER Clear
Only works if `-Test` switch is on.
Clears the cache of the specific suite.
.PARAMETER Watch
Only works if `-Test` switch is on.
This watches the files included on specified tests.
.PARAMETER Database
Switch to run database operations.
.PARAMETER Initialize
Only works if `-Database` switch is on.
Switch to migrate all database tables for the first time.
.PARAMETER Upgrade
Only works if `-Database` switch is on.
Switch to migrate all database tables.
.PARAMETER Downgrade
Only works if `-Database` switch is on.
Switch to undo some migration of tables.
.PARAMETER Reset
Only works if `-Database` switch is on.
Switch to redo all migration of tables from the start.
.PARAMETER Seed
Only works if `-Database` switch is on.
Seeds the database with new values.
.PARAMETER Unseed
Only works if `-Database` switch is on.
Removes seeded values from last seeding.
.PARAMETER Reseed
Only works if `-Database` switch is on.
Redo the seeding of the database.
.PARAMETER Log
Generates changelog.
.INPUTS
All inputs are done through arguments.
.OUTPUTS
Depends on the command ran.
.EXAMPLE
PS> ./execute -help
Show the detailed help.
.EXAMPLE
PS> ./execute -test unit:front -watch
Runs the front-end unit tests.
.EXAMPLE
PS> ./execute -test -suitenames unit:server -watch
Runs and watches the server unit tests.
#>
Param(
[Parameter(ParameterSetName="Help", Position=0)]
[switch]
$Help,
[Parameter(ParameterSetName="Help", Position=1)]
[switch]
$Examples,
[Parameter(ParameterSetName="Server", Position=0)]
[switch]
$Server,
[Parameter(ParameterSetName="Server", Position=1)]
[switch]
$Normal,
[Parameter(ParameterSetName="Server", Position=1)]
[switch]
$Prod,
[Parameter(ParameterSetName="Server", Position=1)]
[switch]
$Routes,
[Parameter(ParameterSetName="Server", Position=1)]
[switch]
$MakeTestPDF,
[Parameter(ParameterSetName="PushRepo", Position=0)]
[switch]
$Push,
[Parameter(ParameterSetName="PullRepo", Position=0)]
[switch]
$Pull,
[Parameter(ParameterSetName="PushRepo", Position=1)]
[Parameter(ParameterSetName="PullRepo", Position=1)]
[string]
$Remote = "",
[Parameter(ParameterSetName="Test", Position=0)]
[switch]
$Test,
[Parameter(ParameterSetName="Test", Mandatory, Position=1)]
[ValidateSet(
"unit:share",
"unit:front",
"unit:component",
"intg:front",
"unit_ci:front",
"unit:back",
"unit:route",
"unit:server",
"unit:database",
"intg:back",
"unit_ci:back"
)]
[string]
$SuiteName,
[Parameter(ParameterSetName="Test", Position=2)]
[string]
$Regex = "",
[Parameter(ParameterSetName="Test", Position=3)]
[switch]
$Clear,
[Parameter(ParameterSetName="Test", Position=4)]
[switch]
$Watch,
[Parameter(ParameterSetName="Test", Position=5)]
[switch]
$Compile,
[Parameter(ParameterSetName="Database", Position=0)]
[switch]
$Database,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Initialize,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Upgrade,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Downgrade,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Reset,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Seed,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Unseed,
[Parameter(ParameterSetName="Database", Position=1)]
[switch]
$Reseed,
[Parameter(ParameterSetName="Log", Position=0)]
[switch]
$Log
)
if ($Help) {
Get-Help $PSCommandPath -detailed
if ($Examples) {
Get-Help $PSCommandPath -examples
}
}
if ($Server) {
if ($Normal) {
& npx cross-env NODE_ENV=dev ts-node ./server
} elseif ($Prod) {
& npx cross-env NODE_ENV=production npx ts-node ./server
} elseif ($Routes) {
& npx ts-node ./server/cli/list_routes.ts
} elseif ($MakeTestPDF) {
& npx ts-node ./server/cli/make_test_pdf.ts
} else {
$command = "pwsh ./execute.ps1 -Server -Normal"
& npx nodemon --watch .env --watch server --watch routes --watch database --watch common_back-end --ext ts --ignore "*.spec.ts" --exec "$command"
}
}
if ($Test) {
$type, $name = $SuiteName.Split(":")
$environment = $type
$configuration = "jest.$($name).$($type).config.json"
if ($type -eq "unit") {
$configuration = "jest.$($name).config.json"
} elseif ($type.StartsWith("unit")) {
$environment = "unit"
}
if ($Compile) {
$testConfiguration = Get-Content $configuration | ConvertFrom-Json
$cachedPath = "t/cache/compiled"
if (!(Test-Path -PathType Container $cachedPath)) {
New-Item -ItemType Directory -Path $cachedPath
}
$possiblePaths = @()
foreach($testRegex in $testConfiguration.testRegex) {
$usablePath = @()
$pathNames = $testRegex.Split("/")
foreach($pathName in $pathNames) {
if ($pathName -match "^[a-zA-Z-_.]+$") {
$usablePath += $pathName
} else {
break
}
}
$usablePath = $usablePath -join "/"
$possiblePaths += "$usablePath/*"
}
# Write-Output ($possiblePaths -join ", ")
$possibleFiles = @()
foreach($path in $possiblePaths) {
$items = Get-ChildItem -Recurse $path
foreach($item in $items) {
if ($item -match ".+.spec.ts$") {
$possibleFiles += $item
}
}
}
$possibleImports = ""
foreach($file in $possibleFiles) {
$escapedfile = ([string]$file).Replace("$PSScriptRoot", "../../..").Replace("\", "/")
$possibleImports += "import `"$escapedfile`"`n"
}
Write-Output ($possibleImports)
$compiledFile = "$cachedPath/$type.$name.spec.ts"
$compiledConfiguration = "hidden.$type.$name.config.json"
Set-Content -Path $compiledFile -Value $possibleImports
$testConfiguration.testRegex = @($compiledFile)
$testConfiguration = ConvertTo-Json $testConfiguration
$testConfiguration = $testConfiguration.replace(" ", " ")
Set-Content -Path $compiledConfiguration -Value $testConfiguration
$configuration = $compiledConfiguration
}
$regexFlag = '""'
if ($Regex -ne "") {
$regexFlag = '"'+$Regex+'"'
}
if ($Clear) {
$cacheDirectory = "t/cache/$($SuiteName.Replace(":", "_"))"
Get-Item $cacheDirectory/* | ForEach-Object $_ {
if ($_.Name -ne ".gitignore") {
$NodeToDelete = "./$($cacheDirectory)/$($_.Name)"
Write-Output "Deleting $NodeToDelete"
Remove-Item $NodeToDelete -Recurse
}
}
}
if ($Watch) {
if ($regexFlag -eq '""') {
Write-Output "npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} $($watchFlag) --watch --detectOpenHandles"
& npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --watch --detectOpenHandles
} else {
Write-Output "npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --testRegex $($regexFlag) --watch --detectOpenHandles"
& npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --testRegex $($regexFlag) --watch --detectOpenHandles
}
} elseif ($regexFlag -eq '""') {
Write-Output "npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --detectOpenHandles"
& npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --detectOpenHandles
} else {
Write-Output "npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --testRegex $($regexFlag) --detectOpenHandles"
& npx cross-env NODE_ENV=$($environment)_test jest -c ${configuration} --testRegex $($regexFlag) --detectOpenHandles
}
}
if ($Push) {
$currentBranch = & git branch --show-current
if ($Remote -eq "") {
$Remote = "origin"
}
& git push -u $($Remote) $($currentBranch)
}
if ($Pull) {
$outputFile = "hidden_cache_remote.txt"
$command = ""
$possibleOutput = ""
$currentBranch = & git branch --show-current
if ($Remote -eq "") {
& git pull --prune
& git fetch
} else {
& git pull --prune $($Remote) $($currentBranch)
& git fetch --prune $($Remote)
}
$possibleBranches = & git branch -l --format='%(refname:lstrip=2)'
$possibleBranches = $possibleBranches -Split "`n"
foreach($branch in $possibleBranches) {
if ($branch -ne "master") {
& git branch -d $branch.trim()
}
}
}
if ($Database) {
if ($Initialize) {
& npx sequelize-cli db:create
& ./execute -Database -Upgrade
}
if ($Upgrade) {
& npx sequelize-cli db:migrate
}
if ($Downgrade) {
& npx sequelize-cli db:migrate:undo
}
if ($Reset) {
& npx sequelize-cli db:drop
& ./execute -Database -Initialize
}
if ($Seed) {
& npx sequelize-cli db:seed:all
}
if ($Unseed) {
& npx sequelize-cli db:seed:undo
}
if ($Reseed) {
& npx sequelize-cli db:seed:undo:all
& npx sequelize-cli db:seed:all
}
}
if ($Log) {
$packageConfiguration = Get-Content package.json | ConvertFrom-Json
$version = $packageConfiguration.version.trimEnd("-dev")
$previousVersion = [int32]($version.Split(".")[1]) - 1
$nextVersion = [int32]($version.Split(".")[1]) + 1
$packageConfiguration.version = "$([string]$version)"
$newPackageConfiguration = ConvertTo-Json $packageConfiguration
$newPackageConfiguration = $newPackageConfiguration.replace(" ", " ")
Set-Content -Path package.json -Value $newPackageConfiguration
$contents = & npx changelogen --from=$("v1.$previousVersion.0")
$contents = $contents.Trim("`n")
$contents = $contents -Split "`n"
$cleanedContents = @("# Changelog", "", "## v$version", "")
$lastLine = "###"
foreach($line in $contents) {
$line = $line.
Trim().
Replace("≡ƒÜÇ", "🚀").
Replace("ΓÜá∩╕Å", "⚠️").
Replace("≡ƒ⌐╣", "🩹").
Replace("≡ƒÆà", "💅").
Replace("≡ƒôû", "📖").
Replace("≡ƒÅí", "🏡").
Replace("Γ£à", "✅").
Replace("≡ƒÄ¿", "🎨").
Replace("≡ƒñû", "🤖").
Replace("≡ƒùÆ∩╕Å", "🗒️").
Replace("Γ¥ñ∩╕Å", "❤️").
Replace("≡ƒæô", "👓").
Replace("≡ƒîè", "🌊").
Replace("≡ƒö⌐", "🔩").
Replace("≡ƒîÉ", "🌐").
Replace("≡ƒöª", "🔦").
Replace("≡ƒªá", "🦠").
Replace("≡ƒöÑ", "🔥").
Replace("≡ƒò╖", "🕷")
if (($line -eq "") -and $lastLine.StartsWith("###")) {
$lastLine = $line
} elseif (($line -eq "" -and $lastLine -ne "") -or $line.StartsWith("-")) {
$lastLine = $line
$cleanedContents += $line
} elseif ($line.StartsWith("###")) {
$lastLine = $line
$cleanedContents += $line
}
}
$cleanedContents = $cleanedContents[0..($cleanedContents.length-4)]
$cleanedContents = $cleanedContents -join "`n"
Rename-Item -Path ./changelogs/CHANGELOG.md -NewName CHANGELOG_v1.$($previousVersion).md
Set-Content -Path changelogs/CHANGELOG.md -Value $cleanedContents
& npm install
& git add .
& git commit -m $("Update current version to v$version")
& git tag -s $("v$version")
$packageConfiguration.version = "1.$nextVersion.0-dev"
$newPackageConfiguration = ConvertTo-Json $packageConfiguration
$newPackageConfiguration = $newPackageConfiguration.replace(" ", " ")
Set-Content -Path package.json -Value $newPackageConfiguration
& npm install
& git add .
& git commit -m $("Update current version to v1.$nextVersion.0-dev")
}