embark/cmd/dashboard/dashboard.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

let windowSize = require('window-size');
2017-03-29 17:50:05 +00:00
let Monitor = require('./monitor.js');
2017-03-30 11:12:39 +00:00
class Dashboard {
constructor(options) {
this.logger = options.logger;
2017-12-17 23:34:41 +00:00
this.events = options.events;
2017-03-30 11:12:39 +00:00
this.plugins = options.plugins;
this.version = options.version;
this.env = options.env;
this.events.on('firstDeploymentDone', this.checkWindowSize.bind(this));
this.events.on('outputDone', this.checkWindowSize.bind(this));
}
checkWindowSize() {
let size = windowSize.get();
if (size.height < 40 || size.width < 118) {
this.logger.warn(__("tip: you can resize the terminal or disable the dashboard with") + " embark run --nodashboard".bold.underline);
}
2017-03-30 11:12:39 +00:00
}
start(done) {
2018-08-31 08:24:45 +00:00
let monitor;
2017-03-30 11:12:39 +00:00
2018-08-31 08:24:45 +00:00
monitor = new Monitor({env: this.env, events: this.events});
this.logger.logFunction = monitor.logEntry;
let plugin = this.plugins.createPlugin('dashboard', {});
plugin.registerAPICall(
'ws',
2018-08-01 09:28:25 +00:00
'/embark-api/dashboard',
2018-08-02 19:17:40 +00:00
(ws, _req) => {
let dashboardState = {contractsState: [], environment: "", status: "", availableServices: []};
// TODO: doesn't feel quite right, should be refactored into a shared
// dashboard state
self.events.request('setDashboardState');
self.events.on('contractsState', (contracts) => {
dashboardState.contractsState = [];
contracts.forEach(function (row) {
dashboardState.contractsState.push({contractName: row[0], address: row[1], status: row[2]});
});
ws.send(JSON.stringify(dashboardState));
});
self.events.on('status', (status) => {
dashboardState.status = status;
ws.send(JSON.stringify(dashboardState));
});
self.events.on('servicesState', (servicesState) => {
dashboardState.availableServices = servicesState;
ws.send(JSON.stringify(dashboardState));
});
}
);
2017-03-30 11:12:39 +00:00
2018-08-31 08:24:45 +00:00
this.events.on('contractsState', monitor.setContracts);
this.events.on('status', monitor.setStatus.bind(monitor));
this.events.on('servicesState', monitor.availableServices.bind(monitor));
2018-07-23 12:30:58 +00:00
2018-08-31 08:24:45 +00:00
this.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor));
2018-02-27 22:50:43 +00:00
2018-08-31 08:24:45 +00:00
this.logger.info('========================'.bold.green);
this.logger.info((__('Welcome to Embark') + ' ' + this.version).yellow.bold);
this.logger.info('========================'.bold.green);
2017-03-30 11:12:39 +00:00
2018-08-31 08:24:45 +00:00
done();
2017-03-30 11:12:39 +00:00
}
}
module.exports = Dashboard;