mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-01-11 14:34:40 +00:00
Merge pull request #770 from embark-framework/features/open-browser
open browser when Embark's webserver starts
This commit is contained in:
commit
7ee02525a7
@ -228,6 +228,7 @@ class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
webServerService(_options) {
|
webServerService(_options) {
|
||||||
|
_options.buildDir = this.config.buildDir;
|
||||||
this.registerModule('webserver', _options);
|
this.registerModule('webserver', _options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,8 +13,7 @@ const Templates = {
|
|||||||
define_web3_simple: require('./code_templates/define-web3-simple.js.ejs'),
|
define_web3_simple: require('./code_templates/define-web3-simple.js.ejs'),
|
||||||
web3_connector: require('./code_templates/web3-connector.js.ejs'),
|
web3_connector: require('./code_templates/web3-connector.js.ejs'),
|
||||||
do_when_loaded: require('./code_templates/do-when-loaded.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'),
|
exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.ejs')
|
||||||
embark_building_placeholder: require('./code_templates/embark-building-placeholder.html.ejs')
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class CodeGenerator {
|
class CodeGenerator {
|
||||||
@ -94,10 +93,6 @@ class CodeGenerator {
|
|||||||
cb(self.generateContractCode(contract, gasLimit));
|
cb(self.generateContractCode(contract, gasLimit));
|
||||||
});
|
});
|
||||||
|
|
||||||
this.events.setCommandHandler('embark-building-placeholder', (cb) => {
|
|
||||||
self.buildPlaceholderPage(cb);
|
|
||||||
});
|
|
||||||
|
|
||||||
self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => {
|
self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => {
|
||||||
cb(self.getEmbarkJsProviderCode());
|
cb(self.getEmbarkJsProviderCode());
|
||||||
});
|
});
|
||||||
@ -379,12 +374,6 @@ class CodeGenerator {
|
|||||||
}
|
}
|
||||||
], cb);
|
], cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
buildPlaceholderPage(cb) {
|
|
||||||
let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')});
|
|
||||||
cb(html);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = CodeGenerator;
|
module.exports = CodeGenerator;
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
|
const fs = require('../../core/fs.js');
|
||||||
var {canonicalHost} = require('../../utils/host.js');
|
var {canonicalHost} = require('../../utils/host.js');
|
||||||
var utils = require('../../utils/utils.js');
|
var utils = require('../../utils/utils.js');
|
||||||
var Server = require('./server.js');
|
var Server = require('./server.js');
|
||||||
|
const opn = require('opn');
|
||||||
|
|
||||||
|
require('ejs');
|
||||||
|
const Templates = {
|
||||||
|
embark_building_placeholder: require('./templates/embark-building-placeholder.html.ejs')
|
||||||
|
};
|
||||||
|
|
||||||
class WebServer {
|
class WebServer {
|
||||||
|
|
||||||
constructor(embark, options) {
|
constructor(embark, options) {
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
this.logger = embark.logger;
|
this.logger = embark.logger;
|
||||||
this.events = embark.events;
|
this.events = embark.events;
|
||||||
|
this.buildDir = options.buildDir;
|
||||||
this.webServerConfig = embark.config.webServerConfig;
|
this.webServerConfig = embark.config.webServerConfig;
|
||||||
if (!this.webServerConfig.enabled) {
|
if (!this.webServerConfig.enabled) {
|
||||||
return;
|
return;
|
||||||
@ -18,7 +25,13 @@ class WebServer {
|
|||||||
|
|
||||||
this.events.emit("status", __("Starting Server"));
|
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.testPort(() => {
|
||||||
this.listenToCommands();
|
this.listenToCommands();
|
||||||
this.registerConsoleCommands();
|
this.registerConsoleCommands();
|
||||||
@ -60,25 +73,48 @@ class WebServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
listenToCommands() {
|
listenToCommands() {
|
||||||
this.events.setCommandHandler('start-webserver', (callback) => this.server.start(callback));
|
this.events.setCommandHandler('build-placeholder', (cb) => this.buildPlaceholderPage(cb));
|
||||||
this.events.setCommandHandler('stop-webserver', (callback) => this.server.stop(callback));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
registerConsoleCommands() {
|
registerConsoleCommands() {
|
||||||
const self = this;
|
const self = this;
|
||||||
self.embark.registerConsoleCommand((cmd, _options) => {
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
||||||
return {
|
return {
|
||||||
match: () => cmd === "webserver start",
|
match: () => cmd === 'webserver start',
|
||||||
process: (callback) => self.events.request("start-webserver", callback)
|
process: (cb) => self.events.request('start-webserver', cb)
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
self.embark.registerConsoleCommand((cmd, _options) => {
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
||||||
return {
|
return {
|
||||||
match: () => cmd === "webserver stop",
|
match: () => cmd === 'webserver stop',
|
||||||
process: (callback) => self.events.request("stop-webserver", callback)
|
process: (cb) => self.events.request('stop-webserver', cb)
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
||||||
|
return {
|
||||||
|
match: () => cmd === 'browser open',
|
||||||
|
process: (cb) => self.events.request('open-browser', cb)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
buildPlaceholderPage(cb) {
|
||||||
|
let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')});
|
||||||
|
fs.mkdirpSync(this.buildDir); // create buildDir if it does not exist
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
let finalhandler = require('finalhandler');
|
let finalhandler = require('finalhandler');
|
||||||
|
const async = require('async');
|
||||||
let http = require('http');
|
let http = require('http');
|
||||||
let serveStatic = require('serve-static');
|
let serveStatic = require('serve-static');
|
||||||
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
|
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
|
||||||
@ -6,31 +7,59 @@ require('http-shutdown').extend();
|
|||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.dist = options.dist || 'dist/';
|
this.buildDir = options.buildDir;
|
||||||
|
this.events = options.events;
|
||||||
this.port = options.port || 8000;
|
this.port = options.port || 8000;
|
||||||
this.hostname = dockerHostSwap(options.host) || defaultHost;
|
this.hostname = dockerHostSwap(options.host) || defaultHost;
|
||||||
|
this.isFirstStart = true;
|
||||||
|
this.opened = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
start(callback) {
|
start(callback) {
|
||||||
if (this.server && this.server.listening) {
|
if (this.server && this.server.listening) {
|
||||||
let message = __("a webserver is already running at") + " " +
|
let message = __("a webserver is already running at") + " " +
|
||||||
("http://" + canonicalHost(this.hostname) +
|
("http://" + canonicalHost(this.hostname) +
|
||||||
":" + this.port).bold.underline.green;
|
":" + this.port).bold.underline.green;
|
||||||
return callback(null, message);
|
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) {
|
this.server = http.createServer(function onRequest(req, res) {
|
||||||
serve(req, res, finalhandler(req, res));
|
serve(req, res, finalhandler(req, res));
|
||||||
}).withShutdown();
|
}).withShutdown();
|
||||||
|
|
||||||
|
const self = this;
|
||||||
|
|
||||||
this.server.listen(this.port, this.hostname, () => {
|
async.waterfall([
|
||||||
this.port = this.server.address().port;
|
function createPlaceholderPage(next) {
|
||||||
callback(null, __("webserver available at") +
|
if (!self.isFirstStart) {
|
||||||
" " +
|
return next();
|
||||||
("http://" + canonicalHost(this.hostname) +
|
}
|
||||||
":" + this.port).bold.underline.green, this.port);
|
self.isFirstStart = false;
|
||||||
|
self.events.request('build-placeholder', next);
|
||||||
|
},
|
||||||
|
function listen(next) {
|
||||||
|
self.server.listen(self.port, self.hostname, () => {
|
||||||
|
self.port = self.server.address().port;
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function openBrowser(next) {
|
||||||
|
if (self.opened) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
self.opened = true;
|
||||||
|
self.events.request('open-browser', next);
|
||||||
|
}
|
||||||
|
], 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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +71,6 @@ class Server {
|
|||||||
callback(null, __("Webserver stopped"));
|
callback(null, __("Webserver stopped"));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Server;
|
module.exports = Server;
|
||||||
|
@ -5,7 +5,6 @@ const utils = require('../utils/utils.js');
|
|||||||
const constants = require('../constants');
|
const constants = require('../constants');
|
||||||
|
|
||||||
class Pipeline {
|
class Pipeline {
|
||||||
|
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.env = options.env;
|
this.env = options.env;
|
||||||
this.buildDir = options.buildDir;
|
this.buildDir = options.buildDir;
|
||||||
@ -16,6 +15,7 @@ class Pipeline {
|
|||||||
this.plugins = options.plugins;
|
this.plugins = options.plugins;
|
||||||
this.webpackConfigName = options.webpackConfigName;
|
this.webpackConfigName = options.webpackConfigName;
|
||||||
this.pipelinePlugins = this.plugins.getPluginsFor('pipeline');
|
this.pipelinePlugins = this.plugins.getPluginsFor('pipeline');
|
||||||
|
this.isFirstBuild = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
build(abi, contractsJSON, path, callback) {
|
build(abi, contractsJSON, path, callback) {
|
||||||
@ -29,10 +29,11 @@ class Pipeline {
|
|||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function createPlaceholderPage(next){
|
function createPlaceholderPage(next){
|
||||||
self.events.request('embark-building-placeholder', (html) => {
|
if (self.isFirstBuild) {
|
||||||
fs.mkdirpSync(self.buildDir); // create dist/ folder if not already exists
|
self.isFirstBuild = false;
|
||||||
fs.writeFile(utils.joinPath(self.buildDir, 'index.html'), html, next);
|
return next();
|
||||||
});
|
}
|
||||||
|
self.events.request('build-placeholder', next);
|
||||||
},
|
},
|
||||||
function buildTheContracts(next) {
|
function buildTheContracts(next) {
|
||||||
self.buildContracts(next);
|
self.buildContracts(next);
|
||||||
|
13
package-lock.json
generated
13
package-lock.json
generated
@ -5272,6 +5272,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz",
|
||||||
"integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA=="
|
"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": {
|
"isarray": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
||||||
@ -6698,6 +6703,14 @@
|
|||||||
"mimic-fn": "^1.0.0"
|
"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": {
|
"optimist": {
|
||||||
"version": "0.3.7",
|
"version": "0.3.7",
|
||||||
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz",
|
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz",
|
||||||
|
@ -66,6 +66,7 @@
|
|||||||
"netcat": "^1.3.5",
|
"netcat": "^1.3.5",
|
||||||
"node-ipc": "^9.1.1",
|
"node-ipc": "^9.1.1",
|
||||||
"node-netcat": "^1.4.8",
|
"node-netcat": "^1.4.8",
|
||||||
|
"opn": "^5.3.0",
|
||||||
"ora": "^2.1.0",
|
"ora": "^2.1.0",
|
||||||
"os-locale": "^2.1.0",
|
"os-locale": "^2.1.0",
|
||||||
"parse-json": "^4.0.0",
|
"parse-json": "^4.0.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user