From 01e637e316dfd064477d06f08cdb03753ae0f7aa Mon Sep 17 00:00:00 2001 From: Bond <4683867+bondz@users.noreply.github.com> Date: Sun, 2 Jun 2024 15:17:19 -0500 Subject: [PATCH 1/5] Update npm to use the cli for package and version queries --- src/npm.ts | 54 +++++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/src/npm.ts b/src/npm.ts index 2ea3128abd52..6b2d2d8df6a9 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -26,34 +26,8 @@ export const createNpmSearchHandler = return []; } // Add optional keyword parameter - const keywordParameter = - keywords?.length > 0 ? `+keywords:${keywords.join(",")}` : ""; + const keywordParameter = keywords?.length > 0 ? keywords.join(" ") : ""; - const queryPackagesUrl = keywordParameter - ? `https://api.npms.io/v2/search?size=20&q=${searchTerm}${keywordParameter}` - : `https://api.npms.io/v2/search/suggestions?q=${searchTerm}&size=20`; - - // Query the API with the package name - const queryPackages = [ - "-s", - "-H", - "Accept: application/json", - queryPackagesUrl, - ]; - // We need to remove the '@' at the end of the searchTerm before querying versions - const queryVersions = [ - "-s", - "-H", - "Accept: application/vnd.npm.install-v1+json", - `https://registry.npmjs.org/${searchTerm.slice(0, -1)}`, - ]; - // If the end of our token is '@', then we want to generate version suggestions - // Otherwise, we want packages - const out = (query: string) => - executeShellCommand({ - command: "curl", - args: query[query.length - 1] === "@" ? queryVersions : queryPackages, - }); // If our token starts with '@', then a 2nd '@' tells us we want // versions. // Otherwise, '@' anywhere else in the string will indicate the same. @@ -62,9 +36,15 @@ export const createNpmSearchHandler = : searchTerm.includes("@"); try { - const data = JSON.parse((await out(searchTerm)).stdout); if (shouldGetVersion) { - // create dist tags suggestions + const out = () => + executeShellCommand({ + command: "npm", + args: ["--json", "info", searchTerm.slice(0, -1)], + }); + + const data = JSON.parse((await out()).stdout); + const versions = Object.entries(data["dist-tags"] || {}).map( ([key, value]) => ({ name: key, @@ -73,17 +53,24 @@ export const createNpmSearchHandler = ) as Fig.Suggestion[]; // create versions versions.push( - ...Object.keys(data.versions) + ...data.versions .map((version) => ({ name: version }) as Fig.Suggestion) .reverse() ); return versions; } - const results = keywordParameter ? data.results : data; + const out = () => + executeShellCommand({ + command: "npm", + args: ["--json", "search", searchTerm, keywordParameter], + }); + + const results = JSON.parse((await out()).stdout); + return results.map((item) => ({ - name: item.package.name, - description: item.package.description, + name: item.name, + description: item.description, })) as Fig.Suggestion[]; } catch (error) { console.error({ error }); @@ -409,6 +396,7 @@ const completionSpec: Fig.Spec = { generators: npmSearchGenerator, debounce: true, isVariadic: true, + filterStrategy: "fuzzy", }, options: [ { From 64d60137a73c1eb6407d425ad295923ff46841e4 Mon Sep 17 00:00:00 2001 From: Bond <4683867+bondz@users.noreply.github.com> Date: Sun, 2 Jun 2024 16:17:50 -0500 Subject: [PATCH 2/5] fix double @ bug --- src/npm.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/npm.ts b/src/npm.ts index 6b2d2d8df6a9..f90c0c9ef0dd 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -93,7 +93,11 @@ export const npmSearchGenerator: Fig.Generator = { // we see the first '@' so we can generate version suggestions return !(oldToken.includes("@") && newToken.includes("@")); }, - getQueryTerm: "@", + getQueryTerm: (token) => { + // If we are looking for versions, there's no need filter the + // search anymore, otherwise use the query the user entered + return token.endsWith("@") ? "" : token; + }, cache: { ttl: 1000 * 60 * 60 * 24 * 2, // 2 days }, From 83e1c79209657856156a0f0d783d56f35e294ff7 Mon Sep 17 00:00:00 2001 From: Bond <4683867+bondz@users.noreply.github.com> Date: Sun, 2 Jun 2024 16:25:39 -0500 Subject: [PATCH 3/5] fuzzy search no longer required --- src/npm.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/npm.ts b/src/npm.ts index f90c0c9ef0dd..fb83766f3f26 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -400,7 +400,6 @@ const completionSpec: Fig.Spec = { generators: npmSearchGenerator, debounce: true, isVariadic: true, - filterStrategy: "fuzzy", }, options: [ { From a52cc1e7522400b7d9dea90b2267769cd9c0075c Mon Sep 17 00:00:00 2001 From: Bond <4683867+bondz@users.noreply.github.com> Date: Sun, 2 Jun 2024 16:52:11 -0500 Subject: [PATCH 4/5] fix for mgnl --- src/npm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/npm.ts b/src/npm.ts index fb83766f3f26..015f4b38bae6 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -63,7 +63,7 @@ export const createNpmSearchHandler = const out = () => executeShellCommand({ command: "npm", - args: ["--json", "search", searchTerm, keywordParameter], + args: ["--json", "search", keywordParameter, searchTerm], }); const results = JSON.parse((await out()).stdout); From fac074f6ec28cd9bcb67821f158c01312af4773a Mon Sep 17 00:00:00 2001 From: Bond <4683867+bondz@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:18:29 +0000 Subject: [PATCH 5/5] Pass linter --- src/npm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/npm.ts b/src/npm.ts index 015f4b38bae6..c1dea1bc9be7 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -94,7 +94,7 @@ export const npmSearchGenerator: Fig.Generator = { return !(oldToken.includes("@") && newToken.includes("@")); }, getQueryTerm: (token) => { - // If we are looking for versions, there's no need filter the + // If we are looking for versions, there's no need filter the // search anymore, otherwise use the query the user entered return token.endsWith("@") ? "" : token; },