Skip to content

Commit 5a58824

Browse files
committed
feat(core): remove plugin install / disable / enable logic
moves initialize logic into plugin_manager closes #872
1 parent 29f0bb4 commit 5a58824

File tree

2 files changed

+23
-112
lines changed

2 files changed

+23
-112
lines changed

packages/core/src/lib/patternlab.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const cleanHtml = require('js-beautify').html;
77

88
const inherits = require('util').inherits;
99
const pm = require('./plugin_manager');
10+
const plugin_manager = new pm();
1011
const packageInfo = require('../../package.json');
1112
const events = require('./events');
12-
const findModules = require('./findModules');
1313
const buildListItems = require('./buildListItems');
1414
const dataLoader = require('./data_loader')();
1515
const loaduikits = require('./loaduikits');
@@ -77,7 +77,6 @@ module.exports = class PatternLab {
7777
// Verify correctness of configuration (?)
7878
this.checkConfiguration(this);
7979

80-
// TODO: determine if this is the best place to wire up plugins
8180
this.initializePlugins(this);
8281
}
8382

@@ -134,23 +133,11 @@ module.exports = class PatternLab {
134133
* Finds and calls the main method of any found plugins.
135134
* @param patternlab - global data store
136135
*/
137-
//todo, move this to plugin_manager
138136
initializePlugins(patternlab) {
139137
if (!patternlab.config.plugins) {
140138
return;
141139
}
142-
const plugin_manager = new pm(
143-
patternlab.config,
144-
path.resolve(__dirname, '../../patternlab-config.json')
145-
);
146-
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
147-
const foundPlugins = findModules(nodeModulesPath, plugin_manager.is_plugin);
148-
foundPlugins.forEach(plugin => {
149-
logger.info(`Found plugin: plugin-${plugin.name}`);
150-
logger.info(`Attempting to load and initialize plugin.`);
151-
const pluginModule = plugin_manager.load_plugin(plugin.modulePath);
152-
pluginModule(patternlab);
153-
});
140+
plugin_manager.intialize_plugins(patternlab);
154141
}
155142

156143
buildGlobalData(additionalData) {

packages/core/src/lib/plugin_manager.js

Lines changed: 21 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const plugin_manager = function(config, configPath) {
3+
const plugin_manager = function() {
44
const path = require('path');
5-
const fs = require('fs-extra');
5+
const findModules = require('./findModules');
66

77
const _ = require('lodash');
88

@@ -20,91 +20,6 @@ const plugin_manager = function(config, configPath) {
2020
return require(modulePath);
2121
}
2222

23-
/**
24-
* Installs a plugin
25-
*
26-
* @param pluginName {string} the name of the plugin
27-
*/
28-
function installPlugin(pluginName) {
29-
console.log('install plugin');
30-
try {
31-
const pluginPath = path.resolve(
32-
path.join(process.cwd(), 'node_modules', pluginName)
33-
);
34-
logger.debug(`Attempting to load plugin from ${pluginPath}`);
35-
let pluginDirStats;
36-
try {
37-
pluginDirStats = fs.statSync(pluginPath);
38-
} catch (ex) {
39-
logger.warning(`${pluginName} not found, use npm to install it first.`);
40-
logger.warning(`${pluginName} not loaded.`);
41-
return;
42-
}
43-
const pluginPathDirExists = pluginDirStats.isDirectory();
44-
if (pluginPathDirExists) {
45-
const diskConfig = fs.readJSONSync(path.resolve(configPath), 'utf8');
46-
47-
//add the plugin entry to patternlab-config.json
48-
if (!diskConfig.plugins) {
49-
diskConfig.plugins = {};
50-
}
51-
52-
const safePluginName = _.kebabCase(pluginName);
53-
54-
if (!diskConfig.plugins[safePluginName]) {
55-
diskConfig.plugins[safePluginName] = {
56-
enabled: true,
57-
initialized: false,
58-
};
59-
}
60-
61-
const pluginPathConfig = path.resolve(pluginPath, 'config.json');
62-
try {
63-
const pluginConfigJSON = require(pluginPathConfig);
64-
if (!diskConfig.plugins[safePluginName].options) {
65-
diskConfig.plugins[safePluginName].options = pluginConfigJSON;
66-
}
67-
} catch (ex) {
68-
//a config.json file is not required at this time
69-
}
70-
71-
//write config entry back
72-
fs.outputFileSync(
73-
path.resolve(configPath),
74-
JSON.stringify(diskConfig, null, 2)
75-
);
76-
77-
logger.info('Plugin ' + safePluginName + ' installed.');
78-
logger.info('Plugin configration added to patternlab-config.json.');
79-
}
80-
} catch (ex) {
81-
logger.warning(
82-
`An error occurred during plugin installation for plugin ${pluginName}`
83-
);
84-
logger.warning(ex);
85-
}
86-
}
87-
88-
/**
89-
* Disables an installed plugin
90-
* Not implemented yet
91-
*/
92-
function disablePlugin(pluginName) {
93-
logger.warning(
94-
`disablePlugin() not implemented yet. No change made to state of plugin ${pluginName}`
95-
);
96-
}
97-
98-
/**
99-
* Enables an installed plugin
100-
* Not implemented yet
101-
*/
102-
function enablePlugin(pluginName) {
103-
logger.warning(
104-
`enablePlugin() not implemented yet. No change made to state of plugin ${pluginName}`
105-
);
106-
}
107-
10823
/**
10924
* Given a path: return the plugin name if the path points to a valid plugin
11025
* module directory, or false if it doesn't.
@@ -121,20 +36,29 @@ const plugin_manager = function(config, configPath) {
12136
return false;
12237
}
12338

39+
/**
40+
* Looks for installed plugins, loads them, and invokes them
41+
* @param {object} patternlab
42+
*/
43+
function initializePlugins(patternlab) {
44+
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
45+
const foundPlugins = findModules(nodeModulesPath, plugin_manager.is_plugin);
46+
foundPlugins.forEach(plugin => {
47+
logger.info(`Found plugin: plugin-${plugin.name}`);
48+
logger.info(`Attempting to load and initialize plugin.`);
49+
const pluginModule = plugin_manager.load_plugin(plugin.modulePath);
50+
pluginModule(patternlab);
51+
});
52+
}
53+
12454
return {
125-
install_plugin: function(pluginName) {
126-
installPlugin(pluginName);
55+
intialize_plugins: patternlab => {
56+
initializePlugins(patternlab);
12757
},
128-
load_plugin: function(modulePath) {
58+
load_plugin: modulePath => {
12959
return loadPlugin(modulePath);
13060
},
131-
disable_plugin: function(pluginName) {
132-
disablePlugin(pluginName);
133-
},
134-
enable_plugin: function(pluginName) {
135-
enablePlugin(pluginName);
136-
},
137-
is_plugin: function(filePath) {
61+
is_plugin: filePath => {
13862
return isPlugin(filePath);
13963
},
14064
};

0 commit comments

Comments
 (0)