1
1
'use strict' ;
2
2
3
- const pluginName = 'plugin-node-tab' ;
3
+ const path = require ( 'path' ) ;
4
+ const EOL = require ( 'os' ) . EOL ;
4
5
6
+ const _ = require ( 'lodash' ) ;
5
7
const fs = require ( 'fs-extra' ) ;
6
8
const glob = require ( 'glob' ) ;
7
- const path = require ( 'path' ) ;
8
- const EOL = require ( 'os' ) . EOL ;
9
+
9
10
const tab_loader = require ( './src/tab-loader' ) ;
10
11
12
+ const pluginName = 'pattern-lab-plugin-tab' ;
13
+
11
14
function writeConfigToOutput ( patternlab , pluginConfig ) {
12
- var pluginConfigPathName = path . resolve (
13
- patternlab . config . paths . public . root ,
14
- 'patternlab-components' ,
15
- 'packages'
16
- ) ;
17
15
try {
18
- fs . outputFileSync (
19
- pluginConfigPathName + '/' + pluginName + '.json' ,
20
- JSON . stringify ( pluginConfig , null , 2 )
21
- ) ;
16
+ _ . each ( patternlab . uikits , uikit => {
17
+ fs . outputFileSync (
18
+ path . join (
19
+ process . cwd ( ) ,
20
+ uikit . outputDir ,
21
+ patternlab . config . paths . public . root ,
22
+ 'patternlab-components' ,
23
+ 'packages' ,
24
+ `/${ pluginName } .json`
25
+ ) ,
26
+ JSON . stringify ( pluginConfig , null , 2 )
27
+ ) ;
28
+ } ) ;
22
29
} catch ( ex ) {
23
30
console . trace (
24
31
pluginName + ': Error occurred while writing pluginFile configuration'
@@ -48,7 +55,7 @@ function registerEvents(patternlab) {
48
55
*/
49
56
function getPluginFrontendConfig ( ) {
50
57
return {
51
- name : 'pattern-lab/' + pluginName ,
58
+ name : pluginName ,
52
59
templates : [ ] ,
53
60
stylesheets : [ ] ,
54
61
javascripts : [
@@ -68,33 +75,38 @@ function getPluginFrontendConfig() {
68
75
* Instead, alter getPluginFrontendConfig() and registerEvents() methods
69
76
*/
70
77
function pluginInit ( patternlab ) {
78
+ console . log ( 'PLUGIN INIT!' ) ;
79
+
71
80
if ( ! patternlab ) {
72
- console . error ( 'patternlab object not provided to plugin-init ' ) ;
81
+ console . error ( 'patternlab object not provided to pluginInit ' ) ;
73
82
process . exit ( 1 ) ;
74
83
}
75
84
76
85
//write the plugin json to public/patternlab-components
77
- var pluginConfig = getPluginFrontendConfig ( ) ;
86
+ const pluginConfig = getPluginFrontendConfig ( ) ;
87
+
88
+ console . log ( patternlab . config . plugins ) ;
89
+
78
90
pluginConfig . tabsToAdd =
79
91
patternlab . config . plugins [ pluginName ] . options . tabsToAdd ;
80
92
writeConfigToOutput ( patternlab , pluginConfig ) ;
81
93
82
- var pluginConfigPathName = path . resolve (
83
- patternlab . config . paths . public . root ,
84
- 'patternlab-components' ,
85
- 'packages'
86
- ) ;
87
- try {
88
- fs . outputFileSync (
89
- pluginConfigPathName + '/' + pluginName + '.json' ,
90
- JSON . stringify ( pluginConfig , null , 2 )
91
- ) ;
92
- } catch ( ex ) {
93
- console . trace (
94
- 'plugin-node -tab: Error occurred while writing pluginFile configuration'
95
- ) ;
96
- console . log ( ex ) ;
97
- }
94
+ // const pluginConfigPathName = path.resolve(
95
+ // patternlab.config.paths.public.root,
96
+ // 'patternlab-components',
97
+ // 'packages'
98
+ // );
99
+ // try {
100
+ // fs.outputFileSync(
101
+ // pluginConfigPathName + '/' + pluginName + '.json',
102
+ // JSON.stringify(pluginConfig, null, 2)
103
+ // );
104
+ // } catch (ex) {
105
+ // console.trace(
106
+ // 'plugin-tab: Error occurred while writing pluginFile configuration'
107
+ // );
108
+ // console.log(ex);
109
+ // }
98
110
99
111
//add the plugin config to the patternlab-object
100
112
if ( ! patternlab . plugins ) {
@@ -103,22 +115,22 @@ function pluginInit(patternlab) {
103
115
patternlab . plugins . push ( pluginConfig ) ;
104
116
105
117
//write the plugin dist folder to public/pattern-lab
106
- var pluginFiles = glob . sync ( __dirname + '/dist/**/*' ) ;
118
+ const pluginFiles = glob . sync ( __dirname + '/dist/**/*' ) ;
107
119
108
120
if ( pluginFiles && pluginFiles . length > 0 ) {
109
- let tab_frontend_snippet = fs . readFileSync (
121
+ const tab_frontend_snippet = fs . readFileSync (
110
122
path . resolve ( __dirname + '/src/snippet.js' ) ,
111
123
'utf8'
112
124
) ;
113
125
114
126
for ( let i = 0 ; i < pluginFiles . length ; i ++ ) {
115
127
try {
116
- var fileStat = fs . statSync ( pluginFiles [ i ] ) ;
128
+ const fileStat = fs . statSync ( pluginFiles [ i ] ) ;
117
129
if ( fileStat . isFile ( ) ) {
118
- var relativePath = path
130
+ const relativePath = path
119
131
. relative ( __dirname , pluginFiles [ i ] )
120
132
. replace ( 'dist' , '' ) ; //dist is dropped
121
- var writePath = path . join (
133
+ const writePath = path . join (
122
134
patternlab . config . paths . public . root ,
123
135
'patternlab-components' ,
124
136
'pattern-lab' ,
@@ -134,10 +146,10 @@ function pluginInit(patternlab) {
134
146
//in this case, we need to alter the dist file to loop through our tabs to load as defined in the package.json
135
147
//we are also being a bit lazy here, since we only expect one file
136
148
let tabJSFileContents = fs . readFileSync ( pluginFiles [ i ] , 'utf8' ) ;
137
- var snippetString = '' ;
149
+ let snippetString = '' ;
138
150
if ( pluginConfig . tabsToAdd && pluginConfig . tabsToAdd . length > 0 ) {
139
151
for ( let j = 0 ; j < pluginConfig . tabsToAdd . length ; j ++ ) {
140
- let tabSnippetLocal = tab_frontend_snippet
152
+ const tabSnippetLocal = tab_frontend_snippet
141
153
. replace ( / < < t y p e > > / g, pluginConfig . tabsToAdd [ j ] )
142
154
. replace (
143
155
/ < < t y p e U C > > / g,
@@ -149,12 +161,18 @@ function pluginInit(patternlab) {
149
161
'/*SNIPPETS*/' ,
150
162
snippetString
151
163
) ;
152
- fs . outputFileSync ( writePath , tabJSFileContents ) ;
164
+ console . log ( 161 , tabJSFileContents ) ;
165
+ _ . each ( patternlab . uikits , uikit => {
166
+ fs . outputFileSync (
167
+ path . join ( process . cwd ( ) , uikit . outputDir , writePath ) ,
168
+ tabJSFileContents
169
+ ) ;
170
+ } ) ;
153
171
}
154
172
}
155
173
} catch ( ex ) {
156
174
console . trace (
157
- 'plugin-node- tab: Error occurred while copying pluginFile' ,
175
+ 'plugin-tab: Error occurred while copying pluginFile' ,
158
176
pluginFiles [ i ]
159
177
) ;
160
178
console . log ( ex ) ;
0 commit comments