From b06f6cc582fc5e171b3d4408d9d45d55b9ecea57 Mon Sep 17 00:00:00 2001 From: bookworm Date: Tue, 7 Apr 2026 00:01:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=93=E5=8C=85=E8=84=9A=E6=9C=AC=20?= =?UTF-8?q?+=20pkg=20=E8=B7=AF=E5=BE=84=E5=85=BC=E5=AE=B9=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit build.ps1: - auto-setup.ps1 → dist/Bookworm-Setup.exe (PS2EXE, 无控制台窗口) - gen-authcode.js → dist/gen-authcode.exe (pkg, 内嵌 Node.js) - 自动安装依赖 (PS2EXE / pkg),输出大小提示 gen-authcode.js: - SCRIPT_DIR 兼容 pkg 打包环境 (process.pkg → process.execPath) .gitignore: - 新增 dist/ (EXE 输出目录,不提交二进制) Co-Authored-By: Claude Opus 4.6 (1M context) --- .gitignore | 1 + build.ps1 | 150 ++++++++++++++++++++++++++++++++++++++++++++++++ gen-authcode.js | 5 +- 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 build.ps1 diff --git a/.gitignore b/.gitignore index e66f6cb..4ce37a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ secrets.txt 管理员SOP.html +dist/ diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..96464b6 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,150 @@ +<# +.SYNOPSIS + Bookworm Portable — 打包工具 (管理员使用) +.DESCRIPTION + 将 auto-setup.ps1 打包为 Bookworm-Setup.exe (PS2EXE) + 将 gen-authcode.js 打包为 gen-authcode.exe (pkg) + 输出到 dist\ 目录 +.USAGE + .\build.ps1 # 打包两个 + .\build.ps1 -Setup # 只打包用户安装器 + .\build.ps1 -Admin # 只打包管理员工具 +#> +param( + [switch]$Setup, # 只打 Bookworm-Setup.exe + [switch]$Admin # 只打 gen-authcode.exe +) + +$ErrorActionPreference = "Stop" +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$DistDir = Join-Path $ScriptDir "dist" + +# 默认两个都打 +$buildSetup = $Setup -or (-not $Setup -and -not $Admin) +$buildAdmin = $Admin -or (-not $Setup -and -not $Admin) + +function Write-Step($msg) { + Write-Host "" + Write-Host " ── $msg" -ForegroundColor Cyan +} +function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } +function Write-Warn($msg) { Write-Host " [!] $msg" -ForegroundColor Yellow } +function Write-Fail($msg) { Write-Host " [!!] $msg" -ForegroundColor Red } + +Write-Host "" +Write-Host " +------------------------------------------+" -ForegroundColor Cyan +Write-Host " | Bookworm Portable — Build Script |" -ForegroundColor Cyan +Write-Host " +------------------------------------------+" -ForegroundColor Cyan + +if (-not (Test-Path $DistDir)) { + New-Item -ItemType Directory $DistDir | Out-Null + Write-OK "创建 dist\ 目录" +} + +# ════════════════════════════════════════════════════════ +# 1. Bookworm-Setup.exe (auto-setup.ps1 → PS2EXE) +# ════════════════════════════════════════════════════════ +if ($buildSetup) { + Write-Step "打包 Bookworm-Setup.exe (PS2EXE)" + + # 安装/检查 PS2EXE + if (-not (Get-Command Invoke-ps2exe -ErrorAction SilentlyContinue)) { + Write-Warn "PS2EXE 未安装,正在安装..." + Install-Module ps2exe -Scope CurrentUser -Force -AllowClobber + Import-Module ps2exe + } + + $inputPs1 = Join-Path $ScriptDir "auto-setup.ps1" + $outputExe = Join-Path $DistDir "Bookworm-Setup.exe" + + if (-not (Test-Path $inputPs1)) { + Write-Fail "找不到 auto-setup.ps1" + exit 1 + } + + Write-Host " 输入: $inputPs1" -ForegroundColor Gray + Write-Host " 输出: $outputExe" -ForegroundColor Gray + + Invoke-ps2exe ` + -InputFile $inputPs1 ` + -OutputFile $outputExe ` + -Title "Bookworm Portable Setup" ` + -Description "Bookworm Smart Assistant 安装向导" ` + -Company "Bookworm" ` + -Version "1.5.0.0" ` + -NoConsole # 纯 GUI,不弹黑色控制台窗口 + + if (Test-Path $outputExe) { + $sizeKB = [math]::Round((Get-Item $outputExe).Length / 1KB) + Write-OK "Bookworm-Setup.exe 打包完成 (${sizeKB} KB)" + } else { + Write-Fail "Bookworm-Setup.exe 打包失败" + exit 1 + } +} + +# ════════════════════════════════════════════════════════ +# 2. gen-authcode.exe (gen-authcode.js → pkg) +# ════════════════════════════════════════════════════════ +if ($buildAdmin) { + Write-Step "打包 gen-authcode.exe (pkg)" + + # 检查 Node.js + if (-not (Get-Command node -ErrorAction SilentlyContinue)) { + Write-Fail "需要 Node.js,请先安装: https://nodejs.org" + exit 1 + } + + # 安装/检查 pkg + $pkgCmd = Get-Command pkg -ErrorAction SilentlyContinue + if (-not $pkgCmd) { + Write-Warn "pkg 未安装,正在安装..." + npm install -g pkg + # 刷新 PATH + $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + + [System.Environment]::GetEnvironmentVariable("Path","User") + } + + $inputJs = Join-Path $ScriptDir "gen-authcode.js" + $outputExe = Join-Path $DistDir "gen-authcode.exe" + + if (-not (Test-Path $inputJs)) { + Write-Fail "找不到 gen-authcode.js" + exit 1 + } + + Write-Host " 输入: $inputJs" -ForegroundColor Gray + Write-Host " 输出: $outputExe" -ForegroundColor Gray + Write-Host " (内嵌 Node.js 运行时,首次打包约 1 分钟)" -ForegroundColor Gray + + pkg $inputJs --targets node18-win-x64 --output $outputExe 2>&1 | + ForEach-Object { Write-Host " $_" -ForegroundColor Gray } + + if (Test-Path $outputExe) { + $sizeMB = [math]::Round((Get-Item $outputExe).Length / 1MB, 1) + Write-OK "gen-authcode.exe 打包完成 (${sizeMB} MB)" + } else { + Write-Fail "gen-authcode.exe 打包失败" + exit 1 + } +} + +# ════════════════════════════════════════════════════════ +# 完成 +# ════════════════════════════════════════════════════════ +Write-Host "" +Write-Host " ============================================" -ForegroundColor Green +Write-Host " 打包完成!输出目录: dist\" -ForegroundColor Green +Write-Host " ============================================" -ForegroundColor Green +Write-Host "" + +Get-ChildItem $DistDir | ForEach-Object { + $sizeMB = [math]::Round($_.Length / 1MB, 1) + Write-Host " $($_.Name.PadRight(30)) ${sizeMB} MB" -ForegroundColor White +} + +Write-Host "" +Write-Host " 分发说明:" -ForegroundColor Gray +Write-Host " Bookworm-Setup.exe → 放入 bookworm-boot 仓库根目录,发给用户" -ForegroundColor Gray +Write-Host " gen-authcode.exe → 管理员本机使用,勿对外分发" -ForegroundColor Gray +Write-Host "" diff --git a/gen-authcode.js b/gen-authcode.js index a2158e4..b3c08e9 100644 --- a/gen-authcode.js +++ b/gen-authcode.js @@ -76,7 +76,10 @@ for (let i = 1; i < rawArgs.length; i++) { else if (!a.startsWith('-')) { secretsTxtArg = a; } } -const SCRIPT_DIR = path.dirname(path.resolve(__filename)); +// pkg 打包后 __filename 指向虚拟快照路径,需用 process.execPath 获取真实目录 +const SCRIPT_DIR = path.dirname( + typeof process.pkg !== 'undefined' ? process.execPath : path.resolve(__filename) +); const SECRETS_TXT = secretsTxtArg || path.join(SCRIPT_DIR, 'secrets.txt'); if (!fs.existsSync(SECRETS_TXT)) {