anc95 / inquirer-file-tree-selection

inquirer prompt for select a file or dir by file tree
49 stars 26 forks source link

adds two options: `onlyShowValid` and `hideChildrenOfValid` #10

Closed hogpilot closed 4 years ago

hogpilot commented 4 years ago

Fixes #9

This is perhaps the most complicated of my recent pull requests, but in my opinion the most useful...

The bulk of new code in this PR is to pre-validate the file tree before it is rendered for the first time. If the user doesn't provide a validate function, then these options aren't triggered. If they do provide a validate function, this will add the following new options:

  1. onlyShowValid is a boolean which, when true, removes any file that isn't valid, and any folder that doesn't have a valid child.
  2. hideChildrenOfValid is a boolean which, when true, removes all children of a folder that is marked as valid (based on the validate function)

The first is pretty self-explanatory, and probably doesn't need any elaboration. However, the benefits of the latter may be more clear with a couple use cases.

If the purpose of the question is for the user to select a project directory from a file tree, then the validate method can check for the presence of a package.json file as a child of each directory. If the directory is thus valid, then there is no need to render any of it's children as the directory itself is the desired target for that question.

Similarly, the question may want the user to select a web application from a web server. In this case, the validate function may check for the presence of a web.config, .htaccess, or index.html. Setting hideChildrenOfValid would prevent the user from going deeper into the app's folder structure than necessary to answer the question.

anc95 commented 4 years ago

very thx for you contribution, advise and commits from you are very helpful, i will merge all the pr to master.