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

Merge npx binary running across tools #2139

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Zeko369
Copy link
Contributor

@Zeko369 Zeko369 commented Oct 19, 2023

TODO:

  • Extract create-PACKAGE loginc from yarn
  • Make sure redwood and similar aliases work
  • Maybe port to deno? (since it technically supports it?)
  • Convert npxSuggestions array in npx.ts to load more specs (instead of hardcoding name and icon)

@withfig-bot
Copy link
Collaborator

withfig-bot commented Oct 19, 2023

Overview

src/pnpx.ts:

Info:

src/bunx.ts:

Info:

src/npx.ts:

Info:

Single Functions:

postProcess:

 function (out) {
    const globalCLIs = npxSuggestions.map((suggestion) => suggestion.name);

    return out
      .split("\n")
      .filter((name) => (filterOutGlobal ? !globalCLIs.includes(name) : true))
      .map((name) => ({
        name,
        icon: "fig://icon?type=command",
        loadSpec: binToSpecOverrides[name] || name,
      }));
  }

URLs:

  • https://raw.githubusercontent.com/babel/logo/master/babel.png
  • https://reactnative.dev/img/pwa/manifest-icon-512.png
  • https://vitejs.dev/logo.svg
  • https://reactnative.dev/img/pwa/manifest-icon-512.png
  • https://tailwindcss.com/favicons/favicon-32x32.png
  • https://nextjs.org/static/favicon/favicon-16x16.png
  • https://raw.githubusercontent.com/nuxt/framework/main/docs/public/icon.png
  • https://raw.githubusercontent.com/pmndrs/branding/master/logo.svg
  • https://raw.githubusercontent.com/prisma/docs/main/src/images/favicon-16x16.png
  • https://raw.githubusercontent.com/eslint/eslint.org/main/src/static/icon-512.png
  • https://prettier.io/icon.png
  • https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Typescript_logo_2020.svg/240px-Typescript_logo_2020.svg.png
  • https://avatars.githubusercontent.com/u/20165699?s=200&v=4
  • https://fig.io/icons/fig-light.png
  • https://fig.io/icons/fig-light.png
  • https://fig.io/icons/fig-light.png
  • https://fig.io/icons/fig-light.png
  • https://nextjs.org/static/favicon/favicon-16x16.png
  • https://create.t3.gg/favicon.svg
  • https://discordjs.dev/favicon-32x32.png
  • https://raw.githubusercontent.com/remotion-dev/remotion/main/packages/docs/static/img/logo-small.png
  • https://raw.githubusercontent.com/remotion-dev/remotion/main/packages/docs/static/img/logo-small.png
  • https://remix.run/favicon-light.1.png
  • https://remix.run/favicon-light.1.png
  • https://playwright.dev/img/playwright-logo.svg
  • https://raw.githubusercontent.com/preset/preset/main/.github/assets/logo.svg
  • https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/favicon.ico
  • https://capacitorjs.com/docs/img/meta/favicon.png
  • https://capacitorjs.com/docs/img/meta/favicon.png
  • https://avatars.githubusercontent.com/u/25686615?s=200&v=4
  • https://stenciljs.com/assets/icon/favicon.ico
  • https://static1.smartbear.co/swagger/media/assets/swagger_fav.png
  • https://static1.smartbear.co/swagger/media/assets/swagger_fav.png
  • https://s1.wp.com/i/webclip.png
  • https://astro.build/favicon.svg

src/_utils/spec.ts:

Info:

src/pnpm.ts:

Info:

Single Functions:

postProcess:

 function (out) {
    const output = filterMessages(out);

    if (output.startsWith("fatal:")) {
      return [];
    }

    return output.split("\n").map((elm) => {
      let name = elm.trim();
      const parts = elm.match(/\S+/g);
      if (parts.length > 1) {
        if (parts[0] == "*") {
          // Current branch.
          return {
            name: elm.replace("*", "").trim(),
            description: "Current branch",
            icon: "⭐️",
          };
        } else if (parts[0] == "+") {
          // Branch checked out in another worktree.
          name = elm.replace("+", "").trim();
        }
      }

      return {
        name,
        description: "Branch",
        icon: "fig://icon?type=git",
      };
    });
  }

postProcess:

 function (out) {
    /**
     * out
     * @example
     * ```
     * Legend: production dependency, optional only, dev only
     *
     * /xxxx/xxxx/<package-name> (PRIVATE)
     *
     * dependencies:
     * lodash 4.17.21
     * foo link:packages/foo
     *
     * devDependencies:
     * typescript 4.7.4
     * ```
     */
    if (out.includes("ERR_PNPM")) {
      return [];
    }

    const output = out
      .split("\n")
      .slice(3)
      // remove empty lines, "*dependencies:" lines, local workspace packages (eg: "foo":"workspace:*")
      .filter(
        (item) =>
          !!item &&
          !item.toLowerCase().includes("dependencies") &&
          !item.includes("link:")
      )
      .map((item) => item.replace(/\s/, "@")); // typescript 4.7.4 -> [email protected]

    return output.map((pkg) => {
      return {
        name: pkg,
        icon: "fig://icon?type=package",
      };
    });
  }

src/yarn.ts:

Info:

Single Functions:

postProcess:

 function (out) {
    if (out.trim() == "") {
      return [];
    }

    try {
      const startIndex = out.indexOf("{");
      const endIndex = out.indexOf("}");
      let output = out.substring(startIndex, endIndex + 1);
      // TODO: fix hacky code
      // reason: JSON parse was not working without double quotes
      output = output
        .replace(/\'/gi, '"')
        .replace("lastUpdateCheck", '"lastUpdateCheck"')
        .replace("registry", '"lastUpdateCheck"');
      const configObject = JSON.parse(output);
      if (configObject) {
        return Object.keys(configObject).map((key) => ({ name: key }));
      }
    } catch (e) {}

    return [];
  }

postProcess:

 function (out, context = []) {
    if (out.trim() === "") {
      return [];
    }

    try {
      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 = context.some((current) => current === pkgName);
          return !isListed;
        })
        .map((pkgName) => ({
          name: pkgName,
          icon: "📦",
          description: dependencies[pkgName]
            ? "dependency"
            : optionalDependencies[pkgName]
              ? "optionalDependency"
              : "devDependency",
        }));
    } catch (e) {
      console.error(e);
      return [];
    }
  }

script:

 function (context) {
    if (context[context.length - 1] === "") return undefined;
    const searchTerm = "create-" + context[context.length - 1];
    return [
      "curl",
      "-s",
      "-H",
      "Accept: application/json",
      `https://api.npms.io/v2/search?q=${searchTerm}&size=20`,
    ];
  }

postProcess:

 function (out) {
    try {
      return JSON.parse(out).results.map(
        (item) =>
          ({
            name: item.package.name.substring(7),
            description: item.package.description,
          }) as Fig.Suggestion
      ) as Fig.Suggestion[];
    } catch (e) {
      return [];
    }
  }

postProcess:

 function (out: string) {
                    if (out.trim() == "") {
                      return [];
                    }
                    try {
                      const packageContent = JSON.parse(out);
                      const scripts = packageContent["scripts"];
                      if (scripts) {
                        return Object.keys(scripts).map((script) => ({
                          name: script,
                        }));
                      }
                    } catch (e) {}
                    return [];
                  }

URLs:

  • https://api.npms.io/v2/search?q=

@withfig-bot
Copy link
Collaborator

Hello @Zeko369,
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.

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.

None yet

2 participants