From 7bf4fa79cd60ba8f129ab62fa201ab469f245d6c Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 9 Jan 2024 23:43:50 +0800 Subject: [PATCH] fix: windows exit immediate (#4) --- lib/index.js | 6 ++++-- lib/process.js | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index bd645f5..06003dc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -56,11 +56,13 @@ function run() { }, }); - function onSignal() { + async function onSignal() { try { restart.clear(); child.kill(); - runChild && runChild.kill(); + if (runChild) { + await runChild.kill(); + } process.exit(0); } catch (err) { console.error(err); diff --git a/lib/process.js b/lib/process.js index 2876884..b534e15 100644 --- a/lib/process.js +++ b/lib/process.js @@ -79,10 +79,15 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => { innerFork(true); - const killRunningChild = signal => { + const killRunningChild = async signal => { if (isWin) { - runChild.send({ - title: 'server-kill', + await new Promise(resolve => { + runChild.once('exit', (code, signal) => { + resolve(); + }); + runChild.send({ + title: 'server-kill', + }); }); } else { runChild.kill(signal); @@ -90,12 +95,12 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => { }; return { - kill(signal) { - killRunningChild(signal); + async kill(signal) { + await killRunningChild(signal); }, - restart() { + async restart() { // 杀进程 - killRunningChild(); + await killRunningChild(); // 重新拉起来 innerFork(); },