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

[NPM]: feat: npm install autosuggests from npms.io #126

Merged
merged 14 commits into from Apr 22, 2021

Conversation

DannyAziz
Copy link
Contributor

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature
What is the current behavior? (You can also link to an open issue here)
Npm install returns packages that are already in the package.json
What is the new behavior (if this is a feature change)?
Usesnpm search to search as you are typing
Additional info:
Screen Recording 2021-04-14 at 21 48 38

@QuiiBz
Copy link
Contributor

QuiiBz commented Apr 15, 2021

Really cool idea! I don't know why but it's a lot slower for me, the npm search command takes ages. Any idea why?

dev/npm.ts Outdated Show resolved Hide resolved
@cstrnt
Copy link
Contributor

cstrnt commented Apr 15, 2021

Hey @DannyAziz you can use npm search --json which makes the string splitting stuff much easier, because you can just parse the json and look for thename prop :) You could also extract the description property as well and display it for the suggestion :)

@DannyAziz
Copy link
Contributor Author

Hey @cstrnt --json seems to fail for me every time I use it with an error:

npm ERR! cb() never called!

npm ERR! This is an error with npm itself. Please report this error at:
npm ERR!     <https://github.com/npm/cli/issues>

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/danny/.npm/_logs/2021-04-15T10_15_41_562Z-debug.log

@cstrnt
Copy link
Contributor

cstrnt commented Apr 15, 2021

@DannyAziz what npm version do you use? (npm -v) ?

@QuiiBz
Copy link
Contributor

QuiiBz commented Apr 15, 2021

Having the same error with Node v15.9.0 and NPM v7.5.3

@cstrnt
Copy link
Contributor

cstrnt commented Apr 15, 2021

npm/cli#2845
It got fixed in version 7.6.2 and works with npm ~6.x

@brendanfalk
Copy link
Member

Hey guys - so this generator is good, however, it would be best to switch over to running a curl request in our script hitting this API: https://api-docs.npms.io/.

Why?
We want generators that are 1. fast and 2. work on everyone's device. We can't assume that an end user has a specific version of a CLI installed (at least not until we version specs). Therefore, we always need a solution that works for everyone.

In general, I'd agree with Tim that using --json is nicer for parsing, however as it's not supported everywhere, we shouldn't do it.

https://api-docs.npms.io/ is fast and works for everyone and so we should switch to it. Not gonna merge this PR until that's done

@DannyAziz
Copy link
Contributor Author

@falky97 Bump

@brendanfalk
Copy link
Member

Code looks good, either @mattschrage or I will test later today / tomorrow
Two concerns:

  1. npm install should ideally rank commands in the local package.json higher up should it not? I guess this is annoying to mix in a slow generator with a fast local json parse of the package.json... But we should think a little more about how to do this
  2. Why is the check PR / danger test not working? (haven't had a chance to look into it)

@mschrage
Copy link
Member

  1. Does anyone really install local packages individually? Seems like the usual workflow is running npm install in this case. Totally could be wrong.

  2. The danger.js workflow failing is not an issue with this PR. We just need to add a Github token for danger to use.

@brendanfalk
Copy link
Member

Yeah @matt probably fair re npm local installs. The only times I have done it for local files was when I was updating the version number. Main workflow is installing foreign packages

@cstrnt cstrnt changed the title feat: (npm) npm install autosuggests from npm search [NPM]: feat: npm install autosuggests from npm search Apr 16, 2021
@cstrnt cstrnt changed the title [NPM]: feat: npm install autosuggests from npm search [NPM]: feat: npm install autosuggests from npms.io Apr 16, 2021
@cstrnt cstrnt closed this Apr 16, 2021
@cstrnt cstrnt reopened this Apr 16, 2021
@withfig-bot
Copy link
Collaborator

withfig-bot commented Apr 16, 2021

Overview

dev/npm.ts:

Info:

Script:
until [[ -f package.json ]] || [[ $PWD = '/' ]]; do cd ..; done; cat package.json
postProcess(function):

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

              try {
                const packageContent = JSON.parse(out);
                const scripts = packageContent["scripts"];
                const figCompletions = packageContent["fig"];

                if (scripts) {
                  const keys = Object.keys(scripts).map((key) => {
                    return Object.assign(
                      {},
                      { icon: "fig://icon?type=npm" },
                      figCompletions[key],
                      { name: key, insertValue: key }
                    ); // ensure that name and insertValue are defined by "scripts" dict
                  });
                  return keys;
                }
              } catch (e) {}

              return [];
            }

Script:
cat package.json
postProcess(function):

 function (out) {
              if (out.trim() === "") {
                return [];
              }
              try {
                const packageContent = JSON.parse(out);
                const dependencies = packageContent["dependencies"];
                if (dependencies) {
                  const dps = Object.keys(dependencies);
                  return dps.map((pkg) => {
                    const scope = pkg.indexOf("/") + 1;
                    if (scope !== -1) {
                      pkg = pkg.substring(scope);
                    }
                    const version = pkg.indexOf("@");
                    if (version !== -1) {
                      pkg = pkg.substring(version);
                    }
                    return {
                      name: pkg,
                      icon: `fig://icon?type=file`,
                      description: "dependency file",
                    };
                  });
                }
              } catch (e) {}
              return [];
            }

Script:
cat package.json
postProcess(function):

 function (out) {
              if (out.trim() === "") {
                return [];
              }
              try {
                const packageContent = JSON.parse(out);
                const dependencies = packageContent["dependencies"];
                if (dependencies) {
                  const dps = Object.keys(dependencies);
                  return dps.map((pkg) => {
                    const scope = pkg.indexOf("/") + 1;
                    if (scope !== -1) {
                      pkg = pkg.substring(scope);
                    }
                    const version = pkg.indexOf("@");
                    if (version !== -1) {
                      pkg = pkg.substring(version);
                    }
                    return {
                      name: pkg,
                      icon: `fig://icon?type=file`,
                      description: "dependency file",
                    };
                  });
                }
              } catch (e) {}
              return [];
            }

Single Functions:

script:

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

postProcess:

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

trigger:

 function () {
    return true;
  }

URLs:

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

@withfig-bot
Copy link
Collaborator

Hello @DannyAziz,
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 have the arg property (arg: {})?
  • Are all options modular? E.g. a -u -x instead of -aux
  • Did you run npm run build?
  • Have all other checks passed?

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

dev/npm.ts Outdated Show resolved Hide resolved
dev/npm.ts Show resolved Hide resolved
Co-authored-by: Tim Raderschad <tim.raderschad@gmail.com>
@brendanfalk brendanfalk merged commit d218378 into withfig:master Apr 22, 2021
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

6 participants