42 lines
1005 B
JavaScript
42 lines
1005 B
JavaScript
import { EventEmitter as Emitter } from 'events'
|
|
import path from 'path'
|
|
import chokidar from 'chokidar'
|
|
|
|
class Watcher extends Emitter {
|
|
constructor(options) {
|
|
super()
|
|
// pass in ignores
|
|
const opts = {
|
|
ignored: '**/node_modules/**',
|
|
ignoreInitial: true
|
|
}
|
|
const watcher = chokidar.watch(options.source, opts)
|
|
this.watcher = watcher
|
|
}
|
|
|
|
start() {
|
|
const handler = (type, f) => {
|
|
const fname = path.basename(f)
|
|
if ( fname.toLowerCase() === 'package.json')
|
|
if (type !=='change') {
|
|
this.emit('error',new Error('package.json was added or removed, ignoring sync and reinstall'))
|
|
return
|
|
} else{
|
|
this.emit('install', f)
|
|
}
|
|
this.emit('sync', f)
|
|
} // end handler
|
|
this.watcher
|
|
.on('add', handler.bind(this, 'add'))
|
|
.on('change', handler.bind(this, 'change'))
|
|
.on('unlink', handler.bind(this, 'remove'))
|
|
}
|
|
|
|
stop() {
|
|
this.watcher.close()
|
|
}
|
|
|
|
}
|
|
|
|
export default Watcher
|