UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

36 lines (30 loc) 1.19 kB
import chokidar from 'chokidar' import { EventEmitter } from 'events' import { resolve } from 'path' type FileWatcherMessage = 'ready' | 'created' | 'updated' | 'removed' | 'error' export interface FileWatcher { // on(msg: FileWatcherMessage, cb: (...args: any[]) => void): this on(msg: 'ready', cb: (path: string) => void): this on(msg: 'created', cb: (path: string) => void): this on(msg: 'updated', cb: (path: string) => void): this on(msg: 'removed', cb: (path: string) => void): this on(msg: 'error', cb: (error: string) => void): this emit(msg: FileWatcherMessage, ...args: any[]): boolean } export class FileWatcher extends EventEmitter { private watcher: chokidar.FSWatcher constructor(public location: string) { super() this.watcher = chokidar .watch(resolve(location)) .on('ready', () => this.emit('ready', location)) .on('add', path => this.emit('created', path)) .on('change', path => this.emit('updated', path)) .on('unlink', path => this.emit('removed', path)) .on('error', error => this.emit('error', error)) // .on('raw', (event, path, details) => {}); } stop() { this.watcher.close() } }