'open-browser' event

This commit is contained in:
Michael Bradley, Jr 2018-09-04 07:34:37 -05:00
parent 103bb43a05
commit 68c6d1549a
2 changed files with 11 additions and 7 deletions

View File

@ -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;

View File

@ -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();
},