Skip to content

Commit

Permalink
[npm | pnpm | yarn] Improve specs (withfig#606)
Browse files Browse the repository at this point in the history
* pnpm related changes

* [npm] add `add` alias

* [pnpm] improve spec

* [npm | pnpm | yarn] reuse generators

* [pnpm] fix: display install package options only if arg is present
  • Loading branch information
zardoy authored Sep 29, 2021
1 parent 6661d5b commit 5cfc53a
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 428 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ npm-debug.log
specs

# we don't have any dependencies which are bundled with
# the specs so we can safely ignore the package-lock/yarn.lock
# the specs so we can safely ignore lockfiles
# to make everyones life easier
package-lock.json
yarn.lock
pnpm-lock.yaml
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"@types/progress": "^2.0.3",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"@withfig/autocomplete-tools": "^1.0.0",
"@withfig/eslint-plugin-fig-linter": "^1.0.0",
"chalk": "^4.1.0",
"chokidar": "^3.5.1",
"danger": "^10.6.4",
Expand All @@ -87,8 +89,6 @@
"pretty-quick": "^3.1.0",
"progress": "^2.0.3",
"ts-node": "^9.1.1",
"typescript": "^4.2.3",
"@withfig/eslint-plugin-fig-linter": "^1.0.0",
"@withfig/autocomplete-tools": "^1.0.0"
"typescript": "^4.2.3"
}
}
96 changes: 56 additions & 40 deletions src/npm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const searchGenerator: Fig.Generator = {
// GENERATORS
export const npmSearchGenerator: 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=20"`;
},
cache: {
ttl: 100 * 24 * 60 * 60 * 3, // 3 days
},
postProcess: function (out) {
try {
return JSON.parse(out).results.map(
Expand Down Expand Up @@ -47,13 +51,15 @@ const workspaceGenerator: Fig.Generator = {
},
};

const dependenciesGenerator: Fig.Generator = {
/** Generator that lists package.json dependencies */
export const dependenciesGenerator: Fig.Generator = {
script:
"until [[ -f package.json ]] || [[ $PWD = '/' ]]; do cd ..; done; cat package.json",
postProcess: function (out, context) {
postProcess: function (out, context = []) {
if (out.trim() === "") {
return [];
}

try {
const packageContent = JSON.parse(out);
const dependencies = packageContent["dependencies"] ?? {};
Expand All @@ -68,18 +74,61 @@ const dependenciesGenerator: Fig.Generator = {
})
.map((pkgName) => ({
name: pkgName,
icon: "📦",
description: dependencies[pkgName]
? "dependency"
: optionalDependencies[pkgName]
? "optionalDependency"
: "devDependency",
}));
} catch {
} catch (e) {
console.error(e);
return [];
}
},
};

/** Generator that lists package.json scripts (with the respect to the `fig` field) */
export const npmScriptsGenerator: Fig.Generator = {
script:
"until [[ -f package.json ]] || [[ $PWD = '/' ]]; do cd ..; done; cat package.json",
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,
/**
* If there are custom definitions for the scripts
* we want to overide the default values
* */
...customScripts,
};
});
}
} catch (e) {
console.error(e);
}

return [];
},
};

const workSpaceOptions: Fig.Option[] = [
{
name: "-wl--workspace",
Expand Down Expand Up @@ -182,7 +231,7 @@ const completionSpec: Fig.Spec = {
args: {
name: "package",
isOptional: true,
generators: searchGenerator,
generators: npmSearchGenerator,
debounce: true,
isVariadic: true,
},
Expand Down Expand Up @@ -305,40 +354,7 @@ const completionSpec: Fig.Spec = {
},
],
args: {
generators: {
script:
"until [[ -f package.json ]] || [[ $PWD = '/' ]]; do cd ..; done; cat package.json",
// splitOn: "\n",
postProcess: function (out) {
if (out.trim() == "") {
return [];
}

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

if (scripts) {
const keys = Object.keys(scripts).map((key) => {
var val = scripts[key] || "";
return Object.assign(
{},
{ icon: "fig://icon?type=npm" },
{ description: typeof val === "string" ? val : "" },
(figCompletions || {})[key], // need the || {} otherwise it errors
{ name: key, insertValue: key }
); // ensure that name and insertValue are defined by "scripts" dict
});
return keys;
}
} catch (e) {
console.error(e);
}

return [];
},
},
generators: npmScriptsGenerator,
},
},
{
Expand Down Expand Up @@ -640,7 +656,7 @@ const completionSpec: Fig.Spec = {
args: {
name: "package",
isOptional: true,
generators: searchGenerator,
generators: npmSearchGenerator,
debounce: true,
},
},
Expand Down
Loading

0 comments on commit 5cfc53a

Please sign in to comment.