Skip to content

Commit

Permalink
tidy this error; tidy the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Dec 12, 2022
1 parent b872457 commit e5ae8f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/app.err-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async function suggestions(err: TeaError) {
switch (err.id) {
case 'not-found: pantry: package.yml': {
const suggestion = await usePantry().getClosestPackageSuggestion(err.ctx.project)
return `did you mean \`${suggestion}\`?`
return suggestion
? `did you mean \`${logger.teal(suggestion)}\`? otherwise… see you on GitHub?`
: undefined
}}
}

Expand All @@ -25,7 +27,7 @@ export default async function(err: Error) {
console.error(`${logger.red('error')}: ${err.title()} (${logger.gray(err.code())})`)
if (suggestion) {
console.error()
console.error(logger.gray(suggestion))
console.error(suggestion)
console.error()
}
console.error(msg(err))
Expand Down
30 changes: 17 additions & 13 deletions src/hooks/usePantry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,26 @@ function entry({ project }: { project: string }): Entry {
throw new TeaError('not-found: pantry: package.yml', {project}, )
}

async function getClosestPackageSuggestion(orgPkg: string): Promise<string> {
let closestPkg = ''
let minDistance = Infinity
const pkgList = []
async function getClosestPackageSuggestion(input: string) {
let choice: string | undefined
let min = Infinity
for await (const {project} of ls()) {
pkgList.push(project)
}
for (const pkgName of pkgList) {
if(pkgName.includes(orgPkg)) return pkgName;
const number = levenshteinDistance(pkgName, orgPkg)
if (number<minDistance) {
minDistance = number
closestPkg = pkgName
if (min == 0) break

getProvides({ project }).then(provides => {
if (provides.includes(input)) {
choice = project
min = 0
}
})

const dist = levenshteinDistance(project, input)
if (dist < min) {
min = dist
choice = project
}
}
return closestPkg
return choice
}

function levenshteinDistance (str1: string, str2:string):number{
Expand Down
12 changes: 2 additions & 10 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class TeaError extends Error {
title() {
switch (this.id) {
case 'not-found: pantry: package.yml':
return `not-found in pantry: ${this.ctx.project}/package.yml`
return `not found in pantry: ${this.ctx.project}`
default:
return this.id
}
Expand All @@ -51,8 +51,6 @@ export default class TeaError extends Error {
msg = undent`
couldn’t find a pkg to provide: \`${ctx.arg0}'
you could be the one to add it to \`tea\`:
https://github.com/teaxyz/pantry.zero#contributing
`
Expand Down Expand Up @@ -80,13 +78,7 @@ export default class TeaError extends Error {
msg = ctx.underr?.message ?? "contract violated"
break
case 'not-found: pantry: package.yml':
msg = undent`
no pantry entry for: ${ctx.project}
your time to shine? we’ll see you on GitHub…
https://github.com/teaxyz/pantry.zero#contributing
`
msg = " https://github.com/teaxyz/pantry.zero#contributing\n"
break
case 'parser: pantry: package.yml':
msg = undent`
Expand Down

0 comments on commit e5ae8f7

Please sign in to comment.