From c670573adb9dc046744183b702a4f9dcfd21355e Mon Sep 17 00:00:00 2001 From: Erik Moura Date: Thu, 30 Nov 2023 13:41:20 -0300 Subject: [PATCH] refactor: alternative approach --- packages/api/core/src/api/start.ts | 31 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/api/core/src/api/start.ts b/packages/api/core/src/api/start.ts index 3021b1e832..487ddfdea0 100644 --- a/packages/api/core/src/api/start.ts +++ b/packages/api/core/src/api/start.ts @@ -1,4 +1,5 @@ import { spawn, SpawnOptions } from 'child_process'; +import { EventEmitter } from 'events'; import { getElectronVersion, listrCompatibleRebuildHook } from '@electron-forge/core-utils'; import { ElectronProcess, ForgeArch, ForgeListrTaskFn, ForgePlatform, ResolvedForgeConfig, StartOptions } from '@electron-forge/shared-types'; @@ -24,6 +25,8 @@ type StartContext = { spawned: ElectronProcess; }; +const restartAppEventEmitter = new EventEmitter(); + export default autoTrace( { name: 'start()', category: '@electron-forge/core' }, async ( @@ -213,20 +216,20 @@ export default autoTrace( }; if (interactive) { - process.on('FORGE_RESTART_APP', async () => { + restartAppEventEmitter.on('restart', async () => { if (lastSpawned) { - console.info(chalk.cyan('\nRestarting App\n')); - lastSpawned.restarted = true; - lastSpawned.kill('SIGTERM'); - lastSpawned.emit('restarted', await forgeSpawnWrapper()); - } - }); + console.info(chalk.cyan('\nRestarting App\n')); + lastSpawned.restarted = true; + lastSpawned.kill('SIGTERM'); + lastSpawned.emit('restarted', await forgeSpawnWrapper()); + } + }); - process.stdin.on('data', async (data) => { - if (data.toString().trim() === 'rs') { - process.emit('FORGE_RESTART_APP'); - } - }); + process.stdin.on('data', async (data) => { + if (data.toString().trim() === 'rs') { + restartApp(); + } + }); process.stdin.resume(); } @@ -238,3 +241,7 @@ export default autoTrace( return spawned; } ); + +export function restartApp() { + restartAppEventEmitter.emit('restart'); +}