From 68c6d1549a224fcd13c8dcb33639fe2852e6e643 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 07:34:37 -0500 Subject: [PATCH] 'open-browser' event --- lib/modules/webserver/index.js | 10 ++++++++++ lib/modules/webserver/server.js | 8 +------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 54ae25c3..e9095bbe 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -2,6 +2,7 @@ const fs = require('../../core/fs.js'); var {canonicalHost} = require('../../utils/host.js'); var utils = require('../../utils/utils.js'); var Server = require('./server.js'); +const opn = require('opn'); require('ejs'); const Templates = { @@ -73,6 +74,7 @@ class WebServer { listenToCommands() { this.events.setCommandHandler('embark-building-placeholder', (cb) => this.buildPlaceholderPage(cb)); + this.events.setCommandHandler('open-browser', (cb) => this.openBrowser(cb)); this.events.setCommandHandler('start-webserver', (cb) => this.server.start(cb)); this.events.setCommandHandler('stop-webserver', (cb) => this.server.stop(cb)); } @@ -102,6 +104,14 @@ class WebServer { fs.mkdirpSync(this.buildDir); // create buildDir/ folder if not already exists fs.writeFile(utils.joinPath(this.buildDir, 'index.html'), html, cb); } + + openBrowser(cb) { + const _cb = () => { cb(); }; + return opn( + `http://${canonicalHost(this.server.hostname)}:${this.server.port}`, + {wait: false} + ).then(_cb, _cb); // fail silently, e.g. in a docker container + } } module.exports = WebServer; diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index c6112632..f0c1a681 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -3,7 +3,6 @@ const async = require('async'); let http = require('http'); let serveStatic = require('serve-static'); const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host'); -const opn = require('opn'); require('http-shutdown').extend(); class Server { @@ -48,12 +47,7 @@ class Server { function openBrowser(next) { if (!self.opened) { self.opened = true; - const _next = () => { next(); }; - // fail silently, e.g. in a docker container - return opn( - `http://${canonicalHost(self.hostname)}:${self.port}`, - {wait: false} - ).then(_next, _next); + return self.events.request('open-browser', next); } next(); },