make isFirstStart, opened props of Server instance

This commit is contained in:
Michael Bradley, Jr 2018-08-31 11:46:41 -05:00
parent 4f8107bf17
commit 1d9237820a
1 changed files with 6 additions and 7 deletions

View File

@ -10,14 +10,13 @@ require('http-shutdown').extend();
require('ejs'); require('ejs');
const embark_building_placeholder = require('../code_generator/code_templates/embark-building-placeholder.html.ejs'); const embark_building_placeholder = require('../code_generator/code_templates/embark-building-placeholder.html.ejs');
let isFirstStart = true;
let opened = false;
class Server { class Server {
constructor(options) { constructor(options) {
this.dist = options.dist || 'dist/'; this.dist = options.dist || 'dist/';
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) {
@ -33,11 +32,11 @@ class Server {
serve(req, res, finalhandler(req, res)); serve(req, res, finalhandler(req, res));
}).withShutdown(); }).withShutdown();
if (isFirstStart) { if (this.isFirstStart) {
const html = embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')}); const html = embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')});
fs.mkdirpSync(this.dist); // create dist/ folder if not already exists fs.mkdirpSync(this.dist); // create dist/ folder if not already exists
fs.writeFileSync(utils.joinPath(this.dist, 'index.html'), html); fs.writeFileSync(utils.joinPath(this.dist, 'index.html'), html);
isFirstStart = false; this.isFirstStart = false;
} }
this.server.listen(this.port, this.hostname, () => { this.server.listen(this.port, this.hostname, () => {
@ -46,12 +45,12 @@ class Server {
" " + " " +
("http://" + canonicalHost(this.hostname) + ("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green, this.port); ":" + this.port).bold.underline.green, this.port);
if (!opened) { if (!this.opened) {
// fail silently, e.g. in a docker container, by cacthing promise // fail silently, e.g. in a docker container, by cacthing promise
// rejection w/ a noop // rejection w/ a noop
opn(`http://${canonicalHost(this.hostname)}:${this.port}`) opn(`http://${canonicalHost(this.hostname)}:${this.port}`)
.catch(function () {}); .catch(function () {});
opened = true; this.opened = true;
} }
}); });
} }