33const plugin_manager = function ( config , configPath ) {
44 const path = require ( 'path' ) ;
55 const fs = require ( 'fs-extra' ) ;
6+
7+ const _ = require ( 'lodash' ) ;
8+
69 const logger = require ( './log' ) ;
710
11+ const pluginMatcher = / ^ p l u g i n - ( .* ) $ / ;
12+
813 /**
914 * Loads a plugin
1015 *
11- * @param pluginName {string} the name of the plugin
16+ * @param modulePath {string} the path to the plugin
1217 * @return {object } the loaded plugin
1318 */
14- function loadPlugin ( pluginName ) {
15- return require ( path . join ( process . cwd ( ) , 'node_modules' , pluginName ) ) ;
19+ function loadPlugin ( modulePath ) {
20+ return require ( modulePath ) ;
1621 }
1722
1823 /**
@@ -21,6 +26,7 @@ const plugin_manager = function(config, configPath) {
2126 * @param pluginName {string} the name of the plugin
2227 */
2328 function installPlugin ( pluginName ) {
29+ console . log ( 'install plugin' ) ;
2430 try {
2531 const pluginPath = path . resolve (
2632 path . join ( process . cwd ( ) , 'node_modules' , pluginName )
@@ -43,8 +49,10 @@ const plugin_manager = function(config, configPath) {
4349 diskConfig . plugins = { } ;
4450 }
4551
46- if ( ! diskConfig . plugins [ pluginName ] ) {
47- diskConfig . plugins [ pluginName ] = {
52+ const safePluginName = _ . kebabCase ( pluginName ) ;
53+
54+ if ( ! diskConfig . plugins [ safePluginName ] ) {
55+ diskConfig . plugins [ safePluginName ] = {
4856 enabled : true ,
4957 initialized : false ,
5058 } ;
@@ -53,8 +61,8 @@ const plugin_manager = function(config, configPath) {
5361 const pluginPathConfig = path . resolve ( pluginPath , 'config.json' ) ;
5462 try {
5563 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 ;
5866 }
5967 } catch ( ex ) {
6068 //a config.json file is not required at this time
@@ -66,7 +74,7 @@ const plugin_manager = function(config, configPath) {
6674 JSON . stringify ( diskConfig , null , 2 )
6775 ) ;
6876
69- logger . info ( 'Plugin ' + pluginName + ' installed.' ) ;
77+ logger . info ( 'Plugin ' + safePluginName + ' installed.' ) ;
7078 logger . info ( 'Plugin configration added to patternlab-config.json.' ) ;
7179 }
7280 } catch ( ex ) {
@@ -97,19 +105,38 @@ const plugin_manager = function(config, configPath) {
97105 ) ;
98106 }
99107
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+
100124 return {
101125 install_plugin : function ( pluginName ) {
102126 installPlugin ( pluginName ) ;
103127 } ,
104- load_plugin : function ( pluginName ) {
105- return loadPlugin ( pluginName ) ;
128+ load_plugin : function ( modulePath ) {
129+ return loadPlugin ( modulePath ) ;
106130 } ,
107131 disable_plugin : function ( pluginName ) {
108132 disablePlugin ( pluginName ) ;
109133 } ,
110134 enable_plugin : function ( pluginName ) {
111135 enablePlugin ( pluginName ) ;
112136 } ,
137+ is_plugin : function ( filePath ) {
138+ return isPlugin ( filePath ) ;
139+ } ,
113140 } ;
114141} ;
115142
0 commit comments