Skip to content
This repository was archived by the owner on Oct 1, 2018. It is now read-only.

Commit 4b0c0cf

Browse files
committed
Updated afterPluginAddHook.js to discover and install modules from dependencies listed in package.json
1 parent 37a23f6 commit 4b0c0cf

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

hooks/afterPluginAddHook.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,34 @@ It will check all necessary module dependencies and install the missing ones loc
44
*/
55

66
var exec = require('child_process').exec,
7-
modules = ['xml2js', 'mkpath', 'rimraf', 'xcode'];
7+
path = require('path'),
8+
cwd = path.resolve(),
9+
modules = ['read-package-json'];
810

911
// region NPM specific
1012

13+
/**
14+
* Discovers module dependencies in plugin's package.json and installs those modules.
15+
* @param {String} pluginId - ID of the plugin calling this hook
16+
*/
17+
function getPackagesFromJson(pluginId){
18+
var readJson = require('read-package-json');
19+
readJson(path.join(cwd, 'plugins', pluginId, 'package.json'), console.error, false, function (er, data) {
20+
if (er) {
21+
console.error("There was an error reading the file: "+er);
22+
return;
23+
}
24+
if(data['dependencies']){
25+
for(var k in data['dependencies']){
26+
modules.push(k);
27+
}
28+
installRequiredNodeModules(function(){
29+
console.log('All dependency modules are installed.');
30+
});
31+
}
32+
});
33+
}
34+
1135
/**
1236
* Check if node package is installed.
1337
*
@@ -49,9 +73,9 @@ function installNodeModule(moduleName, callback) {
4973
/**
5074
* Install all required node packages.
5175
*/
52-
function installRequiredNodeModules() {
76+
function installRequiredNodeModules(callback) {
5377
if (modules.length == 0) {
54-
console.log('All dependency modules are installed.');
78+
callback();
5579
return;
5680
}
5781

@@ -63,13 +87,13 @@ function installRequiredNodeModules() {
6387
return;
6488
} else {
6589
console.log('Module ' + moduleName + ' is installed');
66-
installRequiredNodeModules();
90+
installRequiredNodeModules(callback);
6791
}
6892
});
6993
}
7094

7195
// endregion
7296

7397
module.exports = function(ctx) {
74-
installRequiredNodeModules();
98+
installRequiredNodeModules(getPackagesFromJson.bind(this, ctx.opts.plugin.id));
7599
};

0 commit comments

Comments
 (0)