@codedoc/core
Version:
Create beautiful modern documentation websites.
39 lines • 1.74 kB
JavaScript
import _watch from 'node-watch';
import chalk from 'chalk';
import { join } from 'path';
import { Subject, BehaviorSubject, of } from 'rxjs';
import { buffer, debounceTime, filter, tap, delayWhen } from 'rxjs/operators';
export function watch(root, config, notifier) {
const watchbase = join(root, config.src.base, '/');
const filechange$ = new Subject();
const request$ = new Subject();
const build$ = new BehaviorSubject(false);
_watch(watchbase, {
recursive: true,
filter: (f) => (config.src.pick.test(f) && !config.src.drop.test(f) ||
config.src.toc === f.substr(watchbase.length)),
}, (event, filename) => {
if (event === 'update')
filechange$.next(filename.substr(watchbase.length));
});
filechange$.pipe(tap(filename => {
if (notifier)
console.log(chalk `{blue # Changes in {magenta ${join(config.src.base, filename)}} queueing ...}`);
request$.next('queued');
}), buffer(filechange$.pipe(debounceTime(500), delayWhen(() => !build$.value ? of(true) : build$.pipe(filter(_ => !_))))), filter(chanegs => chanegs.length > 0)).subscribe(changedFiles => {
if (notifier) {
build$.next(true);
console.log(chalk `{gray # Rebuilding due to changes ...}`);
}
changedFiles = changedFiles.filter((file, index) => changedFiles.indexOf(file) === index);
if (changedFiles.includes(config.src.toc)) {
request$.next('all');
}
else {
request$.next(changedFiles);
}
notifier === null || notifier === void 0 ? void 0 : notifier.subscribe(() => build$.next(false));
});
return request$;
}
//# sourceMappingURL=watch.js.map