uci-utils-watcher/example/example.js

63 lines
1.3 KiB
JavaScript

import Watcher from '../src/watcher.js'
import onDeath from 'ondeath'
const USE_CUSTOM_HANDLER=true
const DEBOUNCE=0
const READY_TIMEOUT=null
;
(async () => {
let options = {
source:'./example/repo/**',
ignored:['**/dontwatch.js'],
ignoreList:['./example/repo/.gitignore'],
debounce: DEBOUNCE,
readyTimeout:READY_TIMEOUT
}
let watcher = new Watcher(options)
watcher.on('ready', (state,opts) =>{
console.log('watched files indexed and ready??',state)
if (opts) console.dir(opts)
})
watcher.on('watching', (state, opts) =>{
console.log('watcher is active and listening for changes?',state)
if (opts) console.dir(opts)
})
await watcher.start(
// {ignored:'**/another'}
)
if (USE_CUSTOM_HANDLER) {
watcher.registerHandler(
function handler (type, f) {
this.emit('custom', f, type)
} // end handler
)
watcher.on('custom',
(file, type) => {
console.log(`custom handler ======= file ${file} was ${type} ==========`)
})
}
// default handler event is `changed`
else watcher.on('changed',
(file,type) => {
console.log(`======= file ${file} was ${type} ==========`)
})
onDeath( () => {
console.log('\nHe\'s dead Jim')
watcher.remove()
})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})