Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs are unclear about how to monitor files in nested directories #157

Open
sgronblo opened this issue Oct 11, 2022 · 1 comment
Open

Docs are unclear about how to monitor files in nested directories #157

sgronblo opened this issue Oct 11, 2022 · 1 comment

Comments

@sgronblo
Copy link

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.

@marc136
Copy link
Owner

marc136 commented Oct 12, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants