aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-04-05 01:41:43 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-05-16 00:55:00 +0200
commit7371f3ecf70039f3128e64124eade6c1a18a7564 (patch)
treefc2a09a68de6d5d3b01ca5da118ce8a169e72d60 /scripts
parentfix(main): Fix typo in ConfigFile log message (diff)
downloadsophie-7371f3ecf70039f3128e64124eade6c1a18a7564.tar.gz
sophie-7371f3ecf70039f3128e64124eade6c1a18a7564.tar.zst
sophie-7371f3ecf70039f3128e64124eade6c1a18a7564.zip
build: Properly shut down watch mode
Also fixes argument passing to the application. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/watch.js69
1 files changed, 53 insertions, 16 deletions
diff --git a/scripts/watch.js b/scripts/watch.js
index 9fa6a57..9cd65c8 100644
--- a/scripts/watch.js
+++ b/scripts/watch.js
@@ -1,5 +1,6 @@
1import { spawn } from 'node:child_process'; 1import { spawn } from 'node:child_process';
2import path from 'node:path'; 2import path from 'node:path';
3import { exit, kill } from 'node:process';
3 4
4import { watch } from 'chokidar'; 5import { watch } from 'chokidar';
5import electronPath from 'electron'; 6import electronPath from 'electron';
@@ -171,31 +172,67 @@ function setupMainPackageWatcher(viteDevServer) {
171 172
172 /** @type {import('child_process').ChildProcessByStdio<null, null, import('stream').Readable> 173 /** @type {import('child_process').ChildProcessByStdio<null, null, import('stream').Readable>
173 | undefined} */ 174 | undefined} */
174 let spawnProcess; 175 let childProcess;
176
177 // Prevent overlapping restarts and restarting while exiting.
178 let mayRestart = true;
179
180 function spawnProcess() {
181 childProcess = spawn(
182 String(electronPath),
183 ['.', ...process.argv.slice(2)],
184 {
185 stdio: ['inherit', 'inherit', 'pipe'],
186 },
187 );
188
189 childProcess.stderr.on('data', (/** @type {Buffer} */ data) => {
190 const stderrString = data.toString('utf8').trimEnd();
191 if (!stderrIgnorePatterns.some((r) => r.test(stderrString))) {
192 console.error(stderrString);
193 }
194 });
195
196 childProcess.on('exit', () => process.exit());
197 }
198
199 function killProcess() {
200 mayRestart = false;
201 if (childProcess === undefined) {
202 return;
203 }
204 childProcess.stderr.removeAllListeners('data');
205 childProcess.kill('SIGINT');
206 }
207
208 process.on('SIGINT', () => {
209 if (childProcess !== undefined) {
210 childProcess.removeAllListeners('exit');
211 childProcess.on('exit', process.exit());
212 killProcess();
213 }
214 });
175 215
176 return setupEsbuildWatcher( 216 return setupEsbuildWatcher(
177 'main', 217 'main',
178 [serviceSharedModule, sharedModule], 218 [serviceSharedModule, sharedModule],
179 () => { 219 () => {
180 if (spawnProcess !== undefined) { 220 if (!mayRestart) {
181 spawnProcess.kill('SIGINT'); 221 // We're already about to restart electron.
182 spawnProcess = undefined; 222 return;
183 } 223 }
184 224
185 spawnProcess = spawn( 225 if (childProcess === undefined) {
186 String(electronPath), 226 spawnProcess();
187 ['.', ...process.argv.splice(2)], 227 return;
188 { 228 }
189 stdio: ['inherit', 'inherit', 'pipe'],
190 },
191 );
192 229
193 spawnProcess.stderr.on('data', (/** @type {Buffer} */ data) => { 230 childProcess.removeAllListeners('exit');
194 const stderrString = data.toString('utf8').trimEnd(); 231 childProcess.on('exit', () => {
195 if (!stderrIgnorePatterns.some((r) => r.test(stderrString))) { 232 spawnProcess();
196 console.error(stderrString); 233 mayRestart = true;
197 }
198 }); 234 });
235 killProcess();
199 }, 236 },
200 ); 237 );
201} 238}