()
| 49 | } |
| 50 | |
| 51 | private async initialize(): Promise<void> { |
| 52 | const env: NodeJS.ProcessEnv = { ...process.env, ELECTRON_VITE_RPC: 'true' } |
| 53 | delete env.ELECTRON_RUN_AS_NODE |
| 54 | this.stderr = '' |
| 55 | const compilerPath = path.join( |
| 56 | path.dirname(require.resolve('electron-vite/package.json')), |
| 57 | 'bin', |
| 58 | 'electron-bytecode-main.cjs' |
| 59 | ) |
| 60 | const ps = spawn(getElectronPath(), [compilerPath], { |
| 61 | stdio: ['ignore', 'ignore', 'pipe', 'ipc'], |
| 62 | windowsHide: true, |
| 63 | env |
| 64 | }) |
| 65 | this.ps = ps |
| 66 | if (ps.stderr) { |
| 67 | ps.stderr.setEncoding('utf8') |
| 68 | ps.stderr.on('data', (chunk: string) => { |
| 69 | if (this.ps === ps) this.stderr = (this.stderr + chunk).slice(-64 * 1024) |
| 70 | }) |
| 71 | } |
| 72 | ps.on('message', (message: CompilerResponse) => { |
| 73 | if (this.ps !== ps || !message || message.type !== 'response') return |
| 74 | const pending = this.rpcMap.get(message.invocationId) |
| 75 | if (!pending) return |
| 76 | clearTimeout(pending.timer) |
| 77 | this.rpcMap.delete(message.invocationId) |
| 78 | if (message.error) { |
| 79 | const error = new Error(message.error.message) |
| 80 | error.name = message.error.name |
| 81 | if (message.error.stack) error.stack = message.error.stack |
| 82 | pending.reject(error) |
| 83 | } else pending.resolve(message.result) |
| 84 | }) |
| 85 | ps.on('error', error => { |
| 86 | if (this.ps === ps) this.rejectPending(error) |
| 87 | }) |
| 88 | ps.on('disconnect', () => { |
| 89 | if (this.ps === ps) this.rejectPending(new Error('Electron IPC disconnected')) |
| 90 | }) |
| 91 | ps.on('exit', (code, signal) => { |
| 92 | if (this.ps === ps) this.rejectPending(new Error('Electron exited (code ' + code + ', signal ' + signal + ')')) |
| 93 | }) |
| 94 | ps.on('close', () => { |
| 95 | if (this.ps === ps) { |
| 96 | this.rejectPending(new Error('Electron process closed')) |
| 97 | this.ps = undefined |
| 98 | } |
| 99 | }) |
| 100 | try { |
| 101 | await this.sendRequest('ready', []) |
| 102 | if (!ps.connected || this.stopping) throw new Error('Electron stopped during initialization') |
| 103 | this.ready = true |
| 104 | } catch (error) { |
| 105 | await this.stop() |
| 106 | throw error |
| 107 | } |
| 108 | } |
no test coverage detected