From 797b0bb045b4288fddbc64e33f68d9896b03f1f9 Mon Sep 17 00:00:00 2001 From: Michael Yin Date: Wed, 12 Jun 2019 16:40:53 +1200 Subject: [PATCH] Shutdown all applications on exit --- bin/app.js | 20 +++++++++++++++++--- bin/cli.js | 1 + lib/app.js | 4 ++++ lib/localhostd.js | 6 ++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bin/app.js b/bin/app.js index 19d9f82..085da29 100644 --- a/bin/app.js +++ b/bin/app.js @@ -15,6 +15,13 @@ electron.app.on("ready", () => tray.setToolTip("LocalhostD is running..."); tray.on("double-click", () => app.showWindow()); + const quit = async () => { + // eslint-disable-next-line no-console + console.log("quiting..."); + await app.close(); + electron.app.exit(); + }; + tray.setContextMenu( electron.Menu.buildFromTemplate([ { @@ -26,7 +33,7 @@ electron.app.on("ready", () => { label: "Quit", click: () => { - electron.app.exit(); + quit(); } } ]) @@ -38,13 +45,20 @@ electron.app.on("ready", () => app.showWindow(); }); - electron.app.on("window-all-closed", () => {}); + electron.app.on("window-all-closed", event => { + event.preventDefault(); + }); + + electron.app.on("before-quit", event => { + event.preventDefault(); + quit(); + }); const signal = await waitDeath(); // eslint-disable-next-line no-console console.log(`received ${signal}`); - electron.app.exit(); + await quit(); }) .catch(error => { // eslint-disable-next-line no-console diff --git a/bin/cli.js b/bin/cli.js index 7d70f70..411a152 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -43,6 +43,7 @@ Commander.command("server") // eslint-disable-next-line no-console console.log(`received ${signal}`); + await server.close(); process.exit(0); }) diff --git a/lib/app.js b/lib/app.js index 43a5d38..3d41a40 100644 --- a/lib/app.js +++ b/lib/app.js @@ -91,6 +91,10 @@ class App { autoUpdater.checkForUpdatesAndNotify(); } + async close() { + await this._server.close(); + } + showWindow() { if (process.platform === "darwin") app.dock.show(); if (this._window) return this._window.focus(); diff --git a/lib/localhostd.js b/lib/localhostd.js index d96a4e0..0ff8d08 100644 --- a/lib/localhostd.js +++ b/lib/localhostd.js @@ -282,9 +282,7 @@ class LocalhostD { }); } catch (error) { throw new Error( - `${ - error.message - }: Cannot find executable ${executable} in PATH (${envPath})` + `${error.message}: Cannot find executable ${executable} in PATH (${envPath})` ); } @@ -410,7 +408,7 @@ class LocalhostD { } async ensureDown() { - for (const application of this.state.applications) + for (const application of this._state.applications) await this._ensureApplicationDown(application); }