2018-09-10 08:31:36 +00:00
|
|
|
let fs = require('./../../core/fs.js');
|
|
|
|
let utils = require('./../../utils/utils.js');
|
|
|
|
let async = require('async');
|
2018-09-09 17:37:55 +00:00
|
|
|
class PluginCommand {
|
2018-09-10 17:42:04 +00:00
|
|
|
constructor(embark) {
|
2018-09-09 17:52:57 +00:00
|
|
|
this.embark = embark;
|
2018-09-10 17:42:04 +00:00
|
|
|
this.config = this.embark.pluginConfig;
|
2018-09-10 17:08:17 +00:00
|
|
|
this.embarkConfig = this.config.embarkConfig;
|
2018-09-10 12:55:17 +00:00
|
|
|
this.registerCommand();
|
2018-09-09 17:37:55 +00:00
|
|
|
}
|
2018-09-10 12:55:17 +00:00
|
|
|
registerCommand() {
|
2018-09-10 10:49:37 +00:00
|
|
|
const self = this;
|
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
2018-09-10 08:31:36 +00:00
|
|
|
let cmdArray = cmd.split(' ');
|
2018-09-10 10:49:37 +00:00
|
|
|
cmdArray = cmdArray.filter(cmd => cmd.trim().length > 0);
|
2018-09-10 08:31:36 +00:00
|
|
|
let cmdName = cmdArray[0];
|
2018-09-09 17:37:55 +00:00
|
|
|
return {
|
2018-09-10 08:31:36 +00:00
|
|
|
match: () => cmdName === 'plugin',
|
|
|
|
process: (callback) => {
|
|
|
|
if(cmdArray.length < 3 || cmdArray[1] !== 'install' || typeof cmdArray[2] === 'undefined') {
|
2018-09-10 12:55:17 +00:00
|
|
|
return callback('invalid use of plugin command. Please use plugin install <package>');
|
2018-09-10 08:31:36 +00:00
|
|
|
}
|
2018-09-10 17:08:17 +00:00
|
|
|
let npmInstall = ['npm', 'install', '--save'];
|
2018-09-10 10:49:37 +00:00
|
|
|
npmInstall = npmInstall.concat(cmdArray.slice(2));
|
2018-09-12 05:57:28 +00:00
|
|
|
let npmPackage = npmInstall[3];
|
|
|
|
self.embark.logger.info(`Installing npm package ${npmPackage} ...`);
|
2018-09-10 08:31:36 +00:00
|
|
|
async.waterfall([
|
|
|
|
function npmInstallAsync(cb) {
|
|
|
|
utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function addToEmbarkConfig(cb) {
|
2018-09-10 10:49:37 +00:00
|
|
|
// get the installed package from package.json
|
|
|
|
let packageFile = fs.readJSONSync(self.config.packageFile);
|
|
|
|
let dependencies = Object.keys(packageFile.dependencies);
|
|
|
|
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);
|
2018-09-10 08:31:36 +00:00
|
|
|
}
|
|
|
|
], (err) => {
|
|
|
|
if(err) {
|
2018-09-12 13:57:19 +00:00
|
|
|
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');
|
2018-09-10 08:31:36 +00:00
|
|
|
}
|
2018-09-12 05:57:28 +00:00
|
|
|
callback(null, `npm package ${npmPackage} successfully installed as a plugin`);
|
2018-09-10 08:31:36 +00:00
|
|
|
});
|
2018-09-09 17:52:57 +00:00
|
|
|
}
|
2018-09-09 17:37:55 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PluginCommand;
|