From 52ed0e28561aaaf4483dcf4d93d45c6899796817 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 15:01:18 -0500 Subject: [PATCH] webserver uses event and waterfall for triggering placeholder build, opening browser --- lib/modules/webserver/index.js | 7 ++++- lib/modules/webserver/server.js | 48 ++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 61c36eef6..cf4db794c 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -24,8 +24,13 @@ class WebServer { this.events.emit("status", __("Starting Server")); - this.server = new Server({host: this.host, port: this.port}); + this.server = new Server({ buildDir: this.buildDir, + events: this.events, + host: this.host, + port: this.port + }); + this.testPort(() => { this.listenToCommands(); this.registerConsoleCommands(); diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 7294bf031..7db78f2d7 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -1,4 +1,5 @@ let finalhandler = require('finalhandler'); +const async = require('async'); let http = require('http'); let serveStatic = require('serve-static'); const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host'); @@ -8,6 +9,7 @@ require('http-shutdown').extend(); class Server { constructor(options) { this.buildDir = options.buildDir; + this.events = options.events; this.port = options.port || 8000; this.hostname = dockerHostSwap(options.host) || defaultHost; this.isFirstStart = true; @@ -27,21 +29,41 @@ class Server { serve(req, res, finalhandler(req, res)); }).withShutdown(); + const self = this; - this.server.listen(this.port, this.hostname, () => { - this.port = this.server.address().port; - callback(null, __("webserver available at") + - " " + - ("http://" + canonicalHost(this.hostname) + - ":" + this.port).bold.underline.green, this.port); - if (!this.opened) { - // fail silently, e.g. in a docker container, by cacthing promise - // rejection w/ a noop - opn(`http://${canonicalHost(this.hostname)}:${this.port}`) - .catch(function () {}); - this.opened = true; + async.waterfall([ + function createPlaceholderPage(next) { + if (self.isFirstStart) { + self.isFirstStart = false; + return self.events.request('embark-building-placeholder', next); + } + next(); + }, + function listen(next) { + self.server.listen(self.port, self.hostname, () => { + self.port = self.server.address().port; + next(); + }); + }, + 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); + } + next(); + }, + function reportAvailable(next) { + next(null, __("webserver available at") + + " " + + ("http://" + canonicalHost(self.hostname) + + ":" + self.port).bold.underline.green, self.port); } - }); + ], callback); } stop(callback) {