param( [ValidateSet('clean', 'prepare', 'app', 'win', 'package')] [string]$Stage = 'package' ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $MyInvocation.MyCommand.Path $tempRootName = [string]([char]0x4E34) + [char]0x65F6 $tempPackagingDir = Join-Path (Join-Path $root $tempRootName) 'packaging' $timestamp = Get-Date -Format 'yyyyMMdd-HHmmss' $logPath = Join-Path $tempPackagingDir ("build-$timestamp.log") function Ensure-Dir([string]$PathValue) { if (-not (Test-Path $PathValue)) { New-Item -ItemType Directory -Path $PathValue -Force | Out-Null } } function Write-Log([string]$Message) { Ensure-Dir $tempPackagingDir $line = "[{0}] {1}" -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $Message $line | Tee-Object -FilePath $logPath -Append } function Invoke-Step([string]$Name, [scriptblock]$Action) { Write-Log "START $Name" & $Action Write-Log "DONE $Name" } function Invoke-Npm([string[]]$Args) { & npm.cmd @Args if ($LASTEXITCODE -ne 0) { throw "npm $($Args -join ' ') failed with exit code $LASTEXITCODE" } } function Invoke-Builder([string[]]$Args) { $builderPath = Join-Path $root 'node_modules\.bin\electron-builder.cmd' if (-not (Test-Path $builderPath)) { throw 'electron-builder.cmd not found, run npm install first' } & $builderPath @Args if ($LASTEXITCODE -ne 0) { throw "electron-builder $($Args -join ' ') failed with exit code $LASTEXITCODE" } } function Ensure-AppExecutable() { $appOutDir = Join-Path $root 'dist\win-unpacked' $targetExe = Join-Path $appOutDir 'printServer.exe' $fallbackSource = Join-Path $root 'node_modules\electron\dist\electron.exe' $runtimeMarker = Join-Path $appOutDir 'resources.pak' if (Test-Path $targetExe) { return $true } if ((Test-Path $appOutDir) -and (Test-Path $runtimeMarker) -and (Test-Path $fallbackSource)) { Copy-Item -Path $fallbackSource -Destination $targetExe -Force Write-Log 'FALLBACK app executable copied from node_modules/electron/dist/electron.exe' return $true } return $false } function Ensure-PackagedResources() { $appOutDir = Join-Path $root 'dist\win-unpacked' $resourcesDir = Join-Path $appOutDir 'resources' Ensure-Dir $resourcesDir $configTemplateSource = Join-Path $root 'config.packaged.json' $configTargetPath = Join-Path $resourcesDir 'config.json' if (Test-Path $configTemplateSource) { Copy-Item -Path $configTemplateSource -Destination $configTargetPath -Force } foreach ($fileName in @('printer.ps1')) { $sourcePath = Join-Path $root $fileName $targetPath = Join-Path $resourcesDir $fileName if (Test-Path $sourcePath) { Copy-Item -Path $sourcePath -Destination $targetPath -Force } } foreach ($dirName in @('daemon')) { $sourcePath = Join-Path $root $dirName $targetPath = Join-Path $resourcesDir $dirName if (Test-Path $sourcePath) { if (Test-Path $targetPath) { Remove-Item -Path $targetPath -Recurse -Force } Copy-Item -Path $sourcePath -Destination $targetPath -Recurse -Force } } } function Clean-Stage { foreach ($target in @('dist', 'release')) { $fullPath = Join-Path $root $target if (Test-Path $fullPath) { Remove-Item -Path $fullPath -Recurse -Force } } if (Test-Path $tempPackagingDir) { Remove-Item -Path $tempPackagingDir -Recurse -Force } } function Prepare-Stage { Ensure-Dir $tempPackagingDir $requiredItems = @( 'index.js', 'config.packaged.json', 'printer.ps1', 'views\index.html', 'electron\main.cjs' ) $missing = @() foreach ($item in $requiredItems) { $target = Join-Path $root $item if (-not (Test-Path $target)) { $missing += $item } } if ($missing.Count -gt 0) { throw "missing required packaging inputs: $($missing -join ', ')" } $manifest = [PSCustomObject]@{ version = (Get-Content (Join-Path $root 'package.json') -Raw | ConvertFrom-Json).version checkedAt = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss') requiredItems = $requiredItems } $manifest | ConvertTo-Json -Depth 4 | Set-Content -Path (Join-Path $tempPackagingDir 'preflight-check.json') -Encoding UTF8 } function App-Stage { $builderFailed = $false try { Invoke-Builder @('--dir', '--win') } catch { $builderFailed = $true Write-Log ("WARN app stage failed before completion: " + $_.Exception.Message) } if ($builderFailed -and (Ensure-AppExecutable)) { Ensure-PackagedResources Write-Log 'APP stage fallback completed with unpacked executable workaround' return } if ($builderFailed) { throw 'app stage failed and no usable unpacked executable was produced' } Ensure-PackagedResources Write-Log 'APP stage resources synchronized' } function Invoke-OfflineInstaller { $offlineBuilderPath = Join-Path $root 'electron\build-offline-installer.ps1' & powershell.exe -ExecutionPolicy Bypass -File $offlineBuilderPath if ($LASTEXITCODE -ne 0) { throw "offline installer build failed with exit code $LASTEXITCODE" } } function Win-Stage { App-Stage $nsisBuilt = $false try { & node (Join-Path $root 'electron\build-installer.mjs') if ($LASTEXITCODE -ne 0) { throw "installer build failed with exit code $LASTEXITCODE" } $nsisBuilt = $true Write-Log 'NSIS installer build completed' } catch { Write-Log ("WARN NSIS installer unavailable: " + $_.Exception.Message) } if (-not $nsisBuilt) { Invoke-OfflineInstaller Write-Log 'OFFLINE installer build completed with IExpress fallback' } } switch ($Stage) { 'clean' { Invoke-Step 'clean' { Clean-Stage } } 'prepare' { Invoke-Step 'prepare' { Prepare-Stage } } 'app' { Invoke-Step 'prepare' { Prepare-Stage } Invoke-Step 'app' { App-Stage } } 'win' { Invoke-Step 'prepare' { Prepare-Stage } Invoke-Step 'win' { Win-Stage } } 'package' { Invoke-Step 'clean' { Clean-Stage } Invoke-Step 'prepare' { Prepare-Stage } Invoke-Step 'win' { Win-Stage } } }