Skip to content

Commit b789491

Browse files
committed
fix(install): command completion with single match
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
1 parent 72ca4a4 commit b789491

File tree

2 files changed

+208
-200
lines changed

2 files changed

+208
-200
lines changed

lib/commands/install.js

+17-22
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,33 @@ class Install extends ArboristWorkspaceCmd {
7777
const partialName = partialWord.slice(lastSlashIdx + 1)
7878
const partialPath = partialWord.slice(0, lastSlashIdx) || '/'
7979

80-
const annotatePackageDirMatch = async sibling => {
81-
const fullPath = join(partialPath, sibling)
80+
const isDirMatch = async sibling => {
8281
if (sibling.slice(0, partialName.length) !== partialName) {
83-
return null
84-
} // not name match
82+
return false
83+
}
8584

8685
try {
87-
const contents = await readdir(fullPath)
88-
return {
89-
fullPath,
90-
isPackage: contents.indexOf('package.json') !== -1,
91-
}
86+
const contents = await readdir(join(partialPath, sibling))
87+
const result = (contents.indexOf('package.json') !== -1)
88+
return result
9289
} catch (er) {
93-
return { isPackage: false }
90+
return false
9491
}
9592
}
9693

9794
try {
9895
const siblings = await readdir(partialPath)
99-
const matches = await Promise.all(
100-
siblings.map(async sibling => {
101-
return await annotatePackageDirMatch(sibling)
102-
})
103-
)
104-
const match = matches.filter(el => !el || el.isPackage).pop()
105-
if (match) {
106-
// Success - only one match and it is a package dir
107-
return [match.fullPath]
108-
} else {
109-
// no matches
110-
return []
96+
const matches = []
97+
for (const sibling of siblings) {
98+
if (await isDirMatch(sibling)) {
99+
matches.push(sibling)
100+
}
101+
}
102+
if (matches.length === 1) {
103+
return [join(partialPath, matches[0])]
111104
}
105+
// no matches
106+
return []
112107
} catch (er) {
113108
return [] // invalid dir: no matching
114109
}

0 commit comments

Comments
 (0)