mirror of
https://github.com/xxnuo/MTranServer.git
synced 2026-09-03 06:35:20 +08:00
Update electron-build.ts
Update electron-build.ts Update electron-build.ts
This commit is contained in:
@@ -76,7 +76,7 @@
|
||||
"buildDependenciesFromSource": false,
|
||||
"copyright": "Copyright © 2025",
|
||||
"extraMetadata": {
|
||||
"main": "scripts/electron-main.js"
|
||||
"main": "dist/main-bundled.js"
|
||||
},
|
||||
"asar": false,
|
||||
"asarUnpack": [
|
||||
@@ -91,8 +91,7 @@
|
||||
"!dist/mtranserver*",
|
||||
"!dist/*.exe",
|
||||
"images/**/*",
|
||||
"package.json",
|
||||
"node_modules/**/*"
|
||||
"package.json"
|
||||
],
|
||||
"directories": {
|
||||
"output": "release"
|
||||
|
||||
@@ -53,7 +53,7 @@ await $`bun tsoa spec-and-routes`;
|
||||
|
||||
if (isLib) {
|
||||
console.log("Building library...");
|
||||
await $`bun build src/index.ts src/main.ts src/desktop.ts --outdir dist --target node --format esm --sourcemap --external zstd-wasm-decoder --external express`;
|
||||
await $`bun build src/index.ts src/main.ts src/desktop.ts --outdir dist --target node --format esm --sourcemap --external '*'`;
|
||||
await $`tsc -p tsconfig.lib.json`;
|
||||
console.log("Build complete!");
|
||||
process.exit(0);
|
||||
@@ -61,7 +61,7 @@ if (isLib) {
|
||||
|
||||
if (isNode) {
|
||||
console.log("Building for Node...");
|
||||
await $`bun build src/main.ts --outdir dist --target node --format esm --sourcemap --external zstd-wasm-decoder --external express`;
|
||||
await $`bun build src/main.ts src/desktop.ts --outdir dist --target node --format esm --sourcemap`;
|
||||
console.log("Build complete!");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { spawn } from 'child_process'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
function run(command: string, args: string[], options?: { cwd?: string; env?: NodeJS.ProcessEnv }) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
@@ -16,133 +16,60 @@ function run(command: string, args: string[], options?: { cwd?: string; env?: No
|
||||
})
|
||||
}
|
||||
|
||||
function cleanDistBinaries(distDir: string) {
|
||||
if (!fs.existsSync(distDir)) return
|
||||
for (const entry of fs.readdirSync(distDir)) {
|
||||
if (entry.startsWith('mtranserver') || entry.endsWith('.exe')) {
|
||||
try {
|
||||
fs.rmSync(path.join(distDir, entry), { recursive: true, force: true })
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function prepareNpmModules(root: string) {
|
||||
const tempDir = path.join(root, '.electron-build-temp')
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
fs.mkdirSync(tempDir, { recursive: true })
|
||||
fs.copyFileSync(path.join(root, 'package.json'), path.join(tempDir, 'package.json'))
|
||||
if (fs.existsSync(path.join(root, 'package-lock.json'))) {
|
||||
fs.copyFileSync(path.join(root, 'package-lock.json'), path.join(tempDir, 'package-lock.json'))
|
||||
}
|
||||
await run('npm', ['install', '--omit=dev', '--ignore-scripts', '--install-strategy=hoisted'], { cwd: tempDir })
|
||||
const targetModules = path.join(root, 'node_modules_prod')
|
||||
fs.rmSync(targetModules, { recursive: true, force: true })
|
||||
fs.renameSync(path.join(tempDir, 'node_modules'), targetModules)
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
return targetModules
|
||||
}
|
||||
|
||||
function getUnpackedDir(root: string) {
|
||||
const platform = process.platform
|
||||
const releaseDir = path.join(root, 'release')
|
||||
if (platform === 'linux') {
|
||||
return path.join(releaseDir, 'linux-unpacked')
|
||||
} else if (platform === 'darwin') {
|
||||
const entries = fs.readdirSync(releaseDir).filter(e => e.endsWith('.app'))
|
||||
if (entries.length > 0) {
|
||||
return path.join(releaseDir, 'mac-arm64', entries[0])
|
||||
}
|
||||
return path.join(releaseDir, 'mac')
|
||||
} else if (platform === 'win32') {
|
||||
return path.join(releaseDir, 'win-unpacked')
|
||||
}
|
||||
return path.join(releaseDir, 'linux-unpacked')
|
||||
}
|
||||
|
||||
async function repackAsar(unpackedDir: string) {
|
||||
const appDir = path.join(unpackedDir, 'resources', 'app')
|
||||
const asarFile = path.join(unpackedDir, 'resources', 'app.asar')
|
||||
const asarUnpackDir = path.join(unpackedDir, 'resources', 'app.asar.unpacked')
|
||||
const binDir = path.join(appDir, 'node_modules', '.bin')
|
||||
if (fs.existsSync(binDir)) {
|
||||
fs.rmSync(binDir, { recursive: true })
|
||||
}
|
||||
if (fs.existsSync(asarFile)) {
|
||||
fs.rmSync(asarFile)
|
||||
}
|
||||
if (fs.existsSync(asarUnpackDir)) {
|
||||
fs.rmSync(asarUnpackDir, { recursive: true })
|
||||
}
|
||||
await run('npx', ['--yes', '@electron/asar', 'pack', appDir, asarFile])
|
||||
fs.rmSync(appDir, { recursive: true })
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const bunBin = process.execPath
|
||||
const root = process.cwd()
|
||||
|
||||
await run(bunBin, ['run', 'build:lib'])
|
||||
// Build for Node (bundles dependencies, produces dist/main.js and dist/desktop.js)
|
||||
console.log('Building for Node with Bun...')
|
||||
await run(bunBin, ['run', 'build:node'])
|
||||
|
||||
const distDir = path.join(root, 'dist')
|
||||
cleanDistBinaries(distDir)
|
||||
const outfile = path.join(root, 'dist', 'main-bundled.js')
|
||||
console.log(`Building Electron main process to ${outfile}...`)
|
||||
|
||||
await run(bunBin, [
|
||||
'build', 'scripts/electron-main.js',
|
||||
'--outfile', outfile,
|
||||
'--target', 'node',
|
||||
'--external', 'electron',
|
||||
'--format', 'esm',
|
||||
'--sourcemap'
|
||||
])
|
||||
|
||||
console.log('Installing production dependencies with npm...')
|
||||
const prodModules = await prepareNpmModules(root)
|
||||
|
||||
const originalModules = path.join(root, 'node_modules')
|
||||
const backupModules = path.join(root, 'node_modules_bun')
|
||||
|
||||
fs.renameSync(originalModules, backupModules)
|
||||
fs.renameSync(prodModules, originalModules)
|
||||
|
||||
const electronBuilderPath = path.join(backupModules, '.bin/electron-builder')
|
||||
const env = { ...process.env, NODE_PATH: backupModules }
|
||||
|
||||
try {
|
||||
const platform = process.platform
|
||||
const dirArgs = [electronBuilderPath, '--dir']
|
||||
if (process.argv.includes('--all')) {
|
||||
dirArgs.push('-mwl')
|
||||
} else if (platform === 'linux') {
|
||||
dirArgs.push('--linux')
|
||||
} else if (platform === 'darwin') {
|
||||
dirArgs.push('--mac')
|
||||
} else if (platform === 'win32') {
|
||||
dirArgs.push('--win')
|
||||
// Verify and fix location if Bun misbehaved
|
||||
if (!fs.existsSync(outfile)) {
|
||||
const wrongLoc = path.join(root, 'scripts', 'main-bundled.js')
|
||||
if (fs.existsSync(wrongLoc)) {
|
||||
console.log(`Moving build output from ${wrongLoc} to ${outfile}...`)
|
||||
fs.renameSync(wrongLoc, outfile)
|
||||
if (fs.existsSync(wrongLoc + '.map')) {
|
||||
fs.renameSync(wrongLoc + '.map', outfile + '.map')
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Packaging directory...')
|
||||
await run(bunBin, dirArgs, { env })
|
||||
|
||||
const unpackedDir = getUnpackedDir(root)
|
||||
const appNodeModules = path.join(unpackedDir, 'resources', 'app', 'node_modules')
|
||||
|
||||
console.log('Copying all node_modules...')
|
||||
fs.rmSync(appNodeModules, { recursive: true, force: true })
|
||||
fs.cpSync(originalModules, appNodeModules, { recursive: true })
|
||||
|
||||
console.log('Repacking asar...')
|
||||
await repackAsar(unpackedDir)
|
||||
|
||||
console.log('Building final package...')
|
||||
const finalArgs = [electronBuilderPath, '--prepackaged', unpackedDir]
|
||||
if (process.argv.includes('--all')) {
|
||||
finalArgs.push('-mwl')
|
||||
} else if (platform === 'linux') {
|
||||
finalArgs.push('--linux', 'AppImage')
|
||||
} else if (platform === 'darwin') {
|
||||
finalArgs.push('--mac')
|
||||
} else if (platform === 'win32') {
|
||||
finalArgs.push('--win')
|
||||
}
|
||||
await run(bunBin, finalArgs, { env })
|
||||
} finally {
|
||||
fs.rmSync(originalModules, { recursive: true, force: true })
|
||||
fs.renameSync(backupModules, originalModules)
|
||||
}
|
||||
|
||||
const electronBuilderPath = path.join(root, 'node_modules', '.bin', 'electron-builder')
|
||||
const env = { ...process.env }
|
||||
|
||||
const platform = process.platform
|
||||
const args = [electronBuilderPath]
|
||||
|
||||
// Add build arguments
|
||||
if (process.argv.includes('--all')) {
|
||||
args.push('-mwl')
|
||||
} else if (platform === 'linux') {
|
||||
args.push('--linux')
|
||||
} else if (platform === 'darwin') {
|
||||
args.push('--mac')
|
||||
} else if (platform === 'win32') {
|
||||
args.push('--win')
|
||||
}
|
||||
|
||||
console.log('Building Electron app...')
|
||||
await run(bunBin, args, { env })
|
||||
}
|
||||
|
||||
main().catch(() => process.exit(1))
|
||||
main().catch((e) => {
|
||||
console.error(e)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user