1
1
"use strict" ;
2
- const plutils = require ( './utilities' ) ;
3
2
const _ = require ( 'lodash' ) ;
4
3
const path = require ( 'path' ) ;
5
4
const process = require ( 'process' ) ;
6
5
7
- let copy = require ( 'recursive-copy' ) ; // eslint-disable-line
8
- let chokidar = require ( 'chokidar' ) ; // eslint-disable-line
6
+ let copy = require ( 'recursive-copy' ) ; // eslint-disable-line prefer-const
7
+ let chokidar = require ( 'chokidar' ) ; // eslint-disable-line prefer-const
9
8
10
9
const asset_copier = ( ) => {
11
10
@@ -44,7 +43,7 @@ const asset_copier = () => {
44
43
if ( options . debug ) {
45
44
console . log ( `Moved ${ p } to ${ dest } ` ) ;
46
45
}
47
- options . emitter . emit ( 'patternlab-file -change' , {
46
+ options . emitter . emit ( 'patternlab-asset -change' , {
48
47
file : p ,
49
48
dest : dest
50
49
} ) ;
@@ -67,14 +66,14 @@ const asset_copier = () => {
67
66
} ;
68
67
69
68
//loop through each directory asset object (source / public pairing)
70
- _ . each ( dirs , ( dir , key ) => {
69
+ _ . each ( dirs , ( dir ) => {
71
70
72
71
//if we want to watch files, do so, otherwise just copy each file
73
72
if ( options . watch ) {
74
73
if ( patternlab . config . debug ) {
75
74
console . log ( `Pattern Lab is watching ${ path . resolve ( basePath , dir . source ) } for changes` ) ;
76
75
}
77
- const watcher = chokidar . watch (
76
+ const assetWatcher = chokidar . watch (
78
77
path . resolve ( basePath , dir . source ) ,
79
78
{
80
79
ignored : / ( ^ | [ \/ \\ ] ) \. ./ ,
@@ -87,7 +86,7 @@ const asset_copier = () => {
87
86
) ;
88
87
89
88
//watch for changes and copy
90
- watcher . on ( 'addDir' , ( p ) => {
89
+ assetWatcher . on ( 'addDir' , ( p ) => {
91
90
const destination = path . resolve ( basePath , dir . public + '/' + path . basename ( p ) ) ;
92
91
copyFile ( p , destination , copyOptions ) ;
93
92
} ) . on ( 'add' , ( p ) => {
@@ -105,14 +104,49 @@ const asset_copier = () => {
105
104
}
106
105
} ) ;
107
106
108
-
109
107
//we need to special case patterns/**/*.md|.json|.pattern-extensions
110
108
if ( options . watch ) {
111
- console . log ( 111 , basePath , assetDirectories . source . patterns )
112
- const patterns = patternlab . engines . getSupportedFileExtensions ( ) . map ( dotExtension => path . join ( basePath , assetDirectories . source . patterns , `/**/*${ dotExtension } ` ) ) ;
113
- console . log ( 112 , patterns ) ;
114
- }
109
+ const baseFileExtensions = [ '.json' , '.yml' , '.yaml' , '.md' ] ;
110
+ const patternWatches = baseFileExtensions . concat ( patternlab . engines . getSupportedFileExtensions ( ) ) . map (
111
+ dotExtension => path . join (
112
+ basePath ,
113
+ assetDirectories . source . patterns ,
114
+ `/**/*${ dotExtension } `
115
+ )
116
+ ) ;
117
+ _ . each ( patternWatches , ( patternWatchPath ) => {
118
+ if ( patternlab . config . debug ) {
119
+ console . log ( `Pattern Lab is watching ${ patternWatchPath } for changes` ) ;
120
+ }
121
+
122
+ const patternWatcher = chokidar . watch (
123
+ path . resolve ( patternWatchPath ) ,
124
+ {
125
+ ignored : / ( ^ | [ \/ \\ ] ) \. ./ ,
126
+ ignoreInitial : true ,
127
+ awaitWriteFinish : {
128
+ stabilityThreshold : 200 ,
129
+ pollInterval : 100
130
+ }
131
+ }
132
+ ) ;
115
133
134
+ //watch for changes and rebuild
135
+ patternWatcher . on ( 'addDir' , ( p ) => {
136
+ patternlab . events . emit ( 'patternlab-pattern-change' , {
137
+ file : p
138
+ } ) ;
139
+ } ) . on ( 'add' , ( p ) => {
140
+ patternlab . events . emit ( 'patternlab-pattern-change' , {
141
+ file : p
142
+ } ) ;
143
+ } ) . on ( 'change' , ( p ) => {
144
+ patternlab . events . emit ( 'patternlab-pattern-change' , {
145
+ file : p
146
+ } ) ;
147
+ } ) ;
148
+ } ) ;
149
+ }
116
150
} ;
117
151
118
152
return {
0 commit comments