adelsz / inquirer-fuzzy-path

Fuzzy file/directory search and select prompt for Inquirer.js
MIT License
88 stars 20 forks source link

Apply user filter before recursively reading directories #2

Closed pastudan closed 5 years ago

pastudan commented 5 years ago

Love this addon!

This is a performance tweak so that time isn't wasted descending into directories that will be filtered out of the prompt later on. This is most noticeable when you are working in a node package with lots of node_modules that you want to filter out.

This only works in Node 10.10+, as the withFileTypes: true and fs.Dirent type were added in this version.

pastudan commented 5 years ago

I just pushed up one more revision which allows you to define a separate function for filtering out unwanted directories. An example of when you might need this could be wanting to descend into directories, but not allowing them to be selected in the prompt.

adelsz commented 5 years ago

Hey @pastudan! Thanks for pointing to this issue and submitting the PR. You are right, filtered out directories should not be descended into. I do not think adding a separate scanFilter option solves this well, as it isn't clear how is it different from pathFilter. I have pushed a more conservative fix for this in this commit.

pastudan commented 5 years ago

The reason I put it in a separate function is because someone may want to not allow selecting a directory from the list of options, but still might want to descend into directories to show the files within.

This is the case with my usage. I need the user to select a file (not a directory), and also want to filter out node_modules.

My usage looks like this:

pathFilter: (isDirectory, path) => {
  return !isDirectory && !path.startsWith('node_modules')
},
scanFilter: (_isDirectory, path) => {
  return !path.startsWith('node_modules')
},

If I only used the pathFilter function, then the !isDirectory part prevents it from descending into other directories at all.

pastudan commented 5 years ago

Its hard to accommodate every use case while also keeping this library simple, but perhaps a less confusing way to do it would be to add a boolean option called showDirectories which could simply filter them out only at display time.

adelsz commented 5 years ago

Thanks @pastudan, that makes it clearer and it is an important use-case. I have pushed a new version adding a new itemType: 'any' | 'file' | 'directory' option to the config and replacing pathFilter with a simpler excludePath option.

pastudan commented 5 years ago

Excellent, thanks!

On Sun, Jan 27, 2019 at 4:48 AM Adel Salakh notifications@github.com wrote:

Thanks @pastudan https://github.com/pastudan, that makes it clearer and it is an important use-case. I have pushed a new version adding a new itemType: 'any' | 'file' | 'directory' option to the config and replacing pathFilter with a simpler excludePath option.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/adelsz/inquirer-fuzzy-path/pull/2#issuecomment-457914972, or mute the thread https://github.com/notifications/unsubscribe-auth/ABPHItu9ur2jSLad5CW-5k63VrE-dHVTks5vHaAbgaJpZM4aT433 .