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-iterator
Usage
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)
paths
Paths/globs to watch (passed to chokidar)chokidarOpts
Options passed to chokidaropts
debounce=100
Debounce 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
Files
instance with the following structure:.files
Complete 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>
Thekeys
are the actual file paths and values are:changed
A boolean that'strue
for files that changed,false
for the restevent
Chokidarevent
corresponding 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.files
object 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 = true
and.event
is one of theevents
providedevents = ['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