From e5ae8f77b49cc8152940b38d64c202aafce4a5c6 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Mon, 12 Dec 2022 16:06:27 -0500 Subject: [PATCH] tidy this error; tidy the code --- src/app.err-handler.ts | 6 ++++-- src/hooks/usePantry.ts | 30 +++++++++++++++++------------- src/utils/error.ts | 12 ++---------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/app.err-handler.ts b/src/app.err-handler.ts index 6c006dbc..73d0ffc3 100644 --- a/src/app.err-handler.ts +++ b/src/app.err-handler.ts @@ -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 }} } @@ -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)) diff --git a/src/hooks/usePantry.ts b/src/hooks/usePantry.ts index cec2a4c7..c8a84cd5 100644 --- a/src/hooks/usePantry.ts +++ b/src/hooks/usePantry.ts @@ -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 { - 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 { + 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{ diff --git a/src/utils/error.ts b/src/utils/error.ts index c8942145..9c8ef400 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -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 } @@ -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 ` @@ -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`