AFFiNE/packages/frontend/electron/scripts/dev.ts

106 lines
2.8 KiB
TypeScript
Raw Normal View History

import type { ChildProcessWithoutNullStreams } from 'node:child_process';
import { spawn } from 'node:child_process';
import { resolve } from 'node:path';
2023-03-16 17:58:21 +03:00
import type { BuildContext } from 'esbuild';
2023-03-16 17:58:21 +03:00
import * as esbuild from 'esbuild';
import kill from 'tree-kill';
2023-03-16 17:58:21 +03:00
import { config, electronDir, rootDir } from './common';
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/,
];
let spawnProcess: ChildProcessWithoutNullStreams | null = null;
2023-03-16 17:58:21 +03:00
function spawnOrReloadElectron() {
if (watchMode) {
return;
}
if (spawnProcess !== null && spawnProcess.pid) {
2023-03-16 17:58:21 +03:00
spawnProcess.off('exit', process.exit);
kill(spawnProcess.pid);
2023-03-16 17:58:21 +03:00
spawnProcess = null;
}
const ext = process.platform === 'win32' ? '.cmd' : '';
const exe = resolve(rootDir, 'node_modules', '.bin', `electron${ext}`);
spawnProcess = spawn(exe, ['.'], {
cwd: electronDir,
env: process.env,
shell: true,
});
2023-03-16 17:58:21 +03:00
spawnProcess.stdout.on('data', d => {
const 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
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-13 05:01:43 +03:00
async function watchLayers() {
let initialBuild = false;
return new Promise<BuildContext>(resolve => {
const buildContextPromise = esbuild.context({
...common,
2023-03-16 17:58:21 +03:00
plugins: [
...(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 {
buildContextPromise.then(resolve);
2023-03-16 17:58:21 +03:00
initialBuild = true;
}
});
},
},
],
});
buildContextPromise.then(async buildContext => {
await buildContext.watch();
});
});
}
await watchLayers();
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
}