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

Is it possible to specify a maximum depth? #296

Closed
staff0rd opened this issue Oct 2, 2016 · 4 comments
Closed

Is it possible to specify a maximum depth? #296

staff0rd opened this issue Oct 2, 2016 · 4 comments

Comments

@staff0rd
Copy link

staff0rd commented Oct 2, 2016

For example if i have;

\some\path\
\another\directory\
\some\longer\path\
\an\even\longer\path\

Is there a way to specify a maximum depth that the tree will be traversed, such that executing glob against the above would only return the first two paths, being 2 deep?

@bluelovers
Copy link

need this

@isaacs
Copy link
Owner

isaacs commented Feb 28, 2023

For two, the easiest way would be to use a pattern like this: glob(['*', '*/*']) and it'll only go two directories deep. Obviously this isn't so great when you want to set the limit to something like 100 folders deep.

Currently, there's no way to set a maximum depth for patterns like ** though, no. It would be a bit tricky to do, but not entirely impossible. I'd approach it by first adding some concept of a "depth" field in PathScurry, which might actually have other benefits anyway.

isaacs added a commit to isaacs/path-scurry that referenced this issue Mar 1, 2023
Returns the depth of the current path in the directory tree.

Fix: #3
Re: isaacs/node-glob#296
@isaacs
Copy link
Owner

isaacs commented Mar 1, 2023

Now that PathScurry tracks the depth, I'm thinking it might be possible to do something like:

glob(pattern, { maxDepth: <some number> })

which would only go as deep as the depth of the current working directory, plus the number specified.

Would it be worthwhile to set a minimum depth as well? For example, to prevent absolute patterns or anything starting with ..?

This definitely has a few edge cases, because we translate ../**/x/x into the combined patterns ../x/x and **/x/x, since it's equivalent and shaves a factor off of the combinatorial explosion. So if you set minDepth: 0, then it would prevent the first part, but not the second. But a pattern like ../x/y/z, run in x, would not return anything, because it'd refuse to walk the parent directory. So that's a little weird.

@isaacs
Copy link
Owner

isaacs commented Mar 2, 2023

Also, would minDepth prevent the walk or just the match? 🤔

Like, in this case: glob('/a/b/c/d/**', { cwd: '/w/x/y/z', minDepth: 4 })

Would that match all the files in /a/b/c/d, because they're all above the depth of 4, or would it not do that, because to do that it has to walk to /a and that's depth 1?

And if it wouldn't match anything, what about this? glob('/a/b/c/d/**', { cwd: '/a/b/c/d', minDepth: 4 })

I think it's probably simpler to just say it limits maximum depth only, and prevents both the walk and the match. So:

glob('../**', { cwd: '/a/b/c', maxDepth:2 })

would find /a/b (at relative depth -1), and /a/b/c/d (relative depth 1) and would match /a/b/c/d/e but not walk over its children.

@isaacs isaacs closed this as completed in 9d3609e Mar 2, 2023
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

3 participants