mihneadb / node-directory-tree

Convert a directory tree to a JS object.
MIT License
523 stars 106 forks source link

exclude .git folder returns a null tree #71

Open nik-lus opened 5 years ago

nik-lus commented 5 years ago

I'm on macOs, node v12, directory-tree v2.2.4...

When I try to create a directory tree by excluding the .git folder:

const tree = dirTree(rootPath, { exclude: [/.git/] }); console.log('tree is ', tree)

I get:

tree is null

But when I specify an absolute path to the .git folder:

const tree = dirTree(rootPath, { exclude: [/\/absolute-path-to-folder\/.git/] }); console.log('tree is ', tree)

I get:

tree is Object{...}

I'd like to exclude all .git folders (without having to specify the absolute path to them).

Has anyone else encountered this? Am I using exclude incorrectly (exclude does work as expected on any other folders starting with ' . ' just not '.git')?

nik-lus commented 5 years ago

Quick follow up, just tested by excluding a 'git' folder (no ' . ' prefix)

const tree = dirTree(rootPath, { exclude: [/git/] }); console.log('tree is ', tree)

and again I get:

tree is null

Is the phrase 'git' somehow a reserved word in the codebase?

mihneadb commented 5 years ago

Hi! What is the full path of the folder you are testing? I think there might be a bug when the path contains what you want excluded https://github.com/mihneadb/node-directory-tree/blob/master/lib/directory-tree.js#L63.

nik-lus commented 5 years ago
path = '/usr/project/.git/'
excludes = [new RegExp('git')]
excludes.some((exclusion) => exclusion.test(path)); 
// from line 63: resolves to true which in turn returns null

Thanks for looking into this.

Edit: some clarification on the folder structure. The folder looks like this:

/usr
    /project
              /.git
              /node_modules
              /x_file_1.txt
              /x_file_2.js

And I'm trying to run the directory tree on path: /usr/project

nik-lus commented 5 years ago

After some more thought, could it be that '.git' is the first folder in the directory and the exclusion /.git/ trips the if statement on line 63 and it returns null and never goes further in the folder?

I can exclude /node_modules/ without a problem and that works as expected.

mihneadb commented 5 years ago

Sorry, shouldn't path be /usr/project? It's the path of the folder you want to be represented as a tree. Might be worth creating a branch/PR with a test case. You can see how the existing stuff is laid out.