withfig / autocomplete

IDE-style autocomplete for your existing terminal & shell
https://fig.io
MIT License
24.38k stars 5.4k forks source link

Update npm to use the cli for package and version queries #2356

Open bondz opened 1 month ago

bondz commented 1 month ago

Fixes #2255

Tested locally with public and scoped packages. Also, fixes an issue where scoped packages would not previously show up because the @ symbol was not considered part of the search.

Demo

withfig-bot commented 1 month ago

Overview

## src/npm.ts: ### Info: ### Single Functions: **custom:** ```typescript async function (tokens, executeShellCommand) { if (!tokens.includes("-g") && !tokens.includes("--global")) { const { stdout: npmPrefix } = await executeShellCommand({ command: "npm", // eslint-disable-next-line @withfig/fig-linter/no-useless-arrays args: ["prefix"], }); const { stdout: out } = await executeShellCommand({ command: "cat", // eslint-disable-next-line @withfig/fig-linter/no-useless-arrays args: [`${npmPrefix}/package.json`], }); const packageContent = JSON.parse(out); const dependencies = packageContent["dependencies"] ?? {}; const devDependencies = packageContent["devDependencies"]; const optionalDependencies = packageContent["optionalDependencies"] ?? {}; Object.assign(dependencies, devDependencies, optionalDependencies); return Object.keys(dependencies) .filter((pkgName) => { const isListed = tokens.some((current) => current === pkgName); return !isListed; }) .map((pkgName) => ({ name: pkgName, icon: "📦", description: dependencies[pkgName] ? "dependency" : optionalDependencies[pkgName] ? "optionalDependency" : "devDependency", })); } else { const { stdout } = await executeShellCommand({ command: "bash", args: ["-c", "ls -1 `npm root -g`"], }); return stdout.split("\n").map((name) => ({ name, icon: "📦", description: "Global dependency", })); } } ``` **postProcess:** ```typescript function (out, [npmClient]) { if (out.trim() == "") { return []; } try { const packageContent = JSON.parse(out); const scripts = packageContent["scripts"]; const figCompletions = packageContent["fig"] || {}; if (scripts) { return Object.entries(scripts).map(([scriptName, scriptContents]) => { const icon = npmClient === "yarn" ? "fig://icon?type=yarn" : "fig://icon?type=npm"; const customScripts: Fig.Suggestion = figCompletions[scriptName]; return { name: scriptName, icon, description: scriptContents as string, priority: 51, /** * If there are custom definitions for the scripts * we want to override the default values * */ ...customScripts, }; }); } } catch (e) { console.error(e); } return []; } ```
withfig-bot commented 1 month ago

Hello @bondz, thank you very much for creating a Pull Request! Here is a small checklist to get this PR merged as quickly as possible:

Please add a 👍 as a reaction to this comment to show that you read this.

bondz commented 1 month ago

This could potentially break mgnl, although it works on my machine™️

I don't have enough information about the tool.

bondz commented 2 weeks ago

@grant0417 @mschrage any changes you'd like me to make?

grant0417 commented 2 weeks ago

I think the changes are good, I just need to stress test them and ensure we dont have any regressions. Will try to do this soon.