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

loading all files with certain name #1

Closed
brentonhouse opened this issue Apr 21, 2017 · 15 comments · Fixed by #2
Closed

loading all files with certain name #1

brentonhouse opened this issue Apr 21, 2017 · 15 comments · Fixed by #2

Comments

@brentonhouse
Copy link

After switching to klaw, I seem to be having some issue loading only files with a certain name.

Say I want to recursively load only files named package.json. It seems like I could do something like:

var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: '!**/package.json'})

I have also tried several other variations of the ignore pattern but nothing seems to match properly.

I am probably missing something obvious... Any ideas?

@manidlou
Copy link
Owner

@brentonhouse you are right. Thanks for your feedback and also sorry for inconvenience! The problem comes from the fact that the way klaw-sync is processing the path when ignore is passed, is incorrect. I already thought about it and found a possible solution (essentially the algorithm has to change a bit). I will fix it and publish it shortly.

@manidlou
Copy link
Owner

@brentonhouse klaw-sync is no longer support ignore option (as of version v2.0.0). You can use filter option to get what you want. Here is a typical example for getting all package.json files.

const path = require('path')
const klawSync = require('klaw-sync')

const filterFn = item => path.basename(item.path) === 'package.json'

const paths = klawSync('/some/dir', { filter: filterFn })

@manidlou manidlou removed the bug label Apr 24, 2017
@jmquigley
Copy link

jmquigley commented Apr 25, 2017

I have a question about this change. I was using ignore, but since this change I'm now using a filter in 2.0.0. I have the following code

const files = walk(directory, {
	nodir: true,
	filter: filterFn,
	noRecurseOnFailedFilter: true
});

My filter function excludes items properly, but it seems that it doesn't respect the "nodir" option; it is including directories too in the "files" return when all I want is files.

Is this expected behavior? How would I get rid of the directories in this call when using a filter function?

@manidlou
Copy link
Owner

@jmquigley thanks for your feedback. It is not an expected behavior. filter should respect nodir option. I will fix it then.

@manidlou
Copy link
Owner

@jmquigley v2.1.0 is released. Please give it a try whenever you can. Hopefully that resolves your issue.

@jmquigley
Copy link

It doesn't work as I would have expected (when using a filter function). If I have the following example structure:

/tmp/dir1/dir2/file.txt

And I walked the directory at /tmp, then I would expect to see:

/tmp/dir1
/tmp/dir1/dir2
/tmp/dir1/dir2/file.txt

If I leave off the nodir: true I see this now with 2.1.0. If I add this nodir option I would have expected to see just:

/tmp/dir1/dir2/file.txt

But that is not what I get back. What I get back now is an empty array. What is happening is that once a directory is suppressed with the nodir option, then all subdirectories appear to be gone along with the files. The whole example structure above is gone, when I would have expected to still get the file.

When I do this without the filter function it works as expected, but then I lose the custom filtering.

Hope this helps.

@manidlou
Copy link
Owner

manidlou commented Apr 26, 2017

It is a little hard to tell without knowing your filter function. If I guess right, you want to get only files that pass the filter function. Other thing, @jmquigley do you use noRecurseOnFailedFilter?

@jmquigley
Copy link

Here is the code:

const filterFn = (item: any) => {
	return self.ignore.every((it: string) => {
		return (item.path.indexOf(it) > -1) ? false : true;
	});
};

const files = walk(directory, {
	nodir: true,
	filter: filterFn,
	noRecurseOnFailedFilter: true
});

The self.ignore is an array of strings. It contains ['.DS_Store', 'Trash']. Basically I want to recursively get all files, no directories, from a root directory and then apply the filterFn to remove any path that has one of the strings in the ignore list.

@manidlou
Copy link
Owner

@jmquigley please try without noRecurseOnFailedFilter: true. Since you still want to read directories (recursively) even if they fail on filter function (to see if their contents contain any files that pass the filter).

@manidlou
Copy link
Owner

@jmquigley ping?!

@manidlou
Copy link
Owner

@jmquigley did that solve your issue?

@jmquigley
Copy link

Sorry, I haven't had the chance yet to test it again. I will do so in the next hour and get back to you.

@jmquigley
Copy link

Yes, that works as long as the option "noRecurseOnFailedFilter" is excluded from the options when using "nodir: true".

@manidlou
Copy link
Owner

Yes, that works as long as the option "noRecurseOnFailedFilter" is excluded from the options when using "nodir: true".

Great! I am happy that resolved your issue 😄

About noRecurseOnFailedFilter, I specifically added a little more details to docs to hopefully make it easy enough to understand why this option is provided and when it is useful.

@manidlou
Copy link
Owner

manidlou commented May 1, 2017

Closes via 68d40ec.

@manidlou manidlou closed this as completed May 1, 2017
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

Successfully merging a pull request may close this issue.

3 participants