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
74 changes: 37 additions & 37 deletions dev/npm.ts
@@ -1,50 +1,41 @@
const searchGenerator: Fig.Generator = {
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;
},
};

export const completionSpec: Fig.Spec = {
name: "npm",
description: "Node package manager",
subcommands: [
{
name: "install",
description: "",
args: [
{
generators: {
script: "cat package.json",
postProcess: function (out) {
if (out.trim() === "") {
return [];
}
try {
const packageConten = JSON.parse(out);
const dependencies = packageConten["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 [];
},
},
},
],
args: {
name: "package",
generators: searchGenerator,
debounce: true,
variadic: true,
},
options: [
{
name: ["-P", "--save-prod"],
description:
"Package will appear in your `dependencies`. This is thedefault unless `-D` or `-O` are present.",
"Package will appear in your `dependencies`. This is the default unless `-D` or `-O` are present.",
},
{
name: ["-D", "--save-dev"],
Expand Down Expand Up @@ -143,7 +134,16 @@ export const completionSpec: Fig.Spec = {
{ name: "help", description: "search npm help documentation" },
{ name: "help-search", description: "get help on npm" },
{ name: "hook", description: "manage registry hooks" },
{ name: "i", description: "install local package" },
{
name: "i",
description: "install local package",
args: {
name: "package",
generators: searchGenerator,
debounce: true,
variadic: true,
},
},
brendanfalk marked this conversation as resolved.
Show resolved Hide resolved
{
name: "install-ci-test",
description: "install a project with a clean slate and run tests",
Expand Down
69 changes: 31 additions & 38 deletions specs/npm.js
@@ -1,50 +1,34 @@
var searchGenerator = {
script: function (context) {
if (context[context.length - 1] === "")
return "";
var 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(function (item) { return item.package.name; });
},
trigger: function () {
return true;
},
};
var completionSpec = {
name: "npm",
description: "Node package manager",
subcommands: [
{
name: "install",
description: "",
args: [
{
generators: {
script: "cat package.json",
postProcess: function (out) {
if (out.trim() === "") {
return [];
}
try {
var packageConten = JSON.parse(out);
var dependencies = packageConten["dependencies"];
if (dependencies) {
var dps = Object.keys(dependencies);
return dps.map(function (pkg) {
var scope = pkg.indexOf("/") + 1;
if (scope !== -1) {
pkg = pkg.substring(scope);
}
var version = pkg.indexOf("@");
if (version !== -1) {
pkg = pkg.substring(version);
}
return {
name: pkg,
icon: "fig://icon?type=file",
description: "dependency file",
};
});
}
}
catch (e) { }
return [];
},
},
},
],
args: {
name: "package",
generators: searchGenerator,
debounce: true,
variadic: true,
},
options: [
{
name: ["-P", "--save-prod"],
description: "Package will appear in your `dependencies`. This is thedefault unless `-D` or `-O` are present.",
description: "Package will appear in your `dependencies`. This is the default unless `-D` or `-O` are present.",
},
{
name: ["-D", "--save-dev"],
Expand Down Expand Up @@ -134,7 +118,16 @@ var completionSpec = {
{ name: "help", description: "search npm help documentation" },
{ name: "help-search", description: "get help on npm" },
{ name: "hook", description: "manage registry hooks" },
{ name: "i", description: "install local package" },
{
name: "i",
description: "install local package",
args: {
name: "package",
generators: searchGenerator,
debounce: true,
variadic: true,
},
},
{
name: "install-ci-test",
description: "install a project with a clean slate and run tests",
Expand Down