1
1
"use strict" ;
2
2
3
3
var fs = require ( 'fs-extra' ) ,
4
+ glob = require ( 'glob' ) ,
4
5
path = require ( 'path' ) ;
5
6
6
7
function plugin_init ( patternlab ) {
7
8
9
+ if ( ! patternlab ) {
10
+ console . error ( 'patternlab object not provided to plugin-init' ) ;
11
+ process . exit ( 1 ) ;
12
+ }
13
+
8
14
//write the plugin json to public/patternlab-components
9
15
var pluginConfig = {
10
16
"name" :"pattern-lab\/plugin-node-tab" ,
@@ -16,7 +22,32 @@ function plugin_init(patternlab) {
16
22
}
17
23
18
24
var pluginConfigPathName = path . resolve ( patternlab . config . paths . public . root , 'patternlab-components' , 'packages' ) ;
19
- fs . outputFileSync ( pluginConfigPathName + '/plugin-tab.json' , JSON . stringify ( pluginConfig , null , 2 ) ) ;
25
+ try {
26
+ fs . outputFileSync ( pluginConfigPathName + '/plugin-tab.json' , JSON . stringify ( pluginConfig , null , 2 ) ) ;
27
+ } catch ( ex ) {
28
+ console . trace ( 'Error occurred while writing pluginFile configuration' ) ;
29
+ console . log ( ex ) ;
30
+ }
31
+
32
+ //write the plugin dist folder to public/pattern-lab
33
+ var pluginFiles = glob . sync ( __dirname + '/dist/**/*' ) ;
34
+
35
+ if ( pluginFiles && pluginFiles . length > 0 ) {
36
+ for ( let i = 0 ; i < pluginFiles . length ; i ++ ) {
37
+ try {
38
+ var fileStat = fs . statSync ( pluginFiles [ i ] ) ;
39
+ if ( fileStat . isFile ( ) ) {
40
+ var relativePath = path . relative ( __dirname , pluginFiles [ i ] ) . replace ( 'dist' , '' ) ; //dist is dropped
41
+ var writePath = path . join ( patternlab . config . paths . public . root , 'patternlab-components' , 'pattern-lab' , 'plugin-node-tab' , relativePath ) ;
42
+ fs . copySync ( pluginFiles [ i ] , writePath ) ;
43
+ }
44
+ } catch ( ex ) {
45
+ console . trace ( 'Error occurred while copying pluginFile' , pluginFiles [ i ] ) ;
46
+ console . log ( ex ) ;
47
+ }
48
+ }
49
+ }
50
+
20
51
}
21
52
22
53
module . exports = plugin_init ;
0 commit comments