import { ITypeCheckerOptions } from './interfaces'; import * as watch from 'watch'; import { getPath } from './getPath'; import { debugPrint } from './debugPrint'; import { Logger } from './logger'; let watchTimeout: any; export function watchSrc(pathToWatch: string, options: ITypeCheckerOptions, callback: Function) { debugPrint('wwatchSrc' + pathToWatch); let basePath = getPath(pathToWatch, options); watch.createMonitor(basePath, (monitor: any) => { // todo-> move into thread // tell user what path we are watching Logger.echo(` Typechecker watching: ${basePath}`); // on created file event monitor.on('created', (f: any /*, stat: any*/) => { Logger.echo(`\n File created: ${f}`); callback(); }); // on changed file event monitor.on('changed', (f: any /*, curr: any, prev: any*/) => { // tell user about event Logger.echo(`\n File changed: ${f}`); Logger.echo(` Calling typechecker`); // have inside timeout, so we only run once when multiple files are saved clearTimeout(watchTimeout); watchTimeout = setTimeout(() => { callback(); }, 100); }); monitor.on('removed', (f: any /*, stat: any*/) => { // tell user about event Logger.echo(`\n File removed: ${f}`); Logger.echo(` Calling typechecker`); // have inside timeout, so we only run once when multiple files are saved clearTimeout(watchTimeout); watchTimeout = setTimeout(() => { callback(); }, 100); }); }); }