Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 2affdc0

Browse files
committed
chore(watch): add ready promise and logging
1 parent 1fb2aa7 commit 2affdc0

File tree

1 file changed

+60
-39
lines changed

1 file changed

+60
-39
lines changed

src/watch.ts

+60-39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fillConfigDefaults, generateBuildOptions, generateContext, replacePathV
44
import { Logger } from './util/logger';
55
import * as chokidar from 'chokidar';
66

7+
// https://github.com/paulmillr/chokidar
78

89
export function watch(context?: BuildContext, options?: BuildOptions, watchConfig?: WatchConfig) {
910
context = generateContext(context);
@@ -17,53 +18,73 @@ export function watch(context?: BuildContext, options?: BuildOptions, watchConfi
1718
const logger = new Logger('watch');
1819

1920
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+
});
2224

2325
}).catch((err: Error) => {
2426
return logger.fail(err);
2527
});
2628
}
2729

2830

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;
6654
}
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+
});
6788
});
6889
}
6990

0 commit comments

Comments
 (0)