0.7.0 • Published 4 years ago

file-watch-iterator v0.7.0

Weekly downloads
435
License
-
Repository
github
Last release
4 years ago

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 chokidar

    • opts

      • 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> The keys are the actual file paths and values are:

          • changed A boolean that's true for files that changed, false for the rest
          • event Chokidar event 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} objects

        Eg.:

        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 the events provided

        • events = ['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
0.7.0

4 years ago

0.6.3

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago