From e0f73c7d22db7bb78b1c5d3ab596d4e4983c2705 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Fri, 31 Aug 2018 09:24:45 +0100 Subject: [PATCH] Make console a module --- cmd/cmd_controller.js | 29 ++---------- cmd/dashboard/dashboard.js | 47 +++++-------------- cmd/dashboard/monitor.js | 7 ++- cmd/dashboard/repl.js | 24 +--------- lib/core/engine.js | 12 +++++ lib/core/modules/coderunner/codeRunner.js | 1 - .../modules/console/index.js | 15 +++--- test/console.js | 6 +-- 8 files changed, 43 insertions(+), 98 deletions(-) rename cmd/dashboard/console.js => lib/modules/console/index.js (91%) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 42265c7a2..12b35c899 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -55,7 +55,6 @@ class EmbarkController { let self = this; self.context = options.context || [constants.contexts.run, constants.contexts.build]; let Dashboard = require('./dashboard/dashboard.js'); - let REPL = require('./dashboard/repl.js'); let webServerConfig = { enabled: options.runWebserver @@ -93,15 +92,6 @@ class EmbarkController { async.parallel([ function startDashboard(callback) { if (!options.useDashboard) { - new REPL({ - env: engine.env, - plugins: engine.plugins, - version: engine.version, - events: engine.events, - logger: engine.logger, - ipc: engine.ipc, - config: engine.config - }).startConsole(); return callback(); } @@ -110,9 +100,7 @@ class EmbarkController { logger: engine.logger, plugins: engine.plugins, version: self.version, - env: engine.env, - ipc: engine.ipc, - config: engine.config + env: engine.env }); dashboard.start(function () { engine.logger.info(__('dashboard start')); @@ -135,6 +123,7 @@ class EmbarkController { engine.startService("storage"); engine.startService("codeGenerator"); engine.startService("namingSystem"); + engine.startService("console"); engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); @@ -262,16 +251,6 @@ class EmbarkController { webpackConfigName: options.webpackConfigName }); engine.init(); - const repl = new REPL({ - env: engine.env, - plugins: engine.plugins, - version: engine.version, - events: engine.events, - logger: engine.logger, - ipc: engine.ipc, - config: engine.config - }); - repl.startConsole(); async.waterfall([ function startServices(callback) { let pluginList = engine.plugins.listPlugins(); @@ -292,11 +271,13 @@ class EmbarkController { engine.startService("codeGenerator"); engine.startService("webServer"); engine.startService("namingSystem"); + engine.startService("console"); return callback(); } engine.startService("codeRunner"); + engine.startService("console"); callback(); }); }, @@ -345,7 +326,7 @@ class EmbarkController { }); }, function startREPL(callback) { - repl.start(callback); + new REPL({events: engine.events, env: engine.env}).start(callback); } ], function (err, _result) { if (err) { diff --git a/cmd/dashboard/dashboard.js b/cmd/dashboard/dashboard.js index 4c707faaa..12715b789 100644 --- a/cmd/dashboard/dashboard.js +++ b/cmd/dashboard/dashboard.js @@ -2,7 +2,6 @@ let async = require('async'); let windowSize = require('window-size'); let Monitor = require('./monitor.js'); -let Console = require('./console.js'); class Dashboard { constructor(options) { @@ -11,8 +10,6 @@ class Dashboard { this.plugins = options.plugins; this.version = options.version; this.env = options.env; - this.ipc = options.ipc; - this.config = options.config; this.events.on('firstDeploymentDone', this.checkWindowSize.bind(this)); this.events.on('outputDone', this.checkWindowSize.bind(this)); @@ -26,45 +23,23 @@ class Dashboard { } start(done) { - let console, monitor; - let self = this; + let monitor; - async.waterfall([ - function startConsole(callback) { - console = new Console({ - events: self.events, - plugins: self.plugins, - version: self.version, - ipc: self.ipc, - logger: self.logger, - config: self.config - }); - callback(); - }, - function startMonitor(callback) { - monitor = new Monitor({env: self.env, console: console, events: self.events}); - self.logger.logFunction = monitor.logEntry; + monitor = new Monitor({env: this.env, events: this.events}); + this.logger.logFunction = monitor.logEntry; - self.events.on('contractsState', monitor.setContracts); - self.events.on('status', monitor.setStatus.bind(monitor)); - self.events.on('servicesState', monitor.availableServices.bind(monitor)); + this.events.on('contractsState', monitor.setContracts); + this.events.on('status', monitor.setStatus.bind(monitor)); + this.events.on('servicesState', monitor.availableServices.bind(monitor)); - self.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor)); + this.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor)); - self.logger.info('========================'.bold.green); - self.logger.info((__('Welcome to Embark') + ' ' + self.version).yellow.bold); - self.logger.info('========================'.bold.green); + this.logger.info('========================'.bold.green); + this.logger.info((__('Welcome to Embark') + ' ' + this.version).yellow.bold); + this.logger.info('========================'.bold.green); - // TODO: do this after monitor is rendered - callback(); - } - ], function () { - self.console = console; - self.monitor = monitor; - done(); - }); + done(); } - } module.exports = Dashboard; diff --git a/cmd/dashboard/monitor.js b/cmd/dashboard/monitor.js index f5ba4bbdf..eb2a256c7 100644 --- a/cmd/dashboard/monitor.js +++ b/cmd/dashboard/monitor.js @@ -366,11 +366,10 @@ class Monitor { } executeCmd(cmd, cb) { - const self = this; - self.logText.log('console> '.bold.green + cmd); - self.console.executeCmd(cmd, function (err, result) { + this.logText.log('console> '.bold.green + cmd); + this.events.request('console:executeCmd', cmd, (err, result) => { let message = err || result; - self.logText.log(message); + this.logText.log(message); if (cb) { cb(message); } diff --git a/cmd/dashboard/repl.js b/cmd/dashboard/repl.js index a99bcd5b5..cdc724a95 100644 --- a/cmd/dashboard/repl.js +++ b/cmd/dashboard/repl.js @@ -1,32 +1,13 @@ const repl = require("repl"); const util = require("util"); - -const Console = require('./console.js'); - class REPL { constructor(options) { - this.logger = options.logger; - this.env = options.env; - this.plugins = options.plugins; this.events = options.events; - this.version = options.version; - this.ipc = options.ipc; - this.config = options.config; - } - - startConsole(){ - this.console = new Console({ - events: this.events, - plugins: this.plugins, - version: this.version, - ipc: this.ipc, - logger: this.logger, - config: this.config - }); + this.env = options.env } enhancedEval(cmd, context, filename, callback) { - this.console.executeCmd(cmd.trim(), callback); + this.events.request('console:executeCmd', cmd.trim(), callback); } enhancedWriter(output) { @@ -38,7 +19,6 @@ class REPL { } start(done) { - this.startConsole(); this.replServer = repl.start({ prompt: "Embark (" + this.env + ") > ", useGlobal: true, diff --git a/lib/core/engine.js b/lib/core/engine.js index acba25ccb..67f716a66 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -60,6 +60,7 @@ class Engine { "fileWatcher": this.fileWatchService, "webServer": this.webServerService, "namingSystem": this.namingSystem, + "console": this.console, "web3": this.web3Service, "libraryManager": this.libraryManagerService, "processManager": this.processManagerService, @@ -129,6 +130,17 @@ class Engine { this.registerModule('ens'); } + console(_options) { + this.registerModule('console', { + events: this.events, + plugins: this.plugins, + version: this.version, + ipc: this.ipc, + logger: this.logger, + config: this.config + }); + } + codeRunnerService(_options) { const CodeRunner = require('./modules/coderunner/codeRunner.js'); this.codeRunner = new CodeRunner({ diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index daaf9e43d..3fccdfd29 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -13,7 +13,6 @@ class CodeRunner { this.IpcClientListen(); this.registerEvents(); this.registerCommands(); - this.events.emit('runcode:ready'); } registerIpcEvents() { diff --git a/cmd/dashboard/console.js b/lib/modules/console/index.js similarity index 91% rename from cmd/dashboard/console.js rename to lib/modules/console/index.js index 31036ed7b..97fa303ba 100644 --- a/cmd/dashboard/console.js +++ b/lib/modules/console/index.js @@ -1,9 +1,10 @@ -let utils = require('../../lib/utils/utils.js'); +let utils = require('../../utils/utils'); const EmbarkJS = require('embarkjs'); const IpfsApi = require('ipfs-api'); const Web3 = require('web3'); + class Console { - constructor(options) { + constructor(_embark, options) { this.events = options.events; this.plugins = options.plugins; this.version = options.version; @@ -14,7 +15,7 @@ class Console { if (this.ipc.isServer()) { this.ipc.on('console:executeCmd', this.executeCmd.bind(this)); } - + this.events.setCommandHandler("console:executeCmd", this.executeCmd.bind(this)); this.registerEmbarkJs(); } @@ -72,11 +73,9 @@ class Console { } registerEmbarkJs() { - this.events.on('runcode:ready', () => { - this.events.emit('runcode:register', 'IpfsApi', IpfsApi, false); - this.events.emit('runcode:register', 'Web3', Web3, false); - this.events.emit('runcode:register', 'EmbarkJS', EmbarkJS, false); - }); + this.events.emit('runcode:register', 'IpfsApi', IpfsApi, false); + this.events.emit('runcode:register', 'Web3', Web3, false); + this.events.emit('runcode:register', 'EmbarkJS', EmbarkJS, false); this.events.on('code-generator-ready', () => { if (this.ipc.connected) { diff --git a/test/console.js b/test/console.js index 68aab0ad4..dac075a7f 100644 --- a/test/console.js +++ b/test/console.js @@ -1,5 +1,5 @@ /*globals describe, it*/ -let Console = require('../cmd/dashboard/console.js'); +let Console = require('../lib/modules/console/'); let Plugins = require('../lib/core/plugins.js'); let IPC = require('../lib/core/ipc.js'); let assert = require('assert'); @@ -8,8 +8,8 @@ let version = require('../package.json').version; describe('embark.Console', function() { let ipc = new IPC({ipcRole: 'none'}); let plugins = new Plugins({plugins: {}}); - let events = {on: () => {}} - let console = new Console({plugins, version, ipc, events}); + let events = {on: () => {}, setCommandHandler: () => {}, emit: () => {}}; + let console = new Console({}, {plugins, version, ipc, events}); describe('#executeCmd', function() {