$ErrorActionPreference = "Stop"

# Run vue-tsc
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = "C:\Program Files\nodejs\node.exe"
$psi.Arguments = "node_modules\vue-tsc\bin\vue-tsc.js"
$psi.WorkingDirectory = "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent"
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
$psi.EnvironmentVariables["NODE_PATH"] = "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\node_modules"
$proc = [System.Diagnostics.Process]::Start($psi)
$stdout = $proc.StandardOutput.ReadToEnd()
$stderr = $proc.StandardError.ReadToEnd()
$proc.WaitForExit()
$exitCode = $proc.ExitCode

$stdout | Out-File -FilePath "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\build-output.txt" -Encoding UTF8
$stderr | Out-File -FilePath "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\build-output.txt" -Append -Encoding UTF8
"vue-tsc exit: $exitCode" | Out-File -FilePath "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\build-output.txt" -Append -Encoding UTF8

if ($exitCode -ne 0) {
    Write-Host "vue-tsc failed"
    exit $exitCode
}

# Run vite build
$psi2 = New-Object System.Diagnostics.ProcessStartInfo
$psi2.FileName = "C:\Program Files\nodejs\node.exe"
$psi2.Arguments = "node_modules\vite\bin\vite.js build"
$psi2.WorkingDirectory = "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent"
$psi2.UseShellExecute = $false
$psi2.RedirectStandardOutput = $true
$psi2.RedirectStandardError = $true
$psi2.EnvironmentVariables["NODE_PATH"] = "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\node_modules"
$proc2 = [System.Diagnostics.Process]::Start($psi2)
$stdout2 = $proc2.StandardOutput.ReadToEnd()
$stderr2 = $proc2.StandardError.ReadToEnd()
$proc2.WaitForExit()
$exitCode2 = $proc2.ExitCode

$stdout2 | Out-File -FilePath "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\build-output.txt" -Append -Encoding UTF8
$stderr2 | Out-File -FilePath "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\build-output.txt" -Append -Encoding UTF8
"vite build exit: $exitCode2" | Out-File -FilePath "d:\资料\03-项目开发\wecom_it_smart_desk\frontend-agent\build-output.txt" -Append -Encoding UTF8