forked from pattern-lab/patternlab-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_manager.js
More file actions
67 lines (57 loc) · 1.69 KB
/
plugin_manager.js
File metadata and controls
67 lines (57 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const plugin_manager = function() {
const path = require('path');
const findModules = require('./findModules');
const _ = require('lodash');
const logger = require('./log');
const pluginMatcher = /^plugin-(.*)$/;
/**
* Loads a plugin
*
* @param modulePath {string} the path to the plugin
* @return {object} the loaded plugin
*/
function loadPlugin(modulePath) {
return require(modulePath);
}
/**
* Given a path: return the plugin name if the path points to a valid plugin
* module directory, or false if it doesn't.
* @param filePath
* @returns Plugin name if exists or FALSE
*/
function isPlugin(filePath) {
const baseName = path.basename(filePath);
const pluginMatch = baseName.match(pluginMatcher);
if (pluginMatch) {
return pluginMatch[1];
}
return false;
}
/**
* Looks for installed plugins, loads them, and invokes them
* @param {object} patternlab
*/
function initializePlugins(patternlab) {
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
const foundPlugins = findModules(nodeModulesPath, plugin_manager.is_plugin);
foundPlugins.forEach(plugin => {
logger.info(`Found plugin: plugin-${plugin.name}`);
logger.info(`Attempting to load and initialize plugin.`);
const pluginModule = plugin_manager.load_plugin(plugin.modulePath);
pluginModule(patternlab);
});
}
return {
intialize_plugins: patternlab => {
initializePlugins(patternlab);
},
load_plugin: modulePath => {
return loadPlugin(modulePath);
},
is_plugin: filePath => {
return isPlugin(filePath);
},
};
};
module.exports = plugin_manager;