AFFiNE/apps/electron/scripts/dev.mjs

95 lines
2.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-async-promise-executor */
import { spawn } from 'node:child_process';
2023-03-16 17:58:21 +03:00
import electronPath from 'electron';
import * as esbuild from 'esbuild';
import { config } from './common.mjs';
2023-03-16 17:58:21 +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/,
];
/** @type {ChildProcessWithoutNullStreams | null} */
let spawnProcess = null;
function spawnOrReloadElectron() {
if (watchMode) {
return;
}
2023-03-16 17:58:21 +03:00
if (spawnProcess !== null) {
spawnProcess.off('exit', process.exit);
spawnProcess.kill('SIGINT');
spawnProcess = null;
}
spawnProcess = spawn(String(electronPath), ['.']);
spawnProcess.stdout.on('data', d => {
let str = d.toString().trim();
if (str) {
console.log(str);
}
});
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;
console.error(data);
2023-03-16 17:58:21 +03:00
});
// Stops the watch script when the application has quit
2023-03-16 17:58:21 +03:00
spawnProcess.on('exit', process.exit);
}
2023-04-25 02:53:36 +03:00
const common = config();
2023-06-13 05:01:43 +03:00
async function watchLayers() {
return new Promise(async resolve => {
let initialBuild = false;
2023-06-13 05:01:43 +03:00
const buildContext = await esbuild.context({
...common.layers,
2023-03-16 17:58:21 +03:00
plugins: [
2023-06-13 05:01:43 +03:00
...(common.layers.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 {
resolve();
2023-03-16 17:58:21 +03:00
initialBuild = true;
}
});
},
},
],
});
2023-06-13 05:01:43 +03:00
await buildContext.watch();
});
}
async function main() {
2023-06-13 05:01:43 +03:00
await watchLayers();
if (watchMode) {
console.log(`Watching for changes...`);
} else {
2023-03-16 17:58:21 +03:00
spawnOrReloadElectron();
console.log(`Electron is started, watching for changes...`);
}
2023-03-16 17:58:21 +03:00
}
main();