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

fix(install): command completion with single match #4023

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,33 @@ class Install extends ArboristWorkspaceCmd {
const partialName = partialWord.slice(lastSlashIdx + 1)
const partialPath = partialWord.slice(0, lastSlashIdx) || '/'

const annotatePackageDirMatch = async sibling => {
const fullPath = join(partialPath, sibling)
const isDirMatch = async sibling => {
if (sibling.slice(0, partialName.length) !== partialName) {
return null
} // not name match
return false
}

try {
const contents = await readdir(fullPath)
return {
fullPath,
isPackage: contents.indexOf('package.json') !== -1,
}
const contents = await readdir(join(partialPath, sibling))
const result = (contents.indexOf('package.json') !== -1)
return result
} catch (er) {
return { isPackage: false }
return false
}
}

try {
const siblings = await readdir(partialPath)
const matches = await Promise.all(
siblings.map(async sibling => {
return await annotatePackageDirMatch(sibling)
})
)
const match = matches.filter(el => !el || el.isPackage).pop()
if (match) {
// Success - only one match and it is a package dir
return [match.fullPath]
} else {
// no matches
return []
const matches = []
for (const sibling of siblings) {
if (await isDirMatch(sibling)) {
matches.push(sibling)
}
}
if (matches.length === 1) {
return [join(partialPath, matches[0])]
}
// no matches
return []
} catch (er) {
return [] // invalid dir: no matching
}
Expand Down
Loading