- 按前缀归类: dev:*(h5/adb/electron)、build:*(h5/app/exe/apk)、android:*(prepare/pack) - 改名: dev:h5:electron→dev:h5、debug:electron→dev:electron、build:h5:electron→build:h5、build:app-plus→build:app - 新增快捷入口: dev→dev:h5、build→build:h5、build:apk(编译+打包+发布一键) - build:apk 不重复调 prepare.cjs(pack.ps1 内部已注入) - 同步更新所有内部引用(dev-electron/build-exe/dev-adb/pack.ps1 提示)与文档
114 lines
4.7 KiB
JavaScript
114 lines
4.7 KiB
JavaScript
/**
|
|
* 一键打包 Windows exe 并发布:
|
|
* 1. 编译前端资源(uni build --mode electron)
|
|
* 2. electron-builder 生成 NSIS 安装包,输出 release/windows/
|
|
* 3. 可选:读取 config/deploy.json 的 windows 段,scp 上传安装包与 latest.json
|
|
* 用法:node scripts/build-exe.cjs [--no-upload]
|
|
*/
|
|
const { spawnSync } = require('child_process')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const crypto = require('crypto')
|
|
|
|
const ROOT = path.resolve(__dirname, '..')
|
|
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
const npxCmd = process.platform === 'win32' ? 'npx.cmd' : 'npx'
|
|
const useShell = process.platform === 'win32'
|
|
const noUpload = process.argv.includes('--no-upload')
|
|
|
|
function run(cmd, args) {
|
|
const command = `${cmd} ${args.join(' ')}`
|
|
console.log(`\n> ${command}`)
|
|
const result = spawnSync(command, { stdio: 'inherit', shell: useShell })
|
|
if (result.status !== 0) {
|
|
console.error(`\n[x] 命令失败(退出码 ${result.status}): ${command}`)
|
|
process.exit(result.status || 1)
|
|
}
|
|
}
|
|
|
|
console.log('[1/2] 编译前端资源 -> dist/build/h5')
|
|
run(npmCmd, ['run', 'build:h5'])
|
|
|
|
console.log('\n[2/2] electron-builder 打包 exe -> release/windows/')
|
|
run(npxCmd, ['electron-builder', '--config', 'electron-builder.config.cjs'])
|
|
|
|
// ---------- 3. 发布上传(config/deploy.json 的 windows 段) ----------
|
|
if (noUpload) {
|
|
console.log('\n[跳过] --no-upload 已指定,跳过发布上传')
|
|
} else {
|
|
const deployPath = path.join(ROOT, 'config', 'deploy.json')
|
|
if (!fs.existsSync(deployPath)) {
|
|
console.log('\n[跳过] 未找到 config/deploy.json,跳过发布上传(复制 config/deploy.example.json 并填写)')
|
|
} else {
|
|
let deploy
|
|
try {
|
|
deploy = JSON.parse(fs.readFileSync(deployPath, 'utf-8'))
|
|
} catch (error) {
|
|
console.error(`\n[x] config/deploy.json 解析失败: ${error.message}`)
|
|
process.exit(1)
|
|
}
|
|
const win = deploy.windows
|
|
if (!win || !win.host || !win.user || !win.remoteDir || !win.downloadUrlPrefix) {
|
|
console.log('\n[跳过] config/deploy.json 缺少 windows 段或字段不全,跳过发布上传')
|
|
} else {
|
|
const keyPath = win.keyPath || ''
|
|
if (keyPath && !fs.existsSync(keyPath)) {
|
|
console.error(`\n[x] SSH 密钥不存在: ${keyPath}`)
|
|
process.exit(1)
|
|
}
|
|
const port = String(win.port || 22)
|
|
const remoteDir = String(win.remoteDir).replace(/\/$/, '')
|
|
const urlPrefix = String(win.downloadUrlPrefix).replace(/\/?$/, '/')
|
|
const latestName = win.latestJsonName || 'latest.json'
|
|
|
|
// 定位 NSIS 安装包(按当前版本精确匹配,避免目录残留旧包被误选)
|
|
const outDir = path.join(ROOT, 'release', 'windows')
|
|
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf-8'))
|
|
const version = pkg.version
|
|
const productName = 'WMS工作台'
|
|
const expectedName = `${productName}-${version}-setup.exe`
|
|
const setupExe = fs.existsSync(path.join(outDir, expectedName))
|
|
? expectedName
|
|
: fs.readdirSync(outDir).find(
|
|
(file) => file.endsWith('.exe') && file.includes('setup') && !file.includes('__uninstaller')
|
|
)
|
|
if (!setupExe) {
|
|
console.error('\\n[x] 未在 release/windows 找到安装包')
|
|
process.exit(1)
|
|
}
|
|
const exePath = path.join(outDir, setupExe)
|
|
const sha256 = crypto.createHash('sha256').update(fs.readFileSync(exePath)).digest('hex')
|
|
const latest = {
|
|
version,
|
|
file: setupExe,
|
|
url: `${urlPrefix}${setupExe}`,
|
|
sha256,
|
|
publishedAtUtc: new Date().toISOString(),
|
|
}
|
|
const latestPath = path.join(outDir, latestName)
|
|
fs.writeFileSync(latestPath, JSON.stringify(latest, null, 2) + '\n', 'utf-8')
|
|
|
|
console.log(`\n[3/3] 发布上传 -> ${win.user}@${win.host}:${remoteDir}/ ...`)
|
|
const scpArgs = [
|
|
...(keyPath ? ['-i', keyPath] : []),
|
|
'-P', port,
|
|
'-o', 'StrictHostKeyChecking=accept-new',
|
|
]
|
|
const target = `${win.user}@${win.host}:${remoteDir}/`
|
|
const uploadExe = spawnSync('scp', [...scpArgs, exePath, target], { stdio: 'inherit' })
|
|
if (uploadExe.status !== 0) {
|
|
console.error(`\n[x] 安装包上传失败(scp 退出码 ${uploadExe.status})`)
|
|
process.exit(uploadExe.status || 1)
|
|
}
|
|
const uploadLatest = spawnSync('scp', [...scpArgs, latestPath, target], { stdio: 'inherit' })
|
|
if (uploadLatest.status !== 0) {
|
|
console.error(`\n[x] ${latestName} 上传失败(scp 退出码 ${uploadLatest.status})`)
|
|
process.exit(uploadLatest.status || 1)
|
|
}
|
|
console.log(`\n[ok] 发布完成: ${urlPrefix}${setupExe}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('\n[ok] 打包完成,安装包位于 release/windows/ 目录')
|