- electron/builder.js: 修复 Linux 平台 npm 命令、增加 .deb 安装包支持 - electron/main.js: 按平台动态设置保存对话框默认扩展名 - package.json: 添加 Linux deb 打包目标 - 新增 Dockerfile 用于 Linux 交叉构建 - 新增 build-linux-docker.ps1 / build-linux.sh 构建脚本 - 新增 site/ 项目官网页面 - .gitignore: 全面忽略二进制文件 - 新增 GitHub Actions CI 工作流
233 lines
7.5 KiB
JavaScript
233 lines
7.5 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
const { execSync, spawn } = require('child_process')
|
|
const { getConfigPath, getSrcDir } = require('./project')
|
|
|
|
let isCancelled = false
|
|
|
|
function cancelPack() {
|
|
isCancelled = true
|
|
}
|
|
|
|
async function startPack(projectPath, outputPath, onProgress) {
|
|
isCancelled = false
|
|
const config = JSON.parse(fs.readFileSync(getConfigPath(projectPath), 'utf-8'))
|
|
const srcDir = getSrcDir(projectPath)
|
|
const tempDir = path.join(projectPath, '.web2app', 'temp-build')
|
|
|
|
onProgress({ stage: 'prepare', message: '准备打包环境...', percent: 5 })
|
|
|
|
if (fs.existsSync(tempDir)) {
|
|
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
}
|
|
|
|
if (isCancelled) return { success: false, cancelled: true }
|
|
onProgress({ stage: 'prepare', message: '复制模板文件...', percent: 10 })
|
|
|
|
copyTemplateSync(tempDir)
|
|
|
|
if (isCancelled) return { success: false, cancelled: true }
|
|
onProgress({ stage: 'copy-files', message: '复制前端资源...', percent: 20 })
|
|
|
|
const frontendDest = path.join(tempDir, 'frontend')
|
|
fs.mkdirSync(frontendDest, { recursive: true })
|
|
copyDirContentsSync(srcDir, frontendDest)
|
|
|
|
if (isCancelled) return { success: false, cancelled: true }
|
|
onProgress({ stage: 'configure', message: '配置应用信息...', percent: 35 })
|
|
|
|
const templatePackagePath = path.join(tempDir, 'package.json')
|
|
const templatePackage = JSON.parse(fs.readFileSync(templatePackagePath, 'utf-8'))
|
|
templatePackage.name = config.name.toLowerCase().replace(/\s+/g, '-')
|
|
templatePackage.productName = config.name
|
|
templatePackage.version = config.version
|
|
templatePackage.description = config.description || `Generated by Web2App - ${config.name}`
|
|
templatePackage.author = config.author || ''
|
|
templatePackage.build = {
|
|
appId: `com.web2app.${templatePackage.name}`,
|
|
productName: config.name,
|
|
artifactName: `${config.name}-Setup-\${version}.\${ext}`,
|
|
directories: {
|
|
output: 'dist'
|
|
},
|
|
files: [
|
|
'main.js',
|
|
'preload.js',
|
|
'frontend/**/*',
|
|
'backend/**/*',
|
|
'node_modules/**/*'
|
|
],
|
|
extraResources: config.icon ? [
|
|
{
|
|
from: path.join(frontendDest, config.icon),
|
|
to: 'icon.png'
|
|
}
|
|
] : [],
|
|
win: {
|
|
target: [
|
|
{
|
|
target: 'nsis',
|
|
arch: ['x64']
|
|
}
|
|
],
|
|
icon: config.icon ? path.join(frontendDest, config.icon) : undefined
|
|
},
|
|
nsis: {
|
|
oneClick: false,
|
|
perMachine: false,
|
|
allowToChangeInstallationDirectory: true,
|
|
createDesktopShortcut: true,
|
|
shortcutName: config.name
|
|
},
|
|
mac: {
|
|
target: ['dmg'],
|
|
icon: config.icon ? path.join(frontendDest, config.icon) : undefined
|
|
},
|
|
linux: {
|
|
target: ['AppImage'],
|
|
icon: config.icon ? path.join(frontendDest, config.icon) : undefined
|
|
}
|
|
}
|
|
fs.writeFileSync(templatePackagePath, JSON.stringify(templatePackage, null, 2))
|
|
|
|
if (isCancelled) return { success: false, cancelled: true }
|
|
onProgress({ stage: 'install', message: '安装依赖 (首次较慢)...', percent: 50 })
|
|
|
|
try {
|
|
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
execSync(`${npmCmd} install --production`, {
|
|
cwd: tempDir,
|
|
stdio: 'pipe',
|
|
timeout: 300000
|
|
})
|
|
} catch (err) {
|
|
onProgress({ stage: 'install', message: `依赖安装失败: ${err.message}`, percent: 50 })
|
|
return { success: false, error: err.message }
|
|
}
|
|
|
|
if (isCancelled) return { success: false, cancelled: true }
|
|
onProgress({ stage: 'build', message: '正在打包生成安装包...', percent: 65 })
|
|
|
|
return new Promise((resolve) => {
|
|
const builderPath = path.join(tempDir, 'node_modules', '.bin', 'electron-builder')
|
|
const builderCmd = process.platform === 'win32' ? `${builderPath}.cmd` : builderPath
|
|
|
|
const builderProcess = spawn(builderCmd, ['--config', templatePackagePath], {
|
|
cwd: tempDir,
|
|
shell: true,
|
|
stdio: ['pipe', 'pipe', 'pipe']
|
|
})
|
|
|
|
let buildOutput = ''
|
|
|
|
builderProcess.stdout.on('data', (data) => {
|
|
buildOutput += data.toString()
|
|
const msg = data.toString()
|
|
|
|
if (msg.includes('packaging')) {
|
|
onProgress({ stage: 'build', message: '正在打包...', percent: 75 })
|
|
} else if (msg.includes('signing')) {
|
|
onProgress({ stage: 'sign', message: '正在签名...', percent: 85 })
|
|
} else {
|
|
onProgress({ stage: 'build', message: msg.trim(), percent: 75 })
|
|
}
|
|
})
|
|
|
|
builderProcess.stderr.on('data', (data) => {
|
|
buildOutput += data.toString()
|
|
})
|
|
|
|
builderProcess.on('close', (code) => {
|
|
if (isCancelled) {
|
|
resolve({ success: false, cancelled: true })
|
|
return
|
|
}
|
|
|
|
if (code !== 0) {
|
|
const errMsg = buildOutput.split('\n').filter(l => l.includes('error')).join('\n') || '打包失败'
|
|
onProgress({ stage: 'error', message: errMsg, percent: 0 })
|
|
resolve({ success: false, error: errMsg, output: buildOutput })
|
|
return
|
|
}
|
|
|
|
onProgress({ stage: 'complete', message: '打包完成!', percent: 100 })
|
|
|
|
const distDir = path.join(tempDir, 'dist')
|
|
let installerPath = null
|
|
if (fs.existsSync(distDir)) {
|
|
const files = fs.readdirSync(distDir)
|
|
.filter(f => f.endsWith('.exe') || f.endsWith('.dmg') || f.endsWith('.AppImage') || f.endsWith('.deb'))
|
|
.sort((a, b) => fs.statSync(path.join(distDir, b)).mtimeMs - fs.statSync(path.join(distDir, a)).mtimeMs)
|
|
if (files.length > 0) {
|
|
installerPath = path.join(distDir, files[0])
|
|
if (outputPath) {
|
|
try {
|
|
fs.copyFileSync(installerPath, outputPath)
|
|
installerPath = outputPath
|
|
} catch (e) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resolve({ success: true, outputPath: installerPath })
|
|
})
|
|
|
|
builderProcess.on('error', (err) => {
|
|
resolve({ success: false, error: err.message })
|
|
})
|
|
})
|
|
}
|
|
|
|
function copyTemplateSync(destDir) {
|
|
const templateDir = path.join(__dirname, '..', 'template')
|
|
const entries = fs.readdirSync(templateDir, { withFileTypes: true })
|
|
for (const entry of entries) {
|
|
if (entry.name.startsWith('.')) continue
|
|
const srcPath = path.join(templateDir, entry.name)
|
|
const destPath = path.join(destDir, entry.name)
|
|
if (entry.isDirectory()) {
|
|
copyDirSync(srcPath, destPath)
|
|
} else {
|
|
fs.mkdirSync(path.dirname(destPath), { recursive: true })
|
|
fs.copyFileSync(srcPath, destPath)
|
|
}
|
|
}
|
|
|
|
fs.mkdirSync(path.join(destDir, 'frontend'), { recursive: true })
|
|
fs.mkdirSync(path.join(destDir, 'backend'), { recursive: true })
|
|
}
|
|
|
|
function copyDirSync(src, dest) {
|
|
fs.mkdirSync(dest, { recursive: true })
|
|
const entries = fs.readdirSync(src, { withFileTypes: true })
|
|
for (const entry of entries) {
|
|
if (entry.name.startsWith('.')) continue
|
|
const srcPath = path.join(src, entry.name)
|
|
const destPath = path.join(dest, entry.name)
|
|
if (entry.isDirectory()) {
|
|
copyDirSync(srcPath, destPath)
|
|
} else {
|
|
fs.copyFileSync(srcPath, destPath)
|
|
}
|
|
}
|
|
}
|
|
|
|
function copyDirContentsSync(src, dest) {
|
|
if (!fs.existsSync(src)) return
|
|
fs.mkdirSync(dest, { recursive: true })
|
|
const entries = fs.readdirSync(src, { withFileTypes: true })
|
|
for (const entry of entries) {
|
|
if (entry.name.startsWith('.')) continue
|
|
const srcPath = path.join(src, entry.name)
|
|
const destPath = path.join(dest, entry.name)
|
|
if (entry.isDirectory()) {
|
|
copyDirSync(srcPath, destPath)
|
|
} else {
|
|
fs.copyFileSync(srcPath, destPath)
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { startPack, cancelPack }
|