@@ -4,6 +4,7 @@ import { fillConfigDefaults, generateBuildOptions, generateContext, replacePathV
4
4
import { Logger } from './util/logger' ;
5
5
import * as chokidar from 'chokidar' ;
6
6
7
+ // https://github.com/paulmillr/chokidar
7
8
8
9
export function watch ( context ?: BuildContext , options ?: BuildOptions , watchConfig ?: WatchConfig ) {
9
10
context = generateContext ( context ) ;
@@ -17,53 +18,73 @@ export function watch(context?: BuildContext, options?: BuildOptions, watchConfi
17
18
const logger = new Logger ( 'watch' ) ;
18
19
19
20
return build ( context , options ) . then ( ( ) => {
20
- startWatchers ( context , options , watchConfig ) ;
21
- return logger . ready ( ) ;
21
+ return startWatchers ( context , options , watchConfig ) . then ( ( ) => {
22
+ return logger . ready ( ) ;
23
+ } ) ;
22
24
23
25
} ) . catch ( ( err : Error ) => {
24
26
return logger . fail ( err ) ;
25
27
} ) ;
26
28
}
27
29
28
30
29
- export function startWatchers ( context : BuildContext , options : BuildOptions , watchConfig : WatchConfig ) {
30
- // https://github.com/paulmillr/chokidar
31
-
32
- watchConfig . watchers . forEach ( watcher => {
33
- if ( watcher . callback && watcher . paths ) {
34
- let taskPromise = Promise . resolve ( ) ;
35
- let nextTask : any = null ;
36
- const watcherOptions = watcher . options || { } ;
37
- if ( ! watcherOptions . cwd ) {
38
- watcherOptions . cwd = context . rootDir ;
39
- }
40
- if ( typeof watcherOptions . ignoreInitial !== 'boolean' ) {
41
- watcherOptions . ignoreInitial = true ;
42
- }
43
- const paths = cleanPaths ( context , watcher . paths ) ;
44
- const chokidarWatcher = chokidar . watch ( paths , watcherOptions ) ;
45
-
46
- chokidarWatcher . on ( 'all' , ( event : string , path : string ) => {
47
- setIonicEnvironment ( options . isProd ) ;
48
-
49
- Logger . debug ( `watch callback start, id: ${ watchCount } , isProd: ${ options . isProd } , event: ${ event } , path: ${ path } ` ) ;
50
-
51
- nextTask = watcher . callback . bind ( null , event , path , context , options ) ;
52
- taskPromise . then ( ( ) => {
53
- Logger . debug ( `watch callback complete, id: ${ watchCount } , isProd: ${ options . isProd } , event: ${ event } , path: ${ path } ` ) ;
54
- taskPromise = nextTask ( ) ;
55
- nextTask = null ;
56
- watchCount ++ ;
57
-
58
- } ) . catch ( err => {
59
- Logger . debug ( `watch callback error, id: ${ watchCount } , isProd: ${ options . isProd } , event: ${ event } , path: ${ path } ` ) ;
60
- Logger . debug ( `${ err } ` ) ;
61
- taskPromise = nextTask ( ) ;
62
- nextTask = null ;
63
- watchCount ++ ;
64
- } ) ;
65
- } ) ;
31
+ function startWatchers ( context : BuildContext , options : BuildOptions , watchConfig : WatchConfig ) {
32
+ const promises = watchConfig
33
+ . watchers
34
+ . filter ( w => w . callback && w . paths )
35
+ . map ( w => startWatcher ( w , context , options , watchConfig ) ) ;
36
+
37
+ return Promise . all ( promises ) ;
38
+ }
39
+
40
+
41
+ function startWatcher ( watcher : Watcher , context : BuildContext , options : BuildOptions , watchConfig : WatchConfig ) {
42
+ return new Promise ( ( resolve , reject ) => {
43
+
44
+ let taskPromise = Promise . resolve ( ) ;
45
+ let nextTask : any = null ;
46
+
47
+ const watcherOptions = watcher . options || { } ;
48
+ if ( ! watcherOptions . cwd ) {
49
+ watcherOptions . cwd = context . rootDir ;
50
+ }
51
+
52
+ if ( typeof watcherOptions . ignoreInitial !== 'boolean' ) {
53
+ watcherOptions . ignoreInitial = true ;
66
54
}
55
+ const paths = cleanPaths ( context , watcher . paths ) ;
56
+ const chokidarWatcher = chokidar . watch ( paths , watcherOptions ) ;
57
+
58
+ chokidarWatcher . on ( 'all' , ( event : string , path : string ) => {
59
+ setIonicEnvironment ( options . isProd ) ;
60
+
61
+ Logger . debug ( `watch callback start, id: ${ watchCount } , isProd: ${ options . isProd } , event: ${ event } , path: ${ path } ` ) ;
62
+
63
+ nextTask = watcher . callback . bind ( null , event , path , context , options ) ;
64
+ taskPromise . then ( ( ) => {
65
+ Logger . debug ( `watch callback complete, id: ${ watchCount } , isProd: ${ options . isProd } , event: ${ event } , path: ${ path } ` ) ;
66
+ taskPromise = nextTask ( ) ;
67
+ nextTask = null ;
68
+ watchCount ++ ;
69
+
70
+ } ) . catch ( err => {
71
+ Logger . debug ( `watch callback error, id: ${ watchCount } , isProd: ${ options . isProd } , event: ${ event } , path: ${ path } ` ) ;
72
+ Logger . debug ( `${ err } ` ) ;
73
+ taskPromise = nextTask ( ) ;
74
+ nextTask = null ;
75
+ watchCount ++ ;
76
+ } ) ;
77
+ } ) ;
78
+
79
+ chokidarWatcher . on ( 'ready' , ( ) => {
80
+ Logger . debug ( `watcher ready: ${ watcherOptions . cwd } ${ paths } ` ) ;
81
+ resolve ( ) ;
82
+ } ) ;
83
+
84
+ chokidarWatcher . on ( 'error' , ( err : any ) => {
85
+ Logger . error ( `watcher error: ${ watcherOptions . cwd } ${ paths } : ${ err } ` ) ;
86
+ reject ( ) ;
87
+ } ) ;
67
88
} ) ;
68
89
}
69
90
0 commit comments