195 lines
6.0 KiB
JavaScript
195 lines
6.0 KiB
JavaScript
import { spawnSync } from 'node:child_process'
|
|
import {
|
|
chmodSync,
|
|
existsSync,
|
|
mkdtempSync,
|
|
readdirSync,
|
|
readFileSync,
|
|
rmSync,
|
|
writeFileSync,
|
|
} from 'node:fs'
|
|
import { homedir, tmpdir } from 'node:os'
|
|
import { delimiter, dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
const desktopRoot = join(__dirname, '..')
|
|
const builderBin = join(
|
|
desktopRoot,
|
|
'node_modules/.bin',
|
|
process.platform === 'win32' ? 'electron-builder.cmd' : 'electron-builder',
|
|
)
|
|
|
|
const env = {
|
|
...process.env,
|
|
ELECTRON_MIRROR: process.env.ELECTRON_MIRROR || 'https://cdn.npmmirror.com/binaries/electron/',
|
|
ELECTRON_BUILDER_BINARIES_MIRROR:
|
|
process.env.ELECTRON_BUILDER_BINARIES_MIRROR ||
|
|
'https://registry.npmmirror.com/-/binary/electron-builder-binaries/',
|
|
}
|
|
|
|
const rawArgs = process.argv.slice(2)
|
|
const mode = rawArgs[0] || '--dir'
|
|
|
|
function hasCommand(cmd, customEnv = env) {
|
|
const probe = process.platform === 'win32'
|
|
? spawnSync('where.exe', [cmd], { env: customEnv, stdio: 'ignore' })
|
|
: spawnSync('which', [cmd], { env: customEnv, stdio: 'ignore' })
|
|
return probe.status === 0
|
|
}
|
|
|
|
function findNvmBinDir() {
|
|
const nvmVersionsDir = join(homedir(), '.nvm', 'versions', 'node')
|
|
if (!existsSync(nvmVersionsDir)) return null
|
|
try {
|
|
const dirs = readdirSync(nvmVersionsDir).sort().reverse()
|
|
for (const dir of dirs) {
|
|
const binDir = join(nvmVersionsDir, dir, 'bin')
|
|
if (existsSync(join(binDir, 'npm'))) {
|
|
return binDir
|
|
}
|
|
}
|
|
} catch {
|
|
return null
|
|
}
|
|
return null
|
|
}
|
|
|
|
function prepareBuilderEnv() {
|
|
const preparedEnv = { ...env }
|
|
if (process.platform === 'win32') {
|
|
return { buildEnv: preparedEnv, cleanup: () => {} }
|
|
}
|
|
|
|
if (hasCommand('npm', preparedEnv)) {
|
|
return { buildEnv: preparedEnv, cleanup: () => {} }
|
|
}
|
|
|
|
const nvmBin = findNvmBinDir()
|
|
if (nvmBin) {
|
|
preparedEnv.PATH = preparedEnv.PATH ? `${nvmBin}${delimiter}${preparedEnv.PATH}` : nvmBin
|
|
if (hasCommand('npm', preparedEnv)) {
|
|
return { buildEnv: preparedEnv, cleanup: () => {} }
|
|
}
|
|
}
|
|
|
|
// Pure-Bun environment fallback: client has no runtime dependencies, so a
|
|
// minimal npm shim satisfies electron-builder v26's NodeModulesCollector lookup
|
|
// when bun.lock (text) is present without Node/npm installed.
|
|
const pkg = JSON.parse(readFileSync(join(desktopRoot, 'package.json'), 'utf8'))
|
|
const shimDir = mkdtempSync(join(tmpdir(), 'planet-eb-npm-'))
|
|
const shimPath = join(shimDir, 'npm')
|
|
const jsonPayload = JSON.stringify({
|
|
name: pkg.name || 'planet-client',
|
|
version: pkg.version || '0.0.0',
|
|
dependencies: {},
|
|
})
|
|
writeFileSync(
|
|
shimPath,
|
|
`#!/usr/bin/env sh\nprintf '%s\\n' '${jsonPayload.replace(/'/g, "'\\''")}'\n`,
|
|
'utf8',
|
|
)
|
|
chmodSync(shimPath, 0o755)
|
|
preparedEnv.PATH = preparedEnv.PATH ? `${shimDir}${delimiter}${preparedEnv.PATH}` : shimDir
|
|
|
|
return {
|
|
buildEnv: preparedEnv,
|
|
cleanup: () => {
|
|
rmSync(shimDir, { recursive: true, force: true })
|
|
},
|
|
}
|
|
}
|
|
|
|
function runBuilder(builderArgs) {
|
|
const { buildEnv, cleanup } = prepareBuilderEnv()
|
|
try {
|
|
const result = spawnSync(builderBin, [...builderArgs, '--publish', 'never'], {
|
|
cwd: desktopRoot,
|
|
env: buildEnv,
|
|
stdio: 'inherit',
|
|
shell: process.platform === 'win32',
|
|
})
|
|
if ((result.status ?? 1) !== 0) {
|
|
process.exit(result.status ?? 1)
|
|
}
|
|
} finally {
|
|
cleanup()
|
|
}
|
|
}
|
|
|
|
function isWsl() {
|
|
return process.platform === 'linux' && Boolean(process.env.WSL_DISTRO_NAME || existsSync('/proc/sys/fs/binfmt_misc/WSLInterop'))
|
|
}
|
|
|
|
function resolveLinuxTargets() {
|
|
const targets = []
|
|
if (hasCommand('mksquashfs')) targets.push('AppImage')
|
|
if (hasCommand('dpkg-deb')) targets.push('deb')
|
|
if (targets.length === 0) targets.push('tar.gz')
|
|
return targets
|
|
}
|
|
|
|
function runWinBuild() {
|
|
if (process.platform === 'win32' || hasCommand('wine') || hasCommand('wine64')) {
|
|
runBuilder(['--win', 'nsis', 'portable'])
|
|
return
|
|
}
|
|
|
|
if (isWsl()) {
|
|
const winBunCheck = spawnSync(
|
|
'powershell.exe',
|
|
['-NoProfile', '-Command', 'Get-Command bun.exe -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source'],
|
|
{ encoding: 'utf8' },
|
|
)
|
|
const winBunPath = (winBunCheck.stdout || '').trim()
|
|
const winPathRes = spawnSync('wslpath', ['-w', desktopRoot], { encoding: 'utf8' })
|
|
const winDesktopRoot = (winPathRes.stdout || '').trim()
|
|
if (winBunCheck.status === 0 && winBunPath && winDesktopRoot) {
|
|
console.log('WSL2 detected without Wine; building Windows installers via Windows host toolchain...')
|
|
const psScript = [
|
|
"$ErrorActionPreference = 'Stop'",
|
|
`$env:ELECTRON_MIRROR = '${env.ELECTRON_MIRROR}'`,
|
|
`$env:ELECTRON_BUILDER_BINARIES_MIRROR = '${env.ELECTRON_BUILDER_BINARIES_MIRROR}'`,
|
|
`Set-Location -LiteralPath '${winDesktopRoot.replace(/'/g, "''")}'`,
|
|
'bun x electron-builder --win nsis portable --publish never',
|
|
].join('; ')
|
|
const winRun = spawnSync('powershell.exe', ['-NoProfile', '-Command', psScript], {
|
|
stdio: 'inherit',
|
|
})
|
|
if ((winRun.status ?? 1) === 0) {
|
|
return
|
|
}
|
|
console.warn('Windows host build fell back to unpacked/zip cross-build without Wine.')
|
|
}
|
|
} else {
|
|
console.warn('Wine not available on Linux host: building standalone Windows zip bundle (win-unpacked + zip).')
|
|
}
|
|
|
|
runBuilder(['--win', 'zip', '--config.win.signAndEditExecutable=false'])
|
|
}
|
|
|
|
function runMacBuild() {
|
|
if (process.platform === 'darwin') {
|
|
runBuilder(['--mac', 'dmg', 'zip'])
|
|
} else {
|
|
console.warn('Non-macOS host detected: building macOS zip bundle without dmg/codesign.')
|
|
runBuilder(['--mac', 'zip'])
|
|
}
|
|
}
|
|
|
|
if (mode === '--dir') {
|
|
runBuilder(['--dir'])
|
|
} else if (mode === '--linux') {
|
|
runBuilder(['--linux', ...resolveLinuxTargets()])
|
|
} else if (mode === '--mac') {
|
|
runMacBuild()
|
|
} else if (mode === '--win') {
|
|
runWinBuild()
|
|
} else if (mode === '--all') {
|
|
runBuilder(['--linux', ...resolveLinuxTargets()])
|
|
runWinBuild()
|
|
runMacBuild()
|
|
} else {
|
|
runBuilder(rawArgs)
|
|
}
|