22
33const pluginName = 'plugin-node-tab' ;
44
5- var fs = require ( 'fs-extra' ) ,
5+ const fs = require ( 'fs-extra' ) ,
66 glob = require ( 'glob' ) ,
77 path = require ( 'path' ) ,
8+ EOL = require ( 'os' ) . EOL ,
89 tab_loader = require ( './src/tab-loader' ) ;
910
1011function onPatternIterate ( patternlab , pattern ) {
@@ -46,6 +47,8 @@ function pluginInit(patternlab) {
4647 process . exit ( 1 ) ;
4748 }
4849
50+ let fileTypes = require ( './package.json' ) . fileTypes ;
51+
4952 //write the plugin json to public/patternlab-components
5053 var pluginConfig = getPluginFrontendConfig ( ) ;
5154 var pluginConfigPathName = path . resolve ( patternlab . config . paths . public . root , 'patternlab-components' , 'packages' ) ;
@@ -66,13 +69,31 @@ function pluginInit(patternlab) {
6669 var pluginFiles = glob . sync ( __dirname + '/dist/**/*' ) ;
6770
6871 if ( pluginFiles && pluginFiles . length > 0 ) {
72+
73+ let tab_frontend_snippet = fs . readFileSync ( path . resolve ( __dirname + '/src/snippet.js' ) , 'utf8' ) ;
74+
6975 for ( let i = 0 ; i < pluginFiles . length ; i ++ ) {
7076 try {
7177 var fileStat = fs . statSync ( pluginFiles [ i ] ) ;
7278 if ( fileStat . isFile ( ) ) {
7379 var relativePath = path . relative ( __dirname , pluginFiles [ i ] ) . replace ( 'dist' , '' ) ; //dist is dropped
7480 var writePath = path . join ( patternlab . config . paths . public . root , 'patternlab-components' , 'pattern-lab' , pluginName , relativePath ) ;
75- fs . copySync ( pluginFiles [ i ] , writePath ) ;
81+
82+ //a message to future plugin authors:
83+ //depending on your plugin's job - you might need to alter the dist file instead of copying.
84+ //if you are simply copying dist files, you can probably do the below:
85+ //fs.copySync(pluginFiles[i], writePath);
86+
87+ //in this case, we need to alter the dist file to loop through our tabs to load as defined in the package.json
88+ //we are also being a bit lazy here, since we only expect one file
89+ let tabJSFileContents = fs . readFileSync ( pluginFiles [ i ] , 'utf8' ) ;
90+ var snippetString = '' ;
91+ for ( let j = 0 ; j < fileTypes . length ; j ++ ) {
92+ let tabSnippetLocal = tab_frontend_snippet . replace ( / < < t y p e > > / g, fileTypes [ j ] ) . replace ( / < < t y p e U C > > / g, fileTypes [ j ] . toUpperCase ( ) ) ;
93+ snippetString += tabSnippetLocal + EOL ;
94+ }
95+ tabJSFileContents = tabJSFileContents . replace ( '/*SNIPPETS*/' , snippetString ) ;
96+ fs . outputFileSync ( writePath , tabJSFileContents ) ;
7697 }
7798 } catch ( ex ) {
7899 console . trace ( 'plugin-node-tab: Error occurred while copying pluginFile' , pluginFiles [ i ] ) ;
0 commit comments