tree-node-cli icon indicating copy to clipboard operation
tree-node-cli copied to clipboard

Some question about ignore directory

Open ViavaCos opened this issue 3 years ago • 3 comments

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

ViavaCos avatar Jun 15 '22 13:06 ViavaCos

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:

https://github.com/yangshun/tree-node-cli/blob/42797f074a37ecb8c7a3004884016c7a5b5288d9/src/index.js#L71-L76

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 avatar Nov 28 '24 20:11 jtroussard

@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.

jtroussard avatar Nov 30 '24 16:11 jtroussard

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

slorber avatar Dec 03 '24 13:12 slorber