From 74f0d05ca03956168b12a39ffb8a90447715f70d Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Sun, 9 Sep 2018 23:07:55 +0530 Subject: [PATCH 01/13] Plugin Command --- cmd/cmd_controller.js | 2 +- lib/core/engine.js | 7 ++++++- lib/modules/plugin_cmd/index.js | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/modules/plugin_cmd/index.js diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 6e35d3b2..cdb9bfaa 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -280,7 +280,7 @@ class EmbarkController { engine.startService("webServer"); engine.startService("namingSystem"); engine.startService("console"); - + engine.startService("pluginCommand"); return callback(); } diff --git a/lib/core/engine.js b/lib/core/engine.js index 7554d43f..013df106 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -70,7 +70,8 @@ class Engine { "processManager": this.processManagerService, "storage": this.storageService, "graph": this.graphService, - "codeCoverage": this.codeCoverageService + "codeCoverage": this.codeCoverageService, + "pluginCommand": this.pluginCommandService }; let service = services[serviceName]; @@ -130,6 +131,10 @@ class Engine { this.servicesMonitor.startMonitor(); } + pluginCommandService() { + this.registerModule('plugin_cmd'); + } + namingSystem(_options) { this.registerModule('ens'); } diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js new file mode 100644 index 00000000..111f5d75 --- /dev/null +++ b/lib/modules/plugin_cmd/index.js @@ -0,0 +1,18 @@ +class PluginCommand { + constructor(embark) { + console.log(embark); + //this.embark = embark; + //this.registerCommands(); + } + registerCommands() { + const self = this; + self.embark.registerConsoleCommand((cmd, _options) => { + return { + match: () => cmd === 'webserver start', + process: (cb) => self.events.request('start-webserver', cb) + }; + }); + } +} + +module.exports = PluginCommand; From c8e812e6389123aeb5855a826312b4b1360cd924 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Sun, 9 Sep 2018 23:22:57 +0530 Subject: [PATCH 02/13] Plugin Command Module --- lib/modules/plugin_cmd/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index 111f5d75..3514e212 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -1,15 +1,16 @@ class PluginCommand { constructor(embark) { - console.log(embark); - //this.embark = embark; - //this.registerCommands(); + this.embark = embark; + this.registerCommands(); } registerCommands() { const self = this; self.embark.registerConsoleCommand((cmd, _options) => { return { - match: () => cmd === 'webserver start', - process: (cb) => self.events.request('start-webserver', cb) + match: () => cmd === 'plugin', + process: (cb) => { + //self.events.request('start-webserver', cb) + } }; }); } From cb751b521381fcd4467cc92e7bfc68251c07defc Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 14:01:36 +0530 Subject: [PATCH 03/13] install npm package using shell.js --- lib/core/engine.js | 2 +- lib/modules/plugin_cmd/index.js | 51 +++++++++++++++++++++++++++++---- lib/utils/utils.js | 14 +++++++-- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 013df106..339692df 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -132,7 +132,7 @@ class Engine { } pluginCommandService() { - this.registerModule('plugin_cmd'); + this.registerModule('plugin_cmd', {embarkConfig: this.embarkConfig}); } namingSystem(_options) { diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index 3514e212..ce04efab 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -1,15 +1,54 @@ +let fs = require('./../../core/fs.js'); +let utils = require('./../../utils/utils.js'); +let async = require('async'); + class PluginCommand { - constructor(embark) { + constructor(embark, config) { this.embark = embark; + this.config = config; + this.embarkConfig = fs.readJSONSync(this.config.embarkConfig); this.registerCommands(); } registerCommands() { - const self = this; - self.embark.registerConsoleCommand((cmd, _options) => { + this.embark.registerConsoleCommand((cmd, _options) => { + let cmdArray = cmd.split(' '); + let cmdName = cmdArray[0]; return { - match: () => cmd === 'plugin', - process: (cb) => { - //self.events.request('start-webserver', cb) + match: () => cmdName === 'plugin', + process: (callback) => { + if(cmdArray.length < 3 || cmdArray[1] !== 'install' || typeof cmdArray[2] === 'undefined') { + return callback(__('arguments to the command are missing or invalid')); + } + let npmInstall = ['npm', 'install']; + if (cmdArray[2] === '--save') { + if(typeof cmdArray[3] === 'undefined') { + return callback(__('npm package argument missing')); + } + npmInstall = npmInstall.concat(cmdArray); + } else { + npmInstall = npmInstall.concat(['--save'].concat(cmdArray.slice(2))); + } + + async.waterfall([ + function npmInstallAsync(cb) { + utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => { + if(err) { + return cb(err); + } + cb(); + }); + }, + function addToEmbarkConfig(cb) { + //TO DO: read from package.json and update the plugins + cb(); + } + ], (err) => { + if(err) { + callback(__(err)); + } else { + callback(null, 'npm package successfully installed as a plugin'); + } + }); } }; }); diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 7ccbc620..2f462cb4 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -129,17 +129,25 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { }); } -function runCmd(cmd, options) { +function runCmd(cmd, options, callback) { const shelljs = require('shelljs'); - let result = shelljs.exec(cmd, options || {silent: true}); + options = options || {silent: true, exitOnError: true}; + console.log(cmd, options, typeof callback); + let result = shelljs.exec(cmd); if (result.code !== 0) { console.log("error doing.. " + cmd); console.log(result.output); if (result.stderr !== undefined) { console.log(result.stderr); } - exit(); + if (typeof callback === 'function') { + return callback(result, null); + } + if (options.exitOnError) { + exit(); + } } + callback(null, result); } function cd(folder) { From 4823d2312e9657d3e6ca3d6af43a71a83e507d37 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 14:07:02 +0530 Subject: [PATCH 04/13] remove console.log --- lib/utils/utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 2f462cb4..eb6c3837 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -132,7 +132,6 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { function runCmd(cmd, options, callback) { const shelljs = require('shelljs'); options = options || {silent: true, exitOnError: true}; - console.log(cmd, options, typeof callback); let result = shelljs.exec(cmd); if (result.code !== 0) { console.log("error doing.. " + cmd); From f20934ac8ce97912d19af1ccebb75bd661f20a7f Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 14:11:40 +0530 Subject: [PATCH 05/13] Use Object.assign --- lib/utils/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index eb6c3837..9a3ef31a 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -131,7 +131,7 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { function runCmd(cmd, options, callback) { const shelljs = require('shelljs'); - options = options || {silent: true, exitOnError: true}; + options = Object.assign({silent: true, exitOnError: true}, options); let result = shelljs.exec(cmd); if (result.code !== 0) { console.log("error doing.. " + cmd); From 78bc27ad95c6efa214cb3128e607e3bec381ee69 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 14:16:02 +0530 Subject: [PATCH 06/13] fix runCmd --- lib/utils/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 9a3ef31a..db8922de 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -131,7 +131,7 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { function runCmd(cmd, options, callback) { const shelljs = require('shelljs'); - options = Object.assign({silent: true, exitOnError: true}, options); + options = Object.assign({silent: true, exitOnError: true}, options || {}); let result = shelljs.exec(cmd); if (result.code !== 0) { console.log("error doing.. " + cmd); From 53de6dcb193b38a71416cdf09b77e3641ecb5675 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 16:19:37 +0530 Subject: [PATCH 07/13] Fixes to npm command parsing --- lib/core/engine.js | 2 +- lib/modules/plugin_cmd/index.js | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 339692df..9f893e4d 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -132,7 +132,7 @@ class Engine { } pluginCommandService() { - this.registerModule('plugin_cmd', {embarkConfig: this.embarkConfig}); + this.registerModule('plugin_cmd', {embarkConfigFile: this.embarkConfig, packageFile: 'package.json'}); } namingSystem(_options) { diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index ce04efab..6487dda1 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -1,17 +1,18 @@ let fs = require('./../../core/fs.js'); let utils = require('./../../utils/utils.js'); let async = require('async'); - class PluginCommand { constructor(embark, config) { this.embark = embark; this.config = config; - this.embarkConfig = fs.readJSONSync(this.config.embarkConfig); + this.embarkConfig = fs.readJSONSync(this.config.embarkConfigFile); this.registerCommands(); } registerCommands() { - this.embark.registerConsoleCommand((cmd, _options) => { + const self = this; + self.embark.registerConsoleCommand((cmd, _options) => { let cmdArray = cmd.split(' '); + cmdArray = cmdArray.filter(cmd => cmd.trim().length > 0); let cmdName = cmdArray[0]; return { match: () => cmdName === 'plugin', @@ -24,10 +25,10 @@ class PluginCommand { if(typeof cmdArray[3] === 'undefined') { return callback(__('npm package argument missing')); } - npmInstall = npmInstall.concat(cmdArray); } else { - npmInstall = npmInstall.concat(['--save'].concat(cmdArray.slice(2))); + npmInstall.push('--save'); } + npmInstall = npmInstall.concat(cmdArray.slice(2)); async.waterfall([ function npmInstallAsync(cb) { @@ -39,8 +40,13 @@ class PluginCommand { }); }, function addToEmbarkConfig(cb) { - //TO DO: read from package.json and update the plugins - cb(); + // get the installed package from package.json + let packageFile = fs.readJSONSync(self.config.packageFile); + let dependencies = Object.keys(packageFile.dependencies); + let npmPackage = npmInstall[3]; + let installedPackage = dependencies.filter((dep) => npmPackage.indexOf(dep) >=0); + self.embarkConfig.plugins[installedPackage[0]] = {}; + fs.writeFile(self.config.embarkConfigFile, JSON.stringify(self.embarkConfig, null, 2), cb); } ], (err) => { if(err) { From 2782acf4383e350bccd5695a4b75aca4f6c04d4c Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 18:25:17 +0530 Subject: [PATCH 08/13] fix plugin command --- lib/modules/plugin_cmd/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index 6487dda1..92808df6 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -6,9 +6,9 @@ class PluginCommand { this.embark = embark; this.config = config; this.embarkConfig = fs.readJSONSync(this.config.embarkConfigFile); - this.registerCommands(); + this.registerCommand(); } - registerCommands() { + registerCommand() { const self = this; self.embark.registerConsoleCommand((cmd, _options) => { let cmdArray = cmd.split(' '); @@ -18,12 +18,12 @@ class PluginCommand { match: () => cmdName === 'plugin', process: (callback) => { if(cmdArray.length < 3 || cmdArray[1] !== 'install' || typeof cmdArray[2] === 'undefined') { - return callback(__('arguments to the command are missing or invalid')); + return callback('invalid use of plugin command. Please use plugin install '); } let npmInstall = ['npm', 'install']; if (cmdArray[2] === '--save') { if(typeof cmdArray[3] === 'undefined') { - return callback(__('npm package argument missing')); + return callback('package argument missing'); } } else { npmInstall.push('--save'); @@ -50,7 +50,7 @@ class PluginCommand { } ], (err) => { if(err) { - callback(__(err)); + callback(err); } else { callback(null, 'npm package successfully installed as a plugin'); } From fe0bbc3559c608d26a72fba81cb4b9358c9d8509 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 22:38:17 +0530 Subject: [PATCH 09/13] changes from the code review --- cmd/cmd_controller.js | 1 + lib/core/engine.js | 2 +- lib/modules/plugin_cmd/index.js | 17 ++++------------- lib/utils/utils.js | 6 ++++-- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index cdb9bfaa..d965cfe1 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -127,6 +127,7 @@ class EmbarkController { engine.startService("codeGenerator"); engine.startService("namingSystem"); engine.startService("console"); + engine.startService("pluginCommand"); engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); diff --git a/lib/core/engine.js b/lib/core/engine.js index 9f893e4d..23af30e4 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -132,7 +132,7 @@ class Engine { } pluginCommandService() { - this.registerModule('plugin_cmd', {embarkConfigFile: this.embarkConfig, packageFile: 'package.json'}); + this.registerModule('plugin_cmd', {embarkConfigFile: this.embarkConfig, embarkConfig: this.config.embarkConfig, packageFile: 'package.json'}); } namingSystem(_options) { diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index 92808df6..c38d551e 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -5,7 +5,7 @@ class PluginCommand { constructor(embark, config) { this.embark = embark; this.config = config; - this.embarkConfig = fs.readJSONSync(this.config.embarkConfigFile); + this.embarkConfig = this.config.embarkConfig; this.registerCommand(); } registerCommand() { @@ -20,16 +20,8 @@ class PluginCommand { if(cmdArray.length < 3 || cmdArray[1] !== 'install' || typeof cmdArray[2] === 'undefined') { return callback('invalid use of plugin command. Please use plugin install '); } - let npmInstall = ['npm', 'install']; - if (cmdArray[2] === '--save') { - if(typeof cmdArray[3] === 'undefined') { - return callback('package argument missing'); - } - } else { - npmInstall.push('--save'); - } + let npmInstall = ['npm', 'install', '--save']; npmInstall = npmInstall.concat(cmdArray.slice(2)); - async.waterfall([ function npmInstallAsync(cb) { utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => { @@ -50,10 +42,9 @@ class PluginCommand { } ], (err) => { if(err) { - callback(err); - } else { - callback(null, 'npm package successfully installed as a plugin'); + return callback(err); } + callback(null, 'npm package successfully installed as a plugin'); }); } }; diff --git a/lib/utils/utils.js b/lib/utils/utils.js index db8922de..dc11127d 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -143,10 +143,12 @@ function runCmd(cmd, options, callback) { return callback(result, null); } if (options.exitOnError) { - exit(); + return exit(); } } - callback(null, result); + if(typeof callback === 'function') { + return callback(null, result); + } } function cd(folder) { From b3eedc4ba29e8f207128662583114a64ad886c28 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Mon, 10 Sep 2018 23:12:04 +0530 Subject: [PATCH 10/13] embark config --- lib/modules/plugin_cmd/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index c38d551e..a7352574 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -2,9 +2,9 @@ let fs = require('./../../core/fs.js'); let utils = require('./../../utils/utils.js'); let async = require('async'); class PluginCommand { - constructor(embark, config) { + constructor(embark) { this.embark = embark; - this.config = config; + this.config = this.embark.pluginConfig; this.embarkConfig = this.config.embarkConfig; this.registerCommand(); } From 1a756e8a057b4887adf7c61a447d10d21dc53666 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Wed, 12 Sep 2018 11:27:28 +0530 Subject: [PATCH 11/13] Added better error logging, dashboard doesnt break in run mode --- lib/modules/plugin_cmd/index.js | 7 +++--- lib/utils/utils.js | 41 +++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index a7352574..778144e1 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -22,6 +22,8 @@ class PluginCommand { } let npmInstall = ['npm', 'install', '--save']; npmInstall = npmInstall.concat(cmdArray.slice(2)); + let npmPackage = npmInstall[3]; + self.embark.logger.info(`Installing npm package ${npmPackage} ...`); async.waterfall([ function npmInstallAsync(cb) { utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => { @@ -35,16 +37,15 @@ class PluginCommand { // get the installed package from package.json let packageFile = fs.readJSONSync(self.config.packageFile); let dependencies = Object.keys(packageFile.dependencies); - let npmPackage = npmInstall[3]; let installedPackage = dependencies.filter((dep) => npmPackage.indexOf(dep) >=0); self.embarkConfig.plugins[installedPackage[0]] = {}; fs.writeFile(self.config.embarkConfigFile, JSON.stringify(self.embarkConfig, null, 2), cb); } ], (err) => { if(err) { - return callback(err); + return callback(`Error installing npm package ${npmPackage}. Please visit https://embark.status.im/plugins/ for more info`); } - callback(null, 'npm package successfully installed as a plugin'); + callback(null, `npm package ${npmPackage} successfully installed as a plugin`); }); } }; diff --git a/lib/utils/utils.js b/lib/utils/utils.js index dc11127d..8eaf99a4 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -131,24 +131,35 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { function runCmd(cmd, options, callback) { const shelljs = require('shelljs'); - options = Object.assign({silent: true, exitOnError: true}, options || {}); - let result = shelljs.exec(cmd); - if (result.code !== 0) { - console.log("error doing.. " + cmd); - console.log(result.output); - if (result.stderr !== undefined) { - console.log(result.stderr); + options = Object.assign({silent: true,exitOnError: true, async: true}, options || {}); + const outputToConsole = !options.silent; + options.silent = true; + let result = shelljs.exec(cmd, options, function (code, stdout) { + if(code !==0) { + if (options.exitOnError) { + return exit(); + } + if(typeof callback === 'function') { + callback(`shell returned code ${code}`); + } + } else { + if(typeof callback === 'function') { + return callback(null, stdout); + } } - if (typeof callback === 'function') { - return callback(result, null); + }); + + result.stdout.on('data', function(data) { + if(outputToConsole) { + console.log(data); } - if (options.exitOnError) { - return exit(); + }); + + result.stderr.on('data', function(data) { + if (outputToConsole) { + console.log(data); } - } - if(typeof callback === 'function') { - return callback(null, result); - } + }); } function cd(folder) { From b2d6421ea6df5d30ad155f739b0b4b85191a9acf Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Wed, 12 Sep 2018 11:50:20 +0530 Subject: [PATCH 12/13] fix code formatting --- lib/utils/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 8eaf99a4..0b4412d8 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -131,11 +131,11 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { function runCmd(cmd, options, callback) { const shelljs = require('shelljs'); - options = Object.assign({silent: true,exitOnError: true, async: true}, options || {}); + options = Object.assign({silent: true, exitOnError: true, async: true}, options || {}); const outputToConsole = !options.silent; options.silent = true; let result = shelljs.exec(cmd, options, function (code, stdout) { - if(code !==0) { + if(code !== 0) { if (options.exitOnError) { return exit(); } From c8eec064b13f42795d92be11b43d2ff582ec88a9 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Wed, 12 Sep 2018 19:27:19 +0530 Subject: [PATCH 13/13] log the error --- lib/modules/plugin_cmd/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/modules/plugin_cmd/index.js b/lib/modules/plugin_cmd/index.js index 778144e1..b840c51a 100644 --- a/lib/modules/plugin_cmd/index.js +++ b/lib/modules/plugin_cmd/index.js @@ -43,7 +43,9 @@ class PluginCommand { } ], (err) => { if(err) { - return callback(`Error installing npm package ${npmPackage}. Please visit https://embark.status.im/plugins/ for more info`); + let errorMessage = `Error installing npm package ${npmPackage}. Please visit https://embark.status.im/plugins/ for all supported plugins`; + self.embark.logger.error(errorMessage); + return callback('Error occurred'); } callback(null, `npm package ${npmPackage} successfully installed as a plugin`); });