node-folder-hash icon indicating copy to clipboard operation
node-folder-hash copied to clipboard

Docs are unclear about how to monitor files in nested directories

Open sgronblo opened this issue 3 years ago • 1 comments

I wanted to monitor files inside a nested directory. Let's say src/app/entities/*.ts.

I tried using something like:

  const hashResult = await folderHash.hashElement(projectDir, {
    folders: {
      include: [
        'src/app/entities',
      ],
      matchPath: true,
      matchBasename: false,
    },
    files: {
      include: ['*.entity.ts'],
    },
    encoding: 'hex',
  })

But it didn't work since the nested directory segments are expected to match the include entries (eg. src is matched against src/app which doesn't match and therefore stops descending). Therefore I had to add separate entries for each level of nesting like this for it to work:

  const hashResult = await folderHash.hashElement(projectDir, {
    folders: {
      include: [
        'src',
        'src/app',
        'src/app/entities',
      ],
      matchPath: true,
      matchBasename: false,
    },
    files: {
      include: ['*.entity.ts'],
    },
    encoding: 'hex',
  })

It might be that I am just missing something or the support for nested include folders could be improved to handle this case.

sgronblo avatar Oct 11 '22 07:10 sgronblo

Thank you for bringing this up @sgronblo,

If you only want to hash files in a subdirectory, you could use this:

const hashResult = await folderHash.hashElement(path.join(projectDir, 'src/app/entities'), {
    files: {
      include: ['*.entity.ts'],
    },
    encoding: 'hex',
  })

But I assume you want to hash files in multiple directories, so either of these could work:

const hashResult = await folderHash.hashElement(projectDir, {
    files: {
      include: ['*.entity.ts'],
    },
    encoding: 'hex',
  })

or

const hashResult = await folderHash.hashElement(projectDir, {
    files: {
      include: ['src/app/entities/*.entity.ts', 'src/*/entities/*.entity.ts'],
      matchPath: true,
      matchBasename: false,
    },
    encoding: 'hex',
  })

But it would potentially traverse too many directories that should be ignored. Maybe you can exclude some directories to reduce the amount of unnecessary checks, but it is not optimal.


I would consider the current behavior a bug.

If matchPath is true, then during traversal, it should also check if the current path is a partial of a path to include.

I have no time to fix it this week, but I would also be open to a pull request if you are interested.

marc136 avatar Oct 12 '22 20:10 marc136