file-watch-iterator v0.7.0
file-watch-iterator
A simple wrapper around chokidar that returns an async iterator which you can for-await on to get the initial, and then later changed files.
Requires ES2018 Async Iteration
Install
npm i file-watch-iteratorUsage
const watch = require('file-watch-iterator')
for await (const files of watch('.')) {
for (const file of files.changed(a)) {
// ...
}
for (const file of files.deleted()) {
// ...
}
}API
watch(paths, chokidarOpts, opts)pathsPaths/globs to watch (passed to chokidar)chokidarOptsOptions passed to chokidaroptsdebounce=100Debounce between file change as well as an indicator of first ever "ready" event (when (initially) the files are "changed" (discovered) very rapidly)
Returns an async-iterable which yields a
Filesinstance with the following structure:.filesComplete and updated list of files:Eg.:
{ '/example/a': {changed: false, event: 'add'}, // previously added '/example/b': {changed: true, event: 'change'}, // newly modified '/example/c': {changed: true, event: 'add'}, // newly added '/example/d': {changed: true, event: 'unlink'}, // newly deleted }<key>Thekeysare the actual file paths and values are:changedA boolean that'struefor files that changed,falsefor the resteventChokidareventcorresponding to the file change
Note: This is more meant for internal use. You may find other methods more useful.
.toArray()Returns an iterable of a modified.filesobject as:{file, changed, event}objectsEg.:
for(const file of files) { console.log(file) }{file: '/example/a', changed: false, event: 'add'} {file: '/example/b', changed: true, event: 'change'} {file: '/example/c', changed: true, event: 'add'} {file: '/example/d', changed: true, event: 'unlink'}.changed(events)Returns an iterable of files whose.changed = trueand.eventis one of theeventsprovidedevents = ['change', 'add']Events to match with the file's.event
Eg.:
for(const file of files.changed()) { console.log(file) }/example/b /example/c.deleted()Alias for.changed(['unlink'])Eg.:
for(const file of files.deleted()) { console.log(file) }/example/d