inquirer-file-tree-selection icon indicating copy to clipboard operation
inquirer-file-tree-selection copied to clipboard

Make directories "valid" by default but not selectable

Open jookshub opened this issue 4 years ago • 2 comments

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.

jookshub avatar Nov 17 '20 04:11 jookshub

Hey @jookshub, do you find any solution?

skydiver avatar Nov 02 '21 02:11 skydiver

I would love this too!

jeffrigby avatar Jul 24 '23 21:07 jeffrigby