cc-haha/desktop/scripts/electron-dev.ts
程序员阿江(Relakkes) 386a41e606 feat(desktop): enable Electron migration path
Introduce the Electron desktop shell alongside the existing React renderer and local Bun server boundary. The migration keeps the DesktopHost contract explicit across Tauri, Electron, and browser runtimes while adding Electron main/preload services for dialogs, shell, notifications, updates, tray/window lifecycle, terminal, preview WebContentsView, app mode, and release/package validation.

The commit also carries the latest local main desktop command updates, including agent slash entries and hidden-by-default markdown thinking details, so the packaged Electron build matches the current main UX surface.

Constraint: React renderer, local Bun server, REST/WebSocket, and sidecar boundaries must remain reusable during the migration
Constraint: macOS dev packages are ad-hoc signed and cannot prove Developer ID notarization or Gatekeeper release launch
Rejected: Browser-only smoke validation | it cannot exercise native dialogs, keychain prompts, notification behavior, or packaged app startup
Confidence: medium
Scope-risk: broad
Directive: Do not remove Tauri host support until signed Electron release artifacts pass native OS smoke on macOS, Windows, and Linux
Tested: bun run check:desktop
Tested: cd desktop && bun run check:electron
Tested: CSC_IDENTITY_AUTO_DISCOVERY=false bun run electron📦dir
Tested: bun run test:package-smoke --platform macos --package-kind dir --artifacts-dir desktop/build-artifacts/electron
Tested: Computer Use read packaged Electron app window at desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app
Not-tested: Developer ID signed/notarized Gatekeeper launch
Not-tested: Real OS notification click-to-session action
Not-tested: Windows and Linux packaged app smoke on real hosts
2026-06-01 22:43:16 +08:00

83 lines
2.0 KiB
TypeScript

export const DEFAULT_RENDERER_URL = 'http://localhost:1420'
export const LOCAL_NO_PROXY_ENTRIES = ['localhost', '127.0.0.1', '::1']
export function mergeNoProxy(existing: string | undefined, required = LOCAL_NO_PROXY_ENTRIES) {
const entries = new Set(
(existing ?? '')
.split(',')
.map(entry => entry.trim())
.filter(Boolean),
)
for (const entry of required) entries.add(entry)
return Array.from(entries).join(',')
}
export function createElectronDevEnv(env: NodeJS.ProcessEnv = process.env) {
const rendererUrl = env.ELECTRON_RENDERER_URL ?? DEFAULT_RENDERER_URL
const noProxy = mergeNoProxy(env.NO_PROXY ?? env.no_proxy)
return {
...env,
ELECTRON_RENDERER_URL: rendererUrl,
NO_PROXY: noProxy,
no_proxy: noProxy,
}
}
async function waitForRenderer(rendererUrl: string) {
const deadline = Date.now() + 30_000
while (Date.now() < deadline) {
try {
const response = await fetch(rendererUrl)
if (response.ok) return
} catch {
await Bun.sleep(250)
}
}
throw new Error(`Timed out waiting for Vite renderer at ${rendererUrl}`)
}
async function main() {
const desktopRoot = new URL('..', import.meta.url).pathname
const childEnv = createElectronDevEnv()
const rendererUrl = childEnv.ELECTRON_RENDERER_URL
process.env.NO_PROXY = childEnv.NO_PROXY
process.env.no_proxy = childEnv.no_proxy
const vite = Bun.spawn(['bun', 'run', 'dev'], {
cwd: desktopRoot,
env: childEnv,
stdout: 'inherit',
stderr: 'inherit',
})
function stopVite() {
vite.kill()
}
process.on('SIGINT', () => {
stopVite()
process.exit(130)
})
process.on('SIGTERM', () => {
stopVite()
process.exit(143)
})
await waitForRenderer(rendererUrl)
const electron = Bun.spawn(['bunx', 'electron', './electron-dist/main.cjs'], {
cwd: desktopRoot,
env: childEnv,
stdout: 'inherit',
stderr: 'inherit',
})
const exitCode = await electron.exited
stopVite()
process.exit(exitCode)
}
if (import.meta.main) {
await main()
}