From b47435cc4533e78f8ce4b012f0c8791e7d6a77b6 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 11:06:03 -0500 Subject: [PATCH 01/18] opn dep --- package-lock.json | 13 +++++++++++++ package.json | 1 + 2 files changed, 14 insertions(+) diff --git a/package-lock.json b/package-lock.json index 1282b0a8..a6076bd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5272,6 +5272,11 @@ "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==" }, + "is-wsl": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz", + "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -6698,6 +6703,14 @@ "mimic-fn": "^1.0.0" } }, + "opn": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/opn/-/opn-5.3.0.tgz", + "integrity": "sha512-bYJHo/LOmoTd+pfiYhfZDnf9zekVJrY+cnS2a5F2x+w5ppvTqObojTP7WiFG+kVZs9Inw+qQ/lw7TroWwhdd2g==", + "requires": { + "is-wsl": "^1.1.0" + } + }, "optimist": { "version": "0.3.7", "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz", diff --git a/package.json b/package.json index dad9bcd3..a196ea05 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "netcat": "^1.3.5", "node-ipc": "^9.1.1", "node-netcat": "^1.4.8", + "opn": "^5.3.0", "ora": "^2.1.0", "os-locale": "^2.1.0", "parse-json": "^4.0.0", From f778dad5895383f9691400394b05e3f33bb7d523 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 11:06:46 -0500 Subject: [PATCH 02/18] write placeholder page when webserver is first starting --- lib/modules/webserver/server.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 340f5307..c0d8d94f 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -1,9 +1,15 @@ let finalhandler = require('finalhandler'); +const fs = require('../../core/fs.js'); let http = require('http'); let serveStatic = require('serve-static'); const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host'); +const utils = require('../../utils/utils.js'); require('http-shutdown').extend(); +require('ejs'); +const embark_building_placeholder = require('../code_generator/code_templates/embark-building-placeholder.html.ejs'); + +let isFirstStart = true; class Server { constructor(options) { this.dist = options.dist || 'dist/'; @@ -24,6 +30,12 @@ class Server { serve(req, res, finalhandler(req, res)); }).withShutdown(); + if (isFirstStart) { + const html = embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); + fs.mkdirpSync(this.dist); // create dist/ folder if not already exists + fs.writeFileSync(utils.joinPath(this.dist, 'index.html'), html); + isFirstStart = false; + } this.server.listen(this.port, this.hostname, () => { this.port = this.server.address().port; From 72943dc0e52c7f6f01c2d44ae7364d1039b1434b Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 11:07:31 -0500 Subject: [PATCH 03/18] open default browser when webserver has started --- lib/modules/webserver/server.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index c0d8d94f..53eda52c 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -3,6 +3,7 @@ const fs = require('../../core/fs.js'); let http = require('http'); let serveStatic = require('serve-static'); const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host'); +const opn = require('opn'); const utils = require('../../utils/utils.js'); require('http-shutdown').extend(); @@ -10,6 +11,8 @@ require('ejs'); const embark_building_placeholder = require('../code_generator/code_templates/embark-building-placeholder.html.ejs'); let isFirstStart = true; +let opened = false; + class Server { constructor(options) { this.dist = options.dist || 'dist/'; @@ -43,6 +46,10 @@ class Server { " " + ("http://" + canonicalHost(this.hostname) + ":" + this.port).bold.underline.green, this.port); + if (!opened) { + opn(`http://${canonicalHost(this.hostname)}:${this.port}`); + opened = true; + } }); } From 4f8107bf17e2c68cf1c1cc685e5614f98515dc02 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 11:16:57 -0500 Subject: [PATCH 04/18] fail silently if opn doesn't work --- lib/modules/webserver/server.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 53eda52c..361e67ec 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -47,7 +47,10 @@ class Server { ("http://" + canonicalHost(this.hostname) + ":" + this.port).bold.underline.green, this.port); if (!opened) { - opn(`http://${canonicalHost(this.hostname)}:${this.port}`); + // fail silently, e.g. in a docker container, by cacthing promise + // rejection w/ a noop + opn(`http://${canonicalHost(this.hostname)}:${this.port}`) + .catch(function () {}); opened = true; } }); From 1d9237820adad40fe746f7e9382472479e54ff3e Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 11:46:41 -0500 Subject: [PATCH 05/18] make isFirstStart, opened props of Server instance --- lib/modules/webserver/server.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 361e67ec..9c3e9059 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -10,14 +10,13 @@ require('http-shutdown').extend(); require('ejs'); const embark_building_placeholder = require('../code_generator/code_templates/embark-building-placeholder.html.ejs'); -let isFirstStart = true; -let opened = false; - class Server { constructor(options) { this.dist = options.dist || 'dist/'; this.port = options.port || 8000; this.hostname = dockerHostSwap(options.host) || defaultHost; + this.isFirstStart = true; + this.opened = false; } start(callback) { @@ -33,11 +32,11 @@ class Server { serve(req, res, finalhandler(req, res)); }).withShutdown(); - if (isFirstStart) { + if (this.isFirstStart) { const html = embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); fs.mkdirpSync(this.dist); // create dist/ folder if not already exists fs.writeFileSync(utils.joinPath(this.dist, 'index.html'), html); - isFirstStart = false; + this.isFirstStart = false; } this.server.listen(this.port, this.hostname, () => { @@ -46,12 +45,12 @@ class Server { " " + ("http://" + canonicalHost(this.hostname) + ":" + this.port).bold.underline.green, this.port); - if (!opened) { + 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 () {}); - opened = true; + this.opened = true; } }); } From 1d38af1f58d17b3d595c033538a33fb1fd136a1d Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 14:51:18 -0500 Subject: [PATCH 06/18] expose buildDir to webserver in same manner as its done for pipeline --- lib/core/engine.js | 1 + lib/modules/webserver/index.js | 2 ++ lib/modules/webserver/server.js | 4 ++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 37706cd2..822ddf6d 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -228,6 +228,7 @@ class Engine { } webServerService(_options) { + _options.buildDir = this.config.buildDir; this.registerModule('webserver', _options); } diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index a0b94850..3f335ea8 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -8,6 +8,7 @@ class WebServer { this.embark = embark; this.logger = embark.logger; this.events = embark.events; + this.buildDir = options.buildDir; this.webServerConfig = embark.config.webServerConfig; if (!this.webServerConfig.enabled) { return; @@ -19,6 +20,7 @@ class WebServer { this.events.emit("status", __("Starting Server")); this.server = new Server({host: this.host, port: this.port}); + buildDir: this.buildDir, this.testPort(() => { this.listenToCommands(); this.registerConsoleCommands(); diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 9c3e9059..73ba07a7 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -12,7 +12,7 @@ const embark_building_placeholder = require('../code_generator/code_templates/em class Server { constructor(options) { - this.dist = options.dist || 'dist/'; + this.buildDir = options.buildDir; this.port = options.port || 8000; this.hostname = dockerHostSwap(options.host) || defaultHost; this.isFirstStart = true; @@ -26,7 +26,7 @@ class Server { ":" + this.port).bold.underline.green; return callback(null, message); } - let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']}); + let serve = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']}); this.server = http.createServer(function onRequest(req, res) { serve(req, res, finalhandler(req, res)); From 6ee195aba917ad23c0fa6cb8abca33fd878c73b4 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 14:59:54 -0500 Subject: [PATCH 07/18] make webserver module responsible for building placeholder --- lib/modules/code_generator/index.js | 13 +------------ lib/modules/webserver/index.js | 14 +++++++++++++- lib/modules/webserver/server.js | 11 ----------- .../embark-building-placeholder.html.ejs | 0 lib/pipeline/pipeline.js | 4 ---- 5 files changed, 14 insertions(+), 28 deletions(-) rename lib/modules/{code_generator/code_templates => webserver/templates}/embark-building-placeholder.html.ejs (100%) diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index ecc1aee5..371f6968 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -13,8 +13,7 @@ const Templates = { define_web3_simple: require('./code_templates/define-web3-simple.js.ejs'), web3_connector: require('./code_templates/web3-connector.js.ejs'), do_when_loaded: require('./code_templates/do-when-loaded.js.ejs'), - exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.ejs'), - embark_building_placeholder: require('./code_templates/embark-building-placeholder.html.ejs') + exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.ejs') }; class CodeGenerator { @@ -94,10 +93,6 @@ class CodeGenerator { cb(self.generateContractCode(contract, gasLimit)); }); - this.events.setCommandHandler('embark-building-placeholder', (cb) => { - self.buildPlaceholderPage(cb); - }); - self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => { cb(self.getEmbarkJsProviderCode()); }); @@ -379,12 +374,6 @@ class CodeGenerator { } ], cb); } - - buildPlaceholderPage(cb) { - let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); - cb(html); - } - } module.exports = CodeGenerator; diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 3f335ea8..61c36eef 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -1,9 +1,14 @@ +const fs = require('../../core/fs.js'); var {canonicalHost} = require('../../utils/host.js'); var utils = require('../../utils/utils.js'); var Server = require('./server.js'); -class WebServer { +require('ejs'); +const Templates = { + embark_building_placeholder: require('./templates/embark-building-placeholder.html.ejs') +}; +class WebServer { constructor(embark, options) { this.embark = embark; this.logger = embark.logger; @@ -62,6 +67,7 @@ class WebServer { } listenToCommands() { + this.events.setCommandHandler('embark-building-placeholder', (cb) => this.buildPlaceholderPage(cb)); this.events.setCommandHandler('start-webserver', (callback) => this.server.start(callback)); this.events.setCommandHandler('stop-webserver', (callback) => this.server.stop(callback)); } @@ -82,6 +88,12 @@ class WebServer { }; }); } + + buildPlaceholderPage(cb) { + let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); + fs.mkdirpSync(this.buildDir); // create buildDir/ folder if not already exists + fs.writeFile(utils.joinPath(this.buildDir, 'index.html'), html, cb); + } } module.exports = WebServer; diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 73ba07a7..7294bf03 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -1,15 +1,10 @@ let finalhandler = require('finalhandler'); -const fs = require('../../core/fs.js'); let http = require('http'); let serveStatic = require('serve-static'); const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host'); const opn = require('opn'); -const utils = require('../../utils/utils.js'); require('http-shutdown').extend(); -require('ejs'); -const embark_building_placeholder = require('../code_generator/code_templates/embark-building-placeholder.html.ejs'); - class Server { constructor(options) { this.buildDir = options.buildDir; @@ -32,12 +27,6 @@ class Server { serve(req, res, finalhandler(req, res)); }).withShutdown(); - if (this.isFirstStart) { - const html = embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); - fs.mkdirpSync(this.dist); // create dist/ folder if not already exists - fs.writeFileSync(utils.joinPath(this.dist, 'index.html'), html); - this.isFirstStart = false; - } this.server.listen(this.port, this.hostname, () => { this.port = this.server.address().port; diff --git a/lib/modules/code_generator/code_templates/embark-building-placeholder.html.ejs b/lib/modules/webserver/templates/embark-building-placeholder.html.ejs similarity index 100% rename from lib/modules/code_generator/code_templates/embark-building-placeholder.html.ejs rename to lib/modules/webserver/templates/embark-building-placeholder.html.ejs diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 1f0a742b..7d2b7538 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -29,10 +29,6 @@ class Pipeline { async.waterfall([ function createPlaceholderPage(next){ - self.events.request('embark-building-placeholder', (html) => { - fs.mkdirpSync(self.buildDir); // create dist/ folder if not already exists - fs.writeFile(utils.joinPath(self.buildDir, 'index.html'), html, next); - }); }, function buildTheContracts(next) { self.buildContracts(next); From 52ed0e28561aaaf4483dcf4d93d45c6899796817 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 15:01:18 -0500 Subject: [PATCH 08/18] 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 61c36eef..cf4db794 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 7294bf03..7db78f2d 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) { From 9cd96ddd2970717c7b332658a8dfdb7748bc04ea Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 15:02:05 -0500 Subject: [PATCH 09/18] formatting -- webserver/server.js --- lib/modules/webserver/server.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 7db78f2d..c6112632 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -19,8 +19,8 @@ class Server { start(callback) { if (this.server && this.server.listening) { let message = __("a webserver is already running at") + " " + - ("http://" + canonicalHost(this.hostname) + - ":" + this.port).bold.underline.green; + ("http://" + canonicalHost(this.hostname) + + ":" + this.port).bold.underline.green; return callback(null, message); } let serve = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']}); @@ -74,7 +74,6 @@ class Server { callback(null, __("Webserver stopped")); }); } - } module.exports = Server; From de0034c1fc98fea6d2fedb6e0fda1d631055444f Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 15:02:19 -0500 Subject: [PATCH 10/18] formatting -- pipeline.js --- lib/pipeline/pipeline.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 7d2b7538..053ac39e 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -5,7 +5,6 @@ const utils = require('../utils/utils.js'); const constants = require('../constants'); class Pipeline { - constructor(options) { this.env = options.env; this.buildDir = options.buildDir; From 03c51fba46def1e9c935667c805c40ebd424c940 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 31 Aug 2018 15:02:42 -0500 Subject: [PATCH 11/18] pipeline fires placeholder event only after first build --- lib/pipeline/pipeline.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 053ac39e..069d4840 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -15,6 +15,7 @@ class Pipeline { this.plugins = options.plugins; this.webpackConfigName = options.webpackConfigName; this.pipelinePlugins = this.plugins.getPluginsFor('pipeline'); + this.isFirstBuild = true; } build(abi, contractsJSON, path, callback) { @@ -28,6 +29,11 @@ class Pipeline { async.waterfall([ function createPlaceholderPage(next){ + if (!self.isFirstBuild) { + return self.events.request('embark-building-placeholder', next); + } + self.isFirstBuild = false; + next(); }, function buildTheContracts(next) { self.buildContracts(next); From 103bb43a055b2482e7f60c788382252c0964f0c5 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 07:32:31 -0500 Subject: [PATCH 12/18] consistent naming, quotes-style --- lib/modules/webserver/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index cf4db794..54ae25c3 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -73,23 +73,26 @@ class WebServer { listenToCommands() { this.events.setCommandHandler('embark-building-placeholder', (cb) => this.buildPlaceholderPage(cb)); - this.events.setCommandHandler('start-webserver', (callback) => this.server.start(callback)); - this.events.setCommandHandler('stop-webserver', (callback) => this.server.stop(callback)); + this.events.setCommandHandler('start-webserver', (cb) => this.server.start(cb)); + this.events.setCommandHandler('stop-webserver', (cb) => this.server.stop(cb)); } registerConsoleCommands() { const self = this; self.embark.registerConsoleCommand((cmd, _options) => { return { - match: () => cmd === "webserver start", - process: (callback) => self.events.request("start-webserver", callback) + match: () => cmd === 'webserver start', + process: (cb) => self.events.request('start-webserver', cb) }; }); self.embark.registerConsoleCommand((cmd, _options) => { return { - match: () => cmd === "webserver stop", - process: (callback) => self.events.request("stop-webserver", callback) + match: () => cmd === 'webserver stop', + process: (cb) => self.events.request('stop-webserver', cb) + }; + }); + }; }); } From 68c6d1549a224fcd13c8dcb33639fe2852e6e643 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 07:34:37 -0500 Subject: [PATCH 13/18] '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(); }, From 57b2c5c4e4a75e475afef6f968dabc786f34c688 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 07:35:02 -0500 Subject: [PATCH 14/18] 'browser open' console command --- lib/modules/webserver/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index e9095bbe..db871008 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -95,6 +95,10 @@ class WebServer { }; }); + self.embark.registerConsoleCommand((cmd, _options) => { + return { + match: () => cmd === 'browser open', + process: (cb) => self.events.request('open-browser', cb) }; }); } From 7d65c2b0cd45ba89b1db13e9a8896b39267cf2e5 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 07:35:53 -0500 Subject: [PATCH 15/18] rename placeholder event: 'build-placeholder' --- lib/modules/webserver/index.js | 2 +- lib/modules/webserver/server.js | 2 +- lib/pipeline/pipeline.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index db871008..3abe7d35 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -73,7 +73,7 @@ class WebServer { } listenToCommands() { - this.events.setCommandHandler('embark-building-placeholder', (cb) => this.buildPlaceholderPage(cb)); + this.events.setCommandHandler('build-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)); diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index f0c1a681..97e00e8d 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -34,7 +34,7 @@ class Server { function createPlaceholderPage(next) { if (self.isFirstStart) { self.isFirstStart = false; - return self.events.request('embark-building-placeholder', next); + return self.events.request('build-placeholder', next); } next(); }, diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 069d4840..fea9fa1b 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -30,7 +30,7 @@ class Pipeline { async.waterfall([ function createPlaceholderPage(next){ if (!self.isFirstBuild) { - return self.events.request('embark-building-placeholder', next); + return self.events.request('build-placeholder', next); } self.isFirstBuild = false; next(); From c02199db0faa31854b1815fa8ad1d8f6e4c2ef46 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 07:36:21 -0500 Subject: [PATCH 16/18] rev comment --- lib/modules/webserver/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 3abe7d35..ac346511 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -105,7 +105,7 @@ class WebServer { buildPlaceholderPage(cb) { let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); - fs.mkdirpSync(this.buildDir); // create buildDir/ folder if not already exists + fs.mkdirpSync(this.buildDir); // create buildDir if it does not exist fs.writeFile(utils.joinPath(this.buildDir, 'index.html'), html, cb); } From b67aa8cc2db1c0d7c99d59f41a7b0ee05206c0f3 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 08:20:27 -0500 Subject: [PATCH 17/18] best practice re: early return w/ callback invocation --- lib/modules/webserver/server.js | 15 ++++++--------- lib/pipeline/pipeline.js | 8 ++++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 97e00e8d..6a0d54fe 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -32,11 +32,11 @@ class Server { async.waterfall([ function createPlaceholderPage(next) { - if (self.isFirstStart) { - self.isFirstStart = false; - return self.events.request('build-placeholder', next); + if (!self.isFirstStart) { + return next(); } - next(); + self.isFirstStart = false; + self.events.request('build-placeholder', next); }, function listen(next) { self.server.listen(self.port, self.hostname, () => { @@ -45,12 +45,9 @@ class Server { }); }, function openBrowser(next) { - if (!self.opened) { - self.opened = true; - return self.events.request('open-browser', next); + if (self.opened) { + return next(); } - next(); - }, function reportAvailable(next) { next(null, __("webserver available at") + " " + diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index fea9fa1b..cbb27c40 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -29,11 +29,11 @@ class Pipeline { async.waterfall([ function createPlaceholderPage(next){ - if (!self.isFirstBuild) { - return self.events.request('build-placeholder', next); + if (self.isFirstBuild) { + self.isFirstBuild = false; + return next(); } - self.isFirstBuild = false; - next(); + self.events.request('build-placeholder', next); }, function buildTheContracts(next) { self.buildContracts(next); From d4be16ba3cdfef11a2d08cd08a67a9acb53309c1 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 4 Sep 2018 08:20:58 -0500 Subject: [PATCH 18/18] move reporting to final callback --- lib/modules/webserver/server.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 6a0d54fe..4f631843 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -48,13 +48,19 @@ class Server { if (self.opened) { return next(); } - function reportAvailable(next) { - next(null, __("webserver available at") + - " " + - ("http://" + canonicalHost(self.hostname) + - ":" + self.port).bold.underline.green, self.port); + self.opened = true; + self.events.request('open-browser', next); } - ], callback); + ], function (err) { + if (err) { + return callback(err); + } + const msg = ( + __('webserver available at') + ' ' + + ('http://' + canonicalHost(self.hostname) + ':' + self.port).bold.underline.green + ); + callback(null, msg, self.port); + }); } stop(callback) {