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

Some question about ignore directory #33

Open
ViavaCos opened this issue Jun 15, 2022 · 3 comments
Open

Some question about ignore directory #33

ViavaCos opened this issue Jun 15, 2022 · 3 comments

Comments

@ViavaCos
Copy link

Hello, is there have any parameter for distinction keep node_modules or hide it.

When i run tree -I "node_modules" i got a result as below:

my-directory
├── assets
│   └── a.png
├── index.js
├── package-lock.json
├── package.json
├── readme.md
└── test.js

The result i want(i want to keep itself when i add a ignore directory):

my-directory
├── node_modules
├── assets
│   └── a.png
├── index.js
├── package-lock.json
├── package.json
├── readme.md
└── test.js
@jtroussard
Copy link
Contributor

jtroussard commented Nov 28, 2024

This is an older question but wanted to answer in case it can help others.

TL;DR

This can already be accomplished with the existing functionality. Just use a more specific RegEx In your case you should use:

tree -I "node_modules/"

Long Answer/Explanation

The key is understanding that the string being matched for the directory itself (e.g., my-directory/node_modules) does not include a trailing slash by default. If you want to exclude the files inside the directory but keep the directory itself in your tree output, you need to adjust your regex pattern to match only the contents of the directory. To achieve this, ensure your pattern matches everything after the directory slash.

tree -I "node_modules/" ... and /node_modules\// for the Node.js API

If you're curious, you can debug the exclusion logic by watching this section of the code:

// Handle excluded patterns.
for (let i = 0; i < options.exclude.length; i++) {
if (options.exclude[i].test(path)) {
return lines;
}
}

You'll want to place a break-point or console log something like this:

console.log(\`Regex: ${options.exclude[i]}, Path: ${path}, Matched: ${options.exclude[i].test(path)}\`);

Make sure to test with a simple example to avoid sifting through console logs or breakpoints until the end of time. Hope this helps and happy coding.

Cheers!

@jtroussard
Copy link
Contributor

@slorber, This might be considered a non-issue and could be closed. However, it might be helpful to update the README with a concise explanation of this matching behavior for clarity.

@slorber
Copy link
Collaborator

slorber commented Dec 3, 2024

Hey @jtroussard , my time is a bit limited to maintain this package. I only became maintainer of it because I needed it for Docusaurus 😅 I never even used it as a CLI.

If you feel that we can explain something better in the README, please send a docs PR directly

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