install npm package using shell.js

This commit is contained in:
Subramanian Venkatesan 2018-09-10 14:01:36 +05:30
parent c8e812e638
commit cb751b5213
3 changed files with 57 additions and 10 deletions

View File

@ -132,7 +132,7 @@ class Engine {
}
pluginCommandService() {
this.registerModule('plugin_cmd');
this.registerModule('plugin_cmd', {embarkConfig: this.embarkConfig});
}
namingSystem(_options) {

View File

@ -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');
}
});
}
};
});

View File

@ -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) {