Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

bondz
Copy link
Contributor

@bondz bondz commented Jun 2, 2024

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
Copy link
Collaborator

withfig-bot commented Jun 2, 2024

Overview

src/npm.ts:

Info:

Single Functions:

custom:

 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:

 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
Copy link
Collaborator

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:

  • Do all subcommands / options which take arguments include the args property (args: {})?
  • Are all options modular? E.g. -a -u -x instead of -aux
  • Have all other checks passed?

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

@bondz bondz marked this pull request as ready for review June 2, 2024 21:20
@bondz bondz marked this pull request as draft June 2, 2024 21:38
@bondz
Copy link
Contributor Author

bondz commented Jun 2, 2024

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

I don't have enough information about the tool.

@bondz bondz marked this pull request as ready for review June 2, 2024 21:53
@bondz
Copy link
Contributor Author

bondz commented Jun 18, 2024

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

@grant0417
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[npm] support private registry
3 participants