Skip to content

Commit e033370

Browse files
committed
install plugin postinstall setting config flag as part of 432
1 parent 9529336 commit e033370

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

core/lib/plugin_manager.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"use strict";
2+
3+
var plugin_manager = function (config, configPath) {
4+
var path = require('path'),
5+
fs = require('fs-extra'),
6+
util = require('./utilities'),
7+
paths = config.paths;
8+
9+
function loadPlugin(pluginName) {
10+
var pluginInstance = require(path.join(process.cwd(), 'node_modules', pluginName));
11+
return pluginInstance;
12+
}
13+
14+
function installPlugin(pluginName) {
15+
try {
16+
var pluginPath = path.resolve(
17+
path.join(process.cwd(), 'node_modules', pluginName)
18+
);
19+
console.log('Attempting to load plugin from', pluginPath);
20+
try {
21+
var pluginDirStats = fs.statSync(pluginPath);
22+
} catch (ex) {
23+
util.logRed(pluginName + ' not found, please use npm to install it first.');
24+
util.logRed(pluginName + ' not loaded.');
25+
return;
26+
}
27+
var pluginPathDirExists = pluginDirStats.isDirectory();
28+
if (pluginPathDirExists) {
29+
30+
//write config entry back
31+
var diskConfig = fs.readJSONSync(path.resolve(configPath), 'utf8');
32+
diskConfig[pluginName] = true;
33+
fs.outputFileSync(path.resolve(configPath), JSON.stringify(diskConfig, null, 2));
34+
}
35+
} catch (ex) {
36+
console.log(ex);
37+
}
38+
}
39+
40+
function detectPlugins() {
41+
var node_modules_path = path.join(process.cwd(), 'node_modules');
42+
var npm_modules = fs.readdirSync(node_modules_path).filter(function (dir) {
43+
var module_path = path.join(process.cwd(), 'node_modules', dir);
44+
return fs.statSync(module_path).isDirectory() && dir.indexOf('plugin-node-') === 0;
45+
});
46+
return npm_modules;
47+
}
48+
49+
function disablePlugin(pluginName) {
50+
console.log('not implemented yet');
51+
}
52+
53+
function enablePlugin(pluginName) {
54+
console.log('not implemented yet');
55+
}
56+
57+
return {
58+
install_plugin: function(pluginName) {
59+
installPlugin(pluginName);
60+
},
61+
load_plugin: function (pluginName) {
62+
loadPlugin(pluginName);
63+
},
64+
detect_plugins: function () {
65+
return detectPlugins();
66+
},
67+
disable_plugin: function (pluginName) {
68+
disablePlugin(pluginName);
69+
},
70+
enable_plugin: function (pluginName) {
71+
enablePlugin(pluginName);
72+
}
73+
};
74+
75+
};
76+
77+
module.exports = plugin_manager;

core/scripts/postinstall.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ try{
44
console.log('Beginning Pattern Lab postinstall...');
55

66
var sm = require('../lib/starterkit_manager.js');
7+
var pm = require('../lib/plugin_manager.js');
78
var u = require('../lib/utilities.js');
89
var path = require('path');
910
var fs = require('fs-extra');
@@ -17,11 +18,28 @@ try{
1718
var foundStarterkits = starterkit_manager.detect_starterkits();
1819

1920
//todo - enhance to support multiple kits with prompt for each or all
20-
if(foundStarterkits && foundStarterkits.length > 0) {
21+
if (foundStarterkits && foundStarterkits.length > 0) {
2122
starterkit_manager.load_starterkit(foundStarterkits[0], true);
2223
} else {
2324
console.log('No starterkits found to automatically load.')
2425
}
26+
27+
//determine if any plugins are already installed
28+
var plugin_manager = new pm(config, configPath);
29+
var foundPlugins = plugin_manager.detect_plugins();
30+
31+
if (foundPlugins && foundPlugins.length > 0) {
32+
33+
for (var i = 0; i < foundPlugins.length; i++) {
34+
console.log('Found plugin', foundPlugins[i]);
35+
plugin_manager.install_plugin(foundPlugins[i]);
36+
}
37+
}
38+
39+
40+
41+
42+
2543
u.logGreen('Pattern Lab postinstall complete.');
2644

2745
} catch (ex) {

0 commit comments

Comments
 (0)