Repository navigation
Expand file tree
/
Copy pathreformat.ps1
More file actions
75 lines (57 loc) · 2.41 KB
/
Copy pathreformat.ps1
File metadata and controls
75 lines (57 loc) · 2.41 KB
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
#Requires -Version 5.1
$ErrorActionPreference = "Stop"
$ProjectDir = if ($PSScriptRoot) { $PSScriptRoot } else { (Get-Location).Path }
$FrontendDir = Join-Path $ProjectDir "frontend"
$BackendDir = Join-Path $ProjectDir "backend"
$EnterpriseFrontendSrcDir = Join-Path $ProjectDir "enterprise\frontend\src"
$EnterpriseBackendDir = Join-Path $ProjectDir "enterprise\backend"
# External commands do not always throw in PowerShell when they fail, so this
# helper stops the script on a non-zero exit code and avoids false success.
function Invoke-CheckedCommand {
param(
[Parameter(Mandatory = $true)]
[string] $FilePath,
[string[]] $ArgumentList = @(),
[string] $CommandName = ""
)
& $FilePath @ArgumentList
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
if ([string]::IsNullOrEmpty($CommandName)) {
$CommandName = "$FilePath $($ArgumentList -join ' ')"
}
throw "$CommandName failed with exit code $exitCode."
}
}
function Invoke-InDirectory {
param(
[Parameter(Mandatory = $true)]
[string] $Directory,
[Parameter(Mandatory = $true)]
[scriptblock] $ScriptBlock
)
if (-not (Test-Path -LiteralPath $Directory -PathType Container)) {
throw "Directory not found: $Directory"
}
Push-Location -LiteralPath $Directory
try {
& $ScriptBlock
}
finally {
Pop-Location
}
}
Invoke-InDirectory -Directory $FrontendDir -ScriptBlock {
Write-Host "Formatting frontend..." -ForegroundColor Cyan
Invoke-CheckedCommand -FilePath "pnpm" -ArgumentList @("exec", "prettier", ".", "--write") -CommandName "pnpm exec prettier . --write"
Write-Host "`nFormatting enterprise frontend..." -ForegroundColor Cyan
Invoke-CheckedCommand -FilePath "pnpm" -ArgumentList @("exec", "prettier", $EnterpriseFrontendSrcDir, "--write") -CommandName "pnpm exec prettier enterprise frontend --write"
}
Invoke-InDirectory -Directory $BackendDir -ScriptBlock {
Write-Host "`nFormatting backend..." -ForegroundColor Cyan
Invoke-CheckedCommand -FilePath "ruff" -ArgumentList @("format", ".") -CommandName "ruff format backend"
}
Invoke-InDirectory -Directory $EnterpriseBackendDir -ScriptBlock {
Write-Host "`nFormatting enterprise backend..." -ForegroundColor Cyan
Invoke-CheckedCommand -FilePath "ruff" -ArgumentList @("format", ".") -CommandName "ruff format enterprise backend"
}