Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import readline from 'node:readline';
import {
getElectronVersion,
listrCompatibleRebuildHook,
onAppRestart,
restartApp,
} from '@electron-forge/core-utils';
import {
ElectronProcess,
Expand Down Expand Up @@ -285,20 +287,26 @@ export default autoTrace(
return lastSpawned;
};

onAppRestart(() => {
if (lastSpawned && !lastSpawned.restarted) {
console.info(
`${chalk.green('✔ ')}${chalk.dim('Restarting Electron app')}`,
);
lastSpawned.restarted = true;
lastSpawned.on('exit', async () => {
lastSpawned!.emit('restarted', await forgeSpawnWrapper());
});
lastSpawned.kill('SIGTERM');
}
});

if (interactive) {
process.stdin.on('data', (data) => {
if (data.toString().trim() === 'rs' && lastSpawned) {
readline.moveCursor(process.stdout, 0, -1);
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
console.info(
`${chalk.green('✔ ')}${chalk.dim('Restarting Electron app')}`,
);
lastSpawned.restarted = true;
lastSpawned.on('exit', async () => {
lastSpawned!.emit('restarted', await forgeSpawnWrapper());
});
lastSpawned.kill('SIGTERM');
restartApp();
}
});
process.stdin.resume();
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"main": "dist/VitePlugin.js",
"typings": "dist/VitePlugin.d.ts",
"dependencies": {
"@electron-forge/core-utils": "workspace:*",
"@electron-forge/plugin-base": "workspace:*",
"@electron-forge/shared-types": "workspace:*",
"chalk": "^4.0.0",
Expand Down
7 changes: 3 additions & 4 deletions packages/plugin/vite/src/config/vite.base.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { builtinModules } from 'node:module';

import { restartApp } from '@electron-forge/core-utils';

import type { AddressInfo } from 'node:net';
import type { ConfigEnv, Plugin, UserConfig, ViteDevServer } from 'vite';

Expand Down Expand Up @@ -103,10 +105,7 @@ export function pluginHotRestart(command: 'reload' | 'restart'): Plugin {
server.ws.send({ type: 'full-reload' });
}
} else if (command === 'restart') {
// Main process hot restart.
// https://github.com/electron/forge/blob/v7.2.0/packages/api/core/src/api/start.ts#L216-L223
// TODO: blocked in #3380
// process.stdin.emit('data', 'rs');
restartApp();
}
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/utils/core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './rebuild';
export * from './electron-version';
export * from './package-manager';
export * from './author-name';
export * from './restart';
21 changes: 21 additions & 0 deletions packages/utils/core-utils/src/restart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EventEmitter } from 'node:events';

const restartEmitter = new EventEmitter();

/**
* Signal the running Electron app to restart.
* Called by plugins (e.g. Vite) when the main process bundle is rebuilt.
*/
export function restartApp(): void {
restartEmitter.emit('restart');
}

/**
* Register a listener for app restart signals.
* Called by the `start` API to wire up the actual restart logic.
* Replaces any previously registered listener to avoid leaks.
*/
export function onAppRestart(listener: () => void): void {
restartEmitter.removeAllListeners('restart');
restartEmitter.on('restart', listener);
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@electron-forge/plugin-vite@workspace:packages/plugin/vite"
dependencies:
"@electron-forge/core-utils": "workspace:*"
"@electron-forge/plugin-base": "workspace:*"
"@electron-forge/shared-types": "workspace:*"
"@electron/packager": "npm:^18.3.5"
Expand Down
Loading