import * as chalk from "chalk"; import * as chokidar from "chokidar"; import {basename, extname} from "path"; import {Data} from "./data"; import {Config, getConfig} from "./config"; export class Watch { private io: any; private data: Data = new Data(); private config: Config = getConfig(); constructor(io: any) { this.io = io; } private eventLog(event: string, path: string): void { let d: Date = new Date(); let h: string = `0${d.getHours()}`.substr(-2); let m: string = `0${d.getMinutes()}`.substr(-2); let s: string = `0${d.getSeconds()}`.substr(-2); console.log(` ${chalk.red(`${h}:${m}:${s}`)}: ${chalk.green(path)} `); } private eventEmit(extType: string, name: string): void { const viewData: any = {}; viewData.extType = extType; viewData.name = name; this.io.emit('change', viewData); } public start(): any { console.log(`Watching ${this.config.src} for changes.`); return chokidar .watch(this.config.src, {ignored: /[\/\\]\./, persistent: true}) .on('change', (filePath: string) => { const ext: string = extname(filePath).replace(/\./, ''); const name: string = basename(filePath).replace(RegExp(extname(filePath)), ''); Object.keys(this.config.ext).map((type) => { if (this.config.ext[type] === ext) { this.eventEmit(type, name); } else if (this.config.ext[type]) { this.eventEmit('context', name); } }); this.eventLog('Changed', filePath); }); } }