Skip to content

Commit 45c8aa6

Browse files
committed
wip better plugin mgmt
1 parent 0607e06 commit 45c8aa6

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

core/lib/patternlab.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ var diveSync = require('diveSync'),
1717
cleanHtml = require('js-beautify').html,
1818
inherits = require('util').inherits,
1919
pm = require('./plugin_manager'),
20+
fs = require('fs-extra'),
2021
plutils = require('./utilities');
2122

2223
var EventEmitter = require('events').EventEmitter;
2324

24-
function buildPatternData(dataFilesPath, fs) {
25+
function buildPatternData(dataFilesPath, fsDep) {
2526
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
2627
var mergeObject = {};
2728
dataFiles.forEach(function (filePath) {
28-
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8');
29+
var jsonData = fsDep.readJSONSync(path.resolve(filePath), 'utf8');
2930
mergeObject = _.merge(mergeObject, jsonData);
3031
});
3132
return mergeObject;
@@ -83,18 +84,40 @@ function checkConfiguration(patternlab) {
8384
* @param patternlab - global data store
8485
*/
8586
function initializePlugins(patternlab) {
87+
88+
if (!patternlab.config.plugins) { return; }
89+
8690
var plugin_manager = new pm(patternlab.config, path.resolve(__dirname, '../../patternlab-config.json'));
8791
var foundPlugins = plugin_manager.detect_plugins();
8892

8993
if (foundPlugins && foundPlugins.length > 0) {
9094

9195
for (var i = 0; i < foundPlugins.length; i++) {
92-
var plugin = plugin_manager.load_plugin(foundPlugins[i]);
96+
97+
let pluginKey = foundPlugins[i];
98+
99+
console.log(pluginKey);
100+
console.log(patternlab.config.plugins[pluginKey]);
101+
102+
var plugin = plugin_manager.load_plugin(pluginKey);
93103
plugin(patternlab);
94104
}
95105
}
96106
}
97107

108+
/**
109+
* Installs a given plugin. Assumes it has already been pulled down via npm
110+
* @param pluginName - the name of the plugin
111+
*/
112+
function installPlugin(pluginName) {
113+
//get the config
114+
var configPath = path.resolve(process.cwd(), 'patternlab-config.json');
115+
var config = fs.readJSONSync(path.resolve(configPath), 'utf8');
116+
var plugin_manager = new pm(config, configPath);
117+
118+
plugin_manager.install_plugin(pluginName);
119+
}
120+
98121
function PatternLabEventEmitter() {
99122
EventEmitter.call(this);
100123
}
@@ -104,7 +127,6 @@ var patternlab_engine = function (config) {
104127
'use strict';
105128

106129
var JSON5 = require('json5'),
107-
fs = require('fs-extra'),
108130
pa = require('./pattern_assembler'),
109131
pe = require('./pattern_exporter'),
110132
lh = require('./lineage_hunter'),
@@ -123,7 +145,6 @@ var patternlab_engine = function (config) {
123145

124146
checkConfiguration(patternlab);
125147

126-
//todo: determine if this is the best place to wire up plugins
127148
initializePlugins(patternlab);
128149

129150
var paths = patternlab.config.paths;
@@ -504,6 +525,9 @@ var patternlab_engine = function (config) {
504525
},
505526
loadstarterkit: function (starterkitName, clean) {
506527
loadStarterKit(starterkitName, clean);
528+
},
529+
installplugin: function (pluginName) {
530+
installPlugin(pluginName);
507531
}
508532
};
509533
};

core/lib/plugin_manager.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ var plugin_manager = function (config, configPath) {
3636
var pluginPathDirExists = pluginDirStats.isDirectory();
3737
if (pluginPathDirExists) {
3838

39-
//write config entry back
4039
var diskConfig = fs.readJSONSync(path.resolve(configPath), 'utf8');
41-
diskConfig[pluginName] = false;
40+
41+
//add the plugin entry to patternlab-config.json
42+
if (!diskConfig.plugins) {
43+
diskConfig.plugins = {};
44+
}
45+
diskConfig.plugins[pluginName] = {
46+
enabled: true,
47+
initialized: false
48+
};
49+
50+
//write config entry back
4251
fs.outputFileSync(path.resolve(configPath), JSON.stringify(diskConfig, null, 2));
4352

4453
util.logGreen('Plugin ' + pluginName + ' installed.');
@@ -60,6 +69,7 @@ var plugin_manager = function (config, configPath) {
6069
var node_modules_path = path.join(process.cwd(), 'node_modules');
6170
return fs.readdirSync(node_modules_path).filter(function (dir) {
6271
var module_path = path.join(process.cwd(), 'node_modules', dir);
72+
console.log(module_path);
6373
return fs.statSync(module_path).isDirectory() && dir.indexOf('plugin-node-') === 0;
6474
});
6575
}

0 commit comments

Comments
 (0)