2024-01-19 06:47:08 +03:00
|
|
|
import type { ChildProcessWithoutNullStreams } from 'node:child_process';
|
2023-05-29 07:53:15 +03:00
|
|
|
import { spawn } from 'node:child_process';
|
2023-11-27 11:45:33 +03:00
|
|
|
import { resolve } from 'node:path';
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-10-19 06:14:30 +03:00
|
|
|
import type { BuildContext } from 'esbuild';
|
2023-03-16 17:58:21 +03:00
|
|
|
import * as esbuild from 'esbuild';
|
2023-12-11 07:26:19 +03:00
|
|
|
import kill from 'tree-kill';
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-11-27 11:45:33 +03:00
|
|
|
import { config, electronDir, rootDir } from './common';
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
// this means we don't spawn electron windows, mainly for testing
|
|
|
|
const watchMode = process.argv.includes('--watch');
|
2023-03-16 17:58:21 +03:00
|
|
|
|
|
|
|
/** Messages on stderr that match any of the contained patterns will be stripped from output */
|
|
|
|
const stderrFilterPatterns = [
|
|
|
|
// warning about devtools extension
|
|
|
|
// https://github.com/cawa-93/vite-electron-builder/issues/492
|
|
|
|
// https://github.com/MarshallOfSound/electron-devtools-installer/issues/143
|
|
|
|
/ExtensionLoadWarning/,
|
|
|
|
];
|
|
|
|
|
2023-10-18 09:13:47 +03:00
|
|
|
let spawnProcess: ChildProcessWithoutNullStreams | null = null;
|
2023-03-16 17:58:21 +03:00
|
|
|
|
|
|
|
function spawnOrReloadElectron() {
|
2023-05-09 10:30:01 +03:00
|
|
|
if (watchMode) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-11 07:26:19 +03:00
|
|
|
if (spawnProcess !== null && spawnProcess.pid) {
|
2023-03-16 17:58:21 +03:00
|
|
|
spawnProcess.off('exit', process.exit);
|
2023-12-11 07:26:19 +03:00
|
|
|
kill(spawnProcess.pid);
|
2023-03-16 17:58:21 +03:00
|
|
|
spawnProcess = null;
|
|
|
|
}
|
|
|
|
|
2023-11-27 11:45:33 +03:00
|
|
|
const ext = process.platform === 'win32' ? '.cmd' : '';
|
|
|
|
const exe = resolve(rootDir, 'node_modules', '.bin', `electron${ext}`);
|
|
|
|
|
|
|
|
spawnProcess = spawn(exe, ['.'], {
|
2023-10-19 06:14:30 +03:00
|
|
|
cwd: electronDir,
|
|
|
|
env: process.env,
|
2024-04-16 09:57:17 +03:00
|
|
|
shell: true,
|
2023-10-19 06:14:30 +03:00
|
|
|
});
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-04-23 09:42:52 +03:00
|
|
|
spawnProcess.stdout.on('data', d => {
|
2023-10-18 09:13:47 +03:00
|
|
|
const str = d.toString().trim();
|
2023-04-23 09:42:52 +03:00
|
|
|
if (str) {
|
|
|
|
console.log(str);
|
|
|
|
}
|
|
|
|
});
|
2023-10-19 06:14:30 +03:00
|
|
|
|
2023-03-16 17:58:21 +03:00
|
|
|
spawnProcess.stderr.on('data', d => {
|
|
|
|
const data = d.toString().trim();
|
|
|
|
if (!data) return;
|
|
|
|
const mayIgnore = stderrFilterPatterns.some(r => r.test(data));
|
|
|
|
if (mayIgnore) return;
|
2023-04-11 21:42:36 +03:00
|
|
|
console.error(data);
|
2023-03-16 17:58:21 +03:00
|
|
|
});
|
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
// Stops the watch script when the application has quit
|
2023-10-19 06:14:30 +03:00
|
|
|
spawnProcess.on('exit', code => {
|
|
|
|
if (code && code !== 0) {
|
|
|
|
console.log(`Electron exited with code ${code}`);
|
|
|
|
}
|
|
|
|
});
|
2023-03-16 17:58:21 +03:00
|
|
|
}
|
|
|
|
|
2023-04-25 02:53:36 +03:00
|
|
|
const common = config();
|
2023-06-02 11:28:47 +03:00
|
|
|
|
2023-06-13 05:01:43 +03:00
|
|
|
async function watchLayers() {
|
2023-10-19 06:14:30 +03:00
|
|
|
let initialBuild = false;
|
|
|
|
return new Promise<BuildContext>(resolve => {
|
|
|
|
const buildContextPromise = esbuild.context({
|
2023-10-18 09:13:47 +03:00
|
|
|
...common,
|
2023-03-16 17:58:21 +03:00
|
|
|
plugins: [
|
2023-10-18 09:13:47 +03:00
|
|
|
...(common.plugins ?? []),
|
2023-03-16 17:58:21 +03:00
|
|
|
{
|
2023-06-13 05:01:43 +03:00
|
|
|
name: 'electron-dev:reload-app-on-layers-change',
|
2023-03-16 17:58:21 +03:00
|
|
|
setup(build) {
|
|
|
|
build.onEnd(() => {
|
|
|
|
if (initialBuild) {
|
2023-06-13 05:01:43 +03:00
|
|
|
console.log(`[layers] has changed, [re]launching electron...`);
|
2023-03-16 17:58:21 +03:00
|
|
|
spawnOrReloadElectron();
|
|
|
|
} else {
|
2023-10-19 06:14:30 +03:00
|
|
|
buildContextPromise.then(resolve);
|
2023-03-16 17:58:21 +03:00
|
|
|
initialBuild = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2023-10-19 06:14:30 +03:00
|
|
|
buildContextPromise.then(async buildContext => {
|
|
|
|
await buildContext.watch();
|
|
|
|
});
|
2023-05-09 10:30:01 +03:00
|
|
|
});
|
|
|
|
}
|
2023-04-23 09:42:52 +03:00
|
|
|
|
2023-10-19 06:14:30 +03:00
|
|
|
await watchLayers();
|
2023-05-09 10:30:01 +03:00
|
|
|
|
2023-10-19 06:14:30 +03:00
|
|
|
if (watchMode) {
|
|
|
|
console.log(`Watching for changes...`);
|
|
|
|
} else {
|
|
|
|
console.log('Starting electron...');
|
|
|
|
spawnOrReloadElectron();
|
|
|
|
console.log(`Electron is started, watching for changes...`);
|
2023-03-16 17:58:21 +03:00
|
|
|
}
|