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.

PR-URL: #4023
Credit: @wraithgar
Close: #4023
Reviewed-by: @lukekarrys
  • Loading branch information
wraithgar committed Nov 10, 2021
1 parent 72ca4a4 commit b789491
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 200 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 b789491

Please sign in to comment.