Skip to content

Commit

Permalink
Fix default item selection
Browse files Browse the repository at this point in the history
  • Loading branch information
adelsz committed Mar 18, 2019
1 parent bb5da72 commit 0704526
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const fuzzy = require('fuzzy');

const readdir = util.promisify(fs.readdir);

function getPaths(rootPath, pattern, excludePath, itemType) {
function getPaths(
rootPath,
pattern,
excludePath,
itemType,
defaultItem,
) {
const fuzzOptions = {
pre: style.green.open,
post: style.green.close,
Expand Down Expand Up @@ -41,9 +47,15 @@ function getPaths(rootPath, pattern, excludePath, itemType) {

const nodes = listNodes(rootPath);
const filterPromise = nodes.then(
nodeList => fuzzy
.filter(pattern || '', nodeList, fuzzOptions)
.map(e => e.string),
(nodeList) => {
const filteredNodes = fuzzy
.filter(pattern || '', nodeList, fuzzOptions)
.map(e => e.string);
if (!pattern && defaultItem) {
filteredNodes.unshift(defaultItem);
}
return filteredNodes;
},
);
return filterPromise;
}
Expand All @@ -56,7 +68,15 @@ class InquirerFuzzyPath extends InquirerAutocomplete {
const questionBase = Object.assign(
{},
question,
{ source: (_, pattern) => getPaths(rootPath, pattern, excludePath, itemType) },
{
source: (_, pattern) => getPaths(
rootPath,
pattern,
excludePath,
itemType,
question.default,
),
},
);
super(questionBase, rl, answers);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inquirer-fuzzy-path",
"version": "2.0.2",
"version": "2.0.3",
"description": "Fuzzy file/directory search and select prompt for inquirer.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 0704526

Please sign in to comment.