Skip to content

Commit

Permalink
Merge pull request #126 from DannyAziz/npm
Browse files Browse the repository at this point in the history
[NPM]: feat: npm install autosuggests from npms.io
  • Loading branch information
Brendan Falk committed Apr 22, 2021
2 parents 6cec9ed + cddafa5 commit d218378
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 75 deletions.
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 @@ -145,7 +136,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,
},
},
{
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 @@ -137,7 +121,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

0 comments on commit d218378

Please sign in to comment.