Skip to content

Commit

Permalink
fix(install): command completion with single match
Browse files Browse the repository at this point in the history
During a refactoring of the tests a bug was found in the install command
completion that would return nothing if there was a valid match, this
fixes that bug and also makes the tests actually test things.
  • Loading branch information
wraithgar committed Nov 9, 2021
1 parent 2d6da1f commit a6c542f
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 235 deletions.
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

0 comments on commit a6c542f

Please sign in to comment.