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

Make directories "valid" by default but not selectable #20

Open
jookshub opened this issue Nov 17, 2020 · 2 comments
Open

Make directories "valid" by default but not selectable #20

jookshub opened this issue Nov 17, 2020 · 2 comments

Comments

@jookshub
Copy link

jookshub commented Nov 17, 2020

Say you want to display only files of a specific extension, like .js this could be your code:

// execute with "ts-node ./demo.ts"

import inquirer from 'inquirer';
import inquirerFileTreeSelection from 'inquirer-file-tree-selection-prompt';
import { extname } from 'path';
import { lstat, PathLike } from 'fs';

inquirer.registerPrompt('file-tree-selection', inquirerFileTreeSelection);

inquirer.prompt({
  type: 'file-tree-selection',
  name: 'file',
  message: 'Please select a JS file',
  onlyShowValid: true,
  multiple: false,
  pageSize: 10,
  root: './db',
  validate: async (item) => {
    return extname(item) === '.js' || await isDirectory(item);
  }
}).then(
  ({file}) =>
  console.log('selected', file)
);

async function isDirectory(path: PathLike) {
  return new Promise((resolve) => {
    lstat(path,
      (err, stat) => {
        if (!err) {
          resolve(stat.isDirectory());
        } else {
          resolve(false);
        }
      });
  });
}

However, this will show folders and these folders will now be valid selections!

It would be better, if you could not select folders using enter but still benefit from onlyShowValid.

@skydiver
Copy link

skydiver commented Nov 2, 2021

Hey @jookshub, do you find any solution?

@jeffrigby
Copy link

I would love this too!

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

No branches or pull requests

3 participants