feat: 打包脚本 + pkg 路径兼容修复

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) <noreply@anthropic.com>
This commit is contained in:
bookworm 2026-04-07 00:01:46 +08:00
parent 5141b7882b
commit b06f6cc582
3 changed files with 155 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
secrets.txt secrets.txt
管理员SOP.html 管理员SOP.html
dist/

150
build.ps1 Normal file
View File

@ -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 ""

View File

@ -76,7 +76,10 @@ for (let i = 1; i < rawArgs.length; i++) {
else if (!a.startsWith('-')) { secretsTxtArg = a; } 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'); const SECRETS_TXT = secretsTxtArg || path.join(SCRIPT_DIR, 'secrets.txt');
if (!fs.existsSync(SECRETS_TXT)) { if (!fs.existsSync(SECRETS_TXT)) {