fabiospampinato/watcher

Providing readdirMap only works if it has a depth of 1

aral opened this issue · 0 comments

aral commented

Use case

I want to use tiny-readdir to build my directory map and pass it to Watcher manually using the readdirMap option.

Example

import readdir from 'tiny-readdir'
import Watcher from 'watcher'

const ignoreRegExp = /(^|[\/\\])\.(?!local\/share\/)+|node_modules/
const pathToWatch = '/var/home/aral/sandbox/domain-fs-test'

const readdirMap = await readdir (pathToWatch, {
  depth: 20,
  limit: 1_000_000,
  followSymlinks: false,
  ignore: targetPath => ignoreRegExp.exec(targetPath.replace(pathToWatch, '')) !== null
})

const watcher = new Watcher(pathToWatch, {readdirMap})

What should happen

All files in readdirMap should be added to the watcher.

What actually happens

Only the files at depth 1 are added.

Recursive

If the watcher is instantiated with recursive: true:

const watcher = new Watcher(pathToWatch, {recursive: true, readdirMap})

Then readdirMap is ignored and all files (including the ones that were filtered out of readdirMap using tiny-readdir) are added to the watcher.

I believe the culprit is this line:

if ( readdirMap && depth === 1 && rootPath in readdirMap ) { // Reusing cached data

Workaround

At this point, the easiest thing to do is to use tiny-readdir to get the initial map and then instantiate watcher using the same ignore rules, etc., and with recursive: true and take the hit of building the directory/file map twice.