3
3
const plugin_manager = function ( config , configPath ) {
4
4
const path = require ( 'path' ) ;
5
5
const fs = require ( 'fs-extra' ) ;
6
+
7
+ const _ = require ( 'lodash' ) ;
8
+
6
9
const logger = require ( './log' ) ;
7
10
11
+ const pluginMatcher = / ^ p l u g i n - ( .* ) $ / ;
12
+
8
13
/**
9
14
* Loads a plugin
10
15
*
11
- * @param pluginName {string} the name of the plugin
16
+ * @param modulePath {string} the path to the plugin
12
17
* @return {object } the loaded plugin
13
18
*/
14
- function loadPlugin ( pluginName ) {
15
- return require ( path . join ( process . cwd ( ) , 'node_modules' , pluginName ) ) ;
19
+ function loadPlugin ( modulePath ) {
20
+ return require ( modulePath ) ;
16
21
}
17
22
18
23
/**
@@ -21,6 +26,7 @@ const plugin_manager = function(config, configPath) {
21
26
* @param pluginName {string} the name of the plugin
22
27
*/
23
28
function installPlugin ( pluginName ) {
29
+ console . log ( 'install plugin' ) ;
24
30
try {
25
31
const pluginPath = path . resolve (
26
32
path . join ( process . cwd ( ) , 'node_modules' , pluginName )
@@ -43,8 +49,10 @@ const plugin_manager = function(config, configPath) {
43
49
diskConfig . plugins = { } ;
44
50
}
45
51
46
- if ( ! diskConfig . plugins [ pluginName ] ) {
47
- diskConfig . plugins [ pluginName ] = {
52
+ const safePluginName = _ . kebabCase ( pluginName ) ;
53
+
54
+ if ( ! diskConfig . plugins [ safePluginName ] ) {
55
+ diskConfig . plugins [ safePluginName ] = {
48
56
enabled : true ,
49
57
initialized : false ,
50
58
} ;
@@ -53,8 +61,8 @@ const plugin_manager = function(config, configPath) {
53
61
const pluginPathConfig = path . resolve ( pluginPath , 'config.json' ) ;
54
62
try {
55
63
const pluginConfigJSON = require ( pluginPathConfig ) ;
56
- if ( ! diskConfig . plugins [ pluginName ] . options ) {
57
- diskConfig . plugins [ pluginName ] . options = pluginConfigJSON ;
64
+ if ( ! diskConfig . plugins [ safePluginName ] . options ) {
65
+ diskConfig . plugins [ safePluginName ] . options = pluginConfigJSON ;
58
66
}
59
67
} catch ( ex ) {
60
68
//a config.json file is not required at this time
@@ -66,7 +74,7 @@ const plugin_manager = function(config, configPath) {
66
74
JSON . stringify ( diskConfig , null , 2 )
67
75
) ;
68
76
69
- logger . info ( 'Plugin ' + pluginName + ' installed.' ) ;
77
+ logger . info ( 'Plugin ' + safePluginName + ' installed.' ) ;
70
78
logger . info ( 'Plugin configration added to patternlab-config.json.' ) ;
71
79
}
72
80
} catch ( ex ) {
@@ -97,19 +105,38 @@ const plugin_manager = function(config, configPath) {
97
105
) ;
98
106
}
99
107
108
+ /**
109
+ * Given a path: return the plugin name if the path points to a valid plugin
110
+ * module directory, or false if it doesn't.
111
+ * @param filePath
112
+ * @returns Plugin name if exists or FALSE
113
+ */
114
+ function isPlugin ( filePath ) {
115
+ const baseName = path . basename ( filePath ) ;
116
+ const pluginMatch = baseName . match ( pluginMatcher ) ;
117
+
118
+ if ( pluginMatch ) {
119
+ return pluginMatch [ 1 ] ;
120
+ }
121
+ return false ;
122
+ }
123
+
100
124
return {
101
125
install_plugin : function ( pluginName ) {
102
126
installPlugin ( pluginName ) ;
103
127
} ,
104
- load_plugin : function ( pluginName ) {
105
- return loadPlugin ( pluginName ) ;
128
+ load_plugin : function ( modulePath ) {
129
+ return loadPlugin ( modulePath ) ;
106
130
} ,
107
131
disable_plugin : function ( pluginName ) {
108
132
disablePlugin ( pluginName ) ;
109
133
} ,
110
134
enable_plugin : function ( pluginName ) {
111
135
enablePlugin ( pluginName ) ;
112
136
} ,
137
+ is_plugin : function ( filePath ) {
138
+ return isPlugin ( filePath ) ;
139
+ } ,
113
140
} ;
114
141
} ;
115
142
0 commit comments