Skip to content

Commit d9e385c

Browse files
committed
Merge remote-tracking branch 'origin/features/plugins-redux' into dev
2 parents 89e086c + b4903ad commit d9e385c

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

core/lib/patternlab.js

Lines changed: 31 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,42 @@ 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+
if (patternlab.config.debug) {
100+
console.log('Found plugin: ', pluginKey);
101+
console.log('Attempting to load and initialize plugin.');
102+
}
103+
104+
var plugin = plugin_manager.load_plugin(pluginKey);
93105
plugin(patternlab);
94106
}
95107
}
96108
}
97109

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

106131
var JSON5 = require('json5'),
107-
fs = require('fs-extra'),
108132
pa = require('./pattern_assembler'),
109133
pe = require('./pattern_exporter'),
110134
lh = require('./lineage_hunter'),
@@ -123,7 +147,6 @@ var patternlab_engine = function (config) {
123147

124148
checkConfiguration(patternlab);
125149

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

129152
var paths = patternlab.config.paths;
@@ -504,6 +527,9 @@ var patternlab_engine = function (config) {
504527
},
505528
loadstarterkit: function (starterkitName, clean) {
506529
loadStarterKit(starterkitName, clean);
530+
},
531+
installplugin: function (pluginName) {
532+
installPlugin(pluginName);
507533
}
508534
};
509535
};

core/lib/plugin_manager.js

Lines changed: 11 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.');

0 commit comments

Comments
 (0)