Files
2026-10-11 07:55:36 -07:00

109 lines
5.1 KiB
JavaScript
Executable File

// Dev launcher — on Windows/Linux, runs Electron under a "CrossSound" process name
// (instead of electron.exe) so `taskkill /f /im electron` on your OTHER Electron apps
// doesn't also kill CrossSound. The renamed binary is a byte-identical copy of
// Electron's own binary, placed in electron's dist/ folder so it finds its sibling
// DLLs, and it reuses the VMP (Widevine) signature: since CrossSound.exe ===
// electron.exe byte-for-byte, electron.exe.sig is a valid signature for it too — so
// DRM keeps working. Runs AFTER `npm run vmp-sign`, so the copied .sig is always fresh.
//
// macOS is different (see below): the binary lives inside Electron.app, so we launch
// it in place instead of renaming it.
//
// Wired into package.json as: build && vmp-sign && node scripts/dev-launch.js
// (Packaged builds already run as CrossSound.exe via Forge — this is dev-only.)
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const electronRoot = path.join(__dirname, '..', 'node_modules', 'electron')
const distDir = path.join(electronRoot, 'dist')
const isWin = process.platform === 'win32'
const isMac = process.platform === 'darwin'
// Resolve the Electron binary the way Electron itself does, via node_modules/electron/
// path.txt (written by its install.js). The path under dist/ is platform-specific:
// Windows electron.exe
// Linux electron
// macOS Electron.app/Contents/MacOS/Electron
// Hardcoding "dist/electron" for every non-Windows platform is the Linux layout only,
// which is why `npm run start` / `npm run dev` died on macOS with
// "[dev] Electron binary not found: .../node_modules/electron/dist/electron".
function resolveElectron() {
const pathFile = path.join(electronRoot, 'path.txt')
if (fs.existsSync(pathFile)) {
const p = path.join(distDir, fs.readFileSync(pathFile, 'utf8').trim())
if (fs.existsSync(p)) return p
}
// path.txt is missing, or was written by an install for a DIFFERENT platform (a
// node_modules tree copied over from another machine) — try this platform's layout.
const rel = isWin ? 'electron.exe'
: isMac ? path.join('Electron.app', 'Contents', 'MacOS', 'Electron')
: 'electron'
const fallback = path.join(distDir, rel)
return fs.existsSync(fallback) ? fallback : null
}
const srcExe = resolveElectron()
if (!srcExe) {
console.error('[dev] Electron binary not found under', distDir)
console.error('[dev] Install it for this platform: node node_modules/electron/install.js')
console.error('[dev] (or delete node_modules/electron and re-run `npm install`)')
process.exit(1)
}
let bin = srcExe
if (isMac) {
// No rename on macOS: the executable lives inside a signed .app bundle and depends
// on it (Info.plist, Frameworks, Resources), so it can't just be copied out — and
// renaming inside the bundle invalidates the ad-hoc code signature, which is fatal
// on Apple Silicon. There's also no `taskkill /im electron` problem to dodge here,
// and the VMP signature lives inside the bundle (Electron Framework.sig), so DRM is
// unaffected. Launch it in place.
} else {
const dstExe = path.join(distDir, isWin ? 'CrossSound.exe' : 'CrossSound')
const srcSig = srcExe + '.sig'
const dstSig = dstExe + '.sig'
// The binary is ~220 MB — copy it only when missing or after an Electron update.
const exeStale = !fs.existsSync(dstExe) || fs.statSync(srcExe).mtimeMs > fs.statSync(dstExe).mtimeMs
if (exeStale) {
console.log('[dev] creating', path.basename(dstExe), '(byte-identical copy of Electron)…')
fs.copyFileSync(srcExe, dstExe)
fs.chmodSync(dstExe, 0o755)
}
// Refresh the VMP signature every run (tiny; vmp-sign regenerates electron.exe.sig).
// Byte-identical exe -> the same signature validates for CrossSound.exe.
if (fs.existsSync(srcSig)) {
fs.copyFileSync(srcSig, dstSig)
} else {
console.warn('[dev] no VMP signature next to', path.basename(srcExe), '— Widevine DRM may fail in dev')
}
bin = dstExe
}
// Kill any stale instances first. There's no single-instance lock, so without this
// every `npm run dev` piles on a NEW window while old ones (with old UI code) keep
// running — and you end up staring at a pre-edit build, convinced your changes
// "aren't taking". Off Windows we match on the FULL binary path (which is inside this
// repo's node_modules), so a stock `Electron` name can't take out unrelated apps.
try {
const { execSync, execFileSync } = require('child_process')
if (isWin) {
execSync(`taskkill /F /IM "${path.basename(bin)}" /T`, { stdio: 'ignore' })
} else {
// pkill -f takes an extended regex, so escape the path's regex metacharacters.
// execFileSync (no shell) keeps spaces and $ in the path out of shell parsing.
const pattern = bin.replace(/[.[\]{}()*+?^$|\\]/g, (c) => '\\' + c)
execFileSync('pkill', ['-f', pattern + ' '], { stdio: 'ignore' })
}
console.log('[dev] closed stale', path.basename(bin), 'instances')
} catch { /* none were running */ }
console.log('[dev] launching', path.basename(bin))
const child = spawn(bin, ['.', ...process.argv.slice(2)], { stdio: 'inherit' })
child.on('exit', (code) => process.exit(code == null ? 0 : code))