1
1
'use strict' ;
2
2
3
- const plugin_manager = function ( config , configPath ) {
3
+ const plugin_manager = function ( ) {
4
4
const path = require ( 'path' ) ;
5
- const fs = require ( 'fs-extra ' ) ;
5
+ const findModules = require ( './findModules ' ) ;
6
6
7
7
const _ = require ( 'lodash' ) ;
8
8
@@ -20,91 +20,6 @@ const plugin_manager = function(config, configPath) {
20
20
return require ( modulePath ) ;
21
21
}
22
22
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
-
108
23
/**
109
24
* Given a path: return the plugin name if the path points to a valid plugin
110
25
* module directory, or false if it doesn't.
@@ -121,20 +36,29 @@ const plugin_manager = function(config, configPath) {
121
36
return false ;
122
37
}
123
38
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
+
124
54
return {
125
- install_plugin : function ( pluginName ) {
126
- installPlugin ( pluginName ) ;
55
+ intialize_plugins : patternlab => {
56
+ initializePlugins ( patternlab ) ;
127
57
} ,
128
- load_plugin : function ( modulePath ) {
58
+ load_plugin : modulePath => {
129
59
return loadPlugin ( modulePath ) ;
130
60
} ,
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 => {
138
62
return isPlugin ( filePath ) ;
139
63
} ,
140
64
} ;
0 commit comments