|
85 | 85 | } |
86 | 86 | // Electron detection and setup |
87 | 87 | if(window.electronAPI){ |
88 | | - window.electronAPI.getNodeWSPort().then(port => { |
89 | | - window.nodeWSEndpoint = `ws://localhost:${port}/phoenixFS`; |
90 | | - fs.setNodeWSEndpoint(`ws://localhost:${port}/phoenixFS`); |
91 | | - window.isNodeSetup = true; |
92 | | - }); |
93 | | - // Heartbeat to keep Node server alive |
94 | | - setInterval(() => { |
95 | | - // Heartbeat is handled by main process in Electron |
96 | | - }, 10000); |
| 88 | + window.electronAPI.getDocumentsDir().then(d => console.log("Electron documentsDir:", d)); |
| 89 | + window.electronAPI.getAppDataDir().then(d => console.log("Electron appDataDir:", d)); |
| 90 | + window.electronAPI.getAppPath().then(appPath => { |
| 91 | + const nodeSrcPath = appPath + "/../src-tauri/node-src/index.js"; |
| 92 | + return window.electronAPI.spawnProcess('node', ['--inspect', nodeSrcPath]); |
| 93 | + }).then(async (myInstanceId) => { |
| 94 | + window.electronAPI.onProcessClose((instanceId, data) => { |
| 95 | + if(instanceId !== myInstanceId) return; |
| 96 | + window.isNodeTerminated = true; |
| 97 | + console.log(`Node: command finished with code ${data.code} and signal ${data.signal}`); |
| 98 | + }); |
| 99 | + window.electronAPI.onProcessStdout((instanceId, line) => { |
| 100 | + if(instanceId !== myInstanceId) return; |
| 101 | + if(line){ |
| 102 | + if(line.trim().startsWith("{")){ |
| 103 | + const jsonMsg = JSON.parse(line); |
| 104 | + pendingCommands[jsonMsg.commandId].resolve(jsonMsg.message); |
| 105 | + delete pendingCommands[jsonMsg.commandId]; |
| 106 | + } else { |
| 107 | + console.log(`Node: ${line}`); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + window.electronAPI.onProcessStderr((instanceId, line) => { |
| 112 | + if(instanceId !== myInstanceId) return; |
| 113 | + console.error(`Node: ${line}`); |
| 114 | + }); |
| 115 | + |
| 116 | + window.execNode = function (commandCode) { |
| 117 | + const newCommandID = commandId++; |
| 118 | + window.electronAPI.writeToProcess(myInstanceId, |
| 119 | + JSON.stringify({commandCode: commandCode, commandId: newCommandID}) + "\n" |
| 120 | + ); |
| 121 | + let resolveP, rejectP; |
| 122 | + const promise = new Promise((resolve, reject) => {resolveP = resolve; rejectP = reject;}); |
| 123 | + pendingCommands[newCommandID] = {resolve: resolveP, reject: rejectP}; |
| 124 | + return promise; |
| 125 | + }; |
| 126 | + |
| 127 | + execNode(NODE_COMMANDS.GET_PORT) |
| 128 | + .then(message => { |
| 129 | + window.nodeWSEndpoint = `ws://localhost:${message.port}/phoenixFS`; |
| 130 | + fs.setNodeWSEndpoint(`ws://localhost:${message.port}/phoenixFS`); |
| 131 | + window.isNodeSetup = true; |
| 132 | + }); |
| 133 | + |
| 134 | + setInterval(() => { |
| 135 | + if(!window.isNodeTerminated) { |
| 136 | + execNode(NODE_COMMANDS.HEART_BEAT); |
| 137 | + } |
| 138 | + }, 10000); |
| 139 | + }).catch(console.error); |
97 | 140 | } |
98 | 141 | </script> |
99 | 142 | <script src="thirdparty/chai@4.2.0.js"></script> |
|
150 | 193 | }; |
151 | 194 | quitIfNeeded = async function(exitStatus) { |
152 | 195 | if(!location.href.startsWith("http://localhost:8081/test/")) { |
153 | | - // during development, we dont quit at end of tests for ease of development and debug |
| 196 | + // during development, we dont switch off node at end of tests for ease of development and debug |
154 | 197 | fs.stopNodeWSEndpoint(); |
| 198 | + window.execNode(NODE_COMMANDS.TERMINATE); |
| 199 | + await waitForTrue(() => { return window.isNodeTerminated; }, 1000); |
155 | 200 | } |
156 | 201 | // Always quit in Electron since there's no CLI arg parsing |
157 | 202 | window.electronAPI.quitApp(exitStatus); |
|
0 commit comments