restructure plugin_cmd

This commit is contained in:
Jonathan Rainville 2018-09-13 10:08:15 -04:00
parent 323ec0d529
commit 70763fc717

View File

@ -1,6 +1,7 @@
let fs = require('./../../core/fs.js'); const fs = require('./../../core/fs.js');
let utils = require('./../../utils/utils.js'); const utils = require('./../../utils/utils.js');
let async = require('async'); const async = require('async');
class PluginCommand { class PluginCommand {
constructor(embark) { constructor(embark) {
this.embark = embark; this.embark = embark;
@ -8,22 +9,29 @@ class PluginCommand {
this.embarkConfig = this.config.embarkConfig; this.embarkConfig = this.config.embarkConfig;
this.registerCommand(); this.registerCommand();
} }
registerCommand() { registerCommand() {
const self = this; this.embark.registerConsoleCommand((cmd, _options) => {
self.embark.registerConsoleCommand((cmd, _options) => {
let cmdArray = cmd.split(' '); let cmdArray = cmd.split(' ');
cmdArray = cmdArray.filter(cmd => cmd.trim().length > 0); cmdArray = cmdArray.filter(cmd => cmd.trim().length > 0);
let cmdName = cmdArray[0]; let cmdName = cmdArray[0];
return { return {
match: () => cmdName === 'plugin', match: () => cmdName === 'plugin',
process: (callback) => { process: this.installPlugin.bind(this, cmdArray)
};
});
}
installPlugin(cmdArray, callback) {
const self = this;
if (cmdArray.length < 3 || cmdArray[1] !== 'install' || typeof cmdArray[2] === 'undefined') { if (cmdArray.length < 3 || cmdArray[1] !== 'install' || typeof cmdArray[2] === 'undefined') {
return callback('invalid use of plugin command. Please use plugin install <package>'); return callback(__('Invalid use of plugin command. Please use `plugin install <package>`'));
} }
let npmInstall = ['npm', 'install', '--save']; let npmInstall = ['npm', 'install', '--save'];
npmInstall = npmInstall.concat(cmdArray.slice(2)); npmInstall = npmInstall.concat(cmdArray.slice(2));
let npmPackage = npmInstall[3]; let npmPackage = npmInstall[3];
self.embark.logger.info(`Installing npm package ${npmPackage} ...`); self.embark.logger.info(__('Installing npm package %s...', npmPackage));
async.waterfall([ async.waterfall([
function npmInstallAsync(cb) { function npmInstallAsync(cb) {
utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => { utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => {
@ -43,14 +51,11 @@ class PluginCommand {
} }
], (err) => { ], (err) => {
if (err) { if (err) {
let errorMessage = `Error installing npm package ${npmPackage}. Please visit https://embark.status.im/plugins/ for all supported plugins`; self.embark.logger.error(__('Error installing npm package %s. Please visit ' +
self.embark.logger.error(errorMessage); 'https://embark.status.im/plugins/ for all supported plugins', npmPackage));
return callback('Error occurred'); return callback(__('Error occurred'));
} }
callback(null, `npm package ${npmPackage} successfully installed as a plugin`); callback(null, __('NPM package %s successfully installed as a plugin', npmPackage));
});
}
};
}); });
} }
} }