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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/_utils/spec.ts
@@ -0,0 +1,7 @@
// eslint-disable-next-line @withfig/fig-linter/no-missing-default-export
export const specToSuggestions = (spec: Fig.Spec): Fig.Suggestion => {
return {
name: spec.name,
...("icon" in spec && { icon: spec.icon }),
};
};
24 changes: 2 additions & 22 deletions src/bunx.ts
@@ -1,31 +1,11 @@
import { npxSuggestions } from "./npx";
import { npxLocalBinsGenerator, npxSuggestions } from "./npx";

const bunx: Fig.Spec = {
name: "bunx",
args: {
name: "command",
isCommand: true,
generators: {
script: [
"bash",
"-c",
"until [[ -d node_modules/ ]] || [[ $PWD = '/' ]]; do cd ..; done; ls -1 node_modules/.bin/`",
],
postProcess: function (out) {
const cli = [...npxSuggestions].reduce(
(acc, { name }) => [...acc, name],
[]
);
return out
.split("\n")
.filter((name) => !cli.includes(name))
.map((name) => ({
name,
icon: "fig://icon?type=command",
loadSpec: name,
}));
},
},
generators: npxLocalBinsGenerator(true),
description: "The command to run",
suggestions: [...npxSuggestions],
},
Expand Down
66 changes: 43 additions & 23 deletions src/npx.ts
@@ -1,7 +1,16 @@
// TODO: When #2138 is merged
// import createRedwoodApp from "./create-redwood-app";
import { specToSuggestions } from "./_utils/spec";
import vite from "./vite";
import createRedwoodApp from "./create-redwood-app";

// TODO: this for all the suggestions that have specs
export const npxSuggestions: Fig.Suggestion[] = [
specToSuggestions(createRedwoodApp),
specToSuggestions(vite),
{
name: "vite",
icon: "https://vitejs.dev/logo.svg",
// TODO: Import from autocannon when it's merged
name: "autocannon",
},
{
name: "babel",
Expand All @@ -11,6 +20,10 @@ export const npxSuggestions: Fig.Suggestion[] = [
name: "create-react-native-app",
icon: "https://reactnative.dev/img/pwa/manifest-icon-512.png",
},
{
name: "create-vite",
icon: "https://vitejs.dev/logo.svg",
},
{
name: "react-native",
icon: "https://reactnative.dev/img/pwa/manifest-icon-512.png",
Expand Down Expand Up @@ -159,33 +172,40 @@ export const npxSuggestions: Fig.Suggestion[] = [
},
];

const binToSpecOverrides = {
rw: "redwood",
};

export const npxLocalBinsGenerator = (
filterOutGlobal = false
): Fig.Generator => ({
script: [
"bash",
"-c",
// TODO: use util to get the first node_modules
`until [[ -d node_modules/ ]] || [[ $PWD = '/' ]]; do cd ..; done; ls -1 node_modules/.bin/`,
],
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,
}));
},
});

const completionSpec: Fig.Spec = {
name: "npx",
description: "Execute binaries from npm packages",
args: {
name: "command",
isCommand: true,
generators: {
script: [
"bash",
"-c",
"until [[ -d node_modules/ ]] || [[ $PWD = '/' ]]; do cd ..; done; ls -1 node_modules/.bin/",
],
postProcess: function (out) {
const cli = [...npxSuggestions].reduce(
(acc, { name }) => [...acc, name],
[]
);
return out
.split("\n")
.filter((name) => !cli.includes(name))
.map((name) => ({
name,
icon: "fig://icon?type=command",
loadSpec: name,
}));
},
},
generators: npxLocalBinsGenerator(true),
suggestions: [...npxSuggestions],
isOptional: true,
},
Expand Down
14 changes: 12 additions & 2 deletions src/pnpm.ts
Expand Up @@ -2,6 +2,7 @@

import { npmScriptsGenerator, npmSearchGenerator } from "./npm";
import { dependenciesGenerator, nodeClis } from "./yarn";
import { npxLocalBinsGenerator } from "./npx";

const filterMessages = (out: string): string => {
return out.startsWith("warning:") || out.startsWith("error:")
Expand Down Expand Up @@ -504,8 +505,8 @@ node_modules/.bin is added to the PATH, so pnpm exec allows executing commands o
args: {
name: "Scripts",
filterStrategy: "fuzzy",
generators: dependenciesGenerator,
isVariadic: true,
// TODO: fix loading + fix loading of rw
generators: npxLocalBinsGenerator(),
},
options: [
{
Expand Down Expand Up @@ -883,6 +884,15 @@ Please note that this is prohibited when a store server is running`,
},
],
},
{
name: "init",
description:
"Creates a basic package.json file in the current directory, if it doesn't exist already",
},
{
name: "doctor",
description: "Checks for known common issues with pnpm configuration",
},
];

const subcommands = [
Expand Down
39 changes: 7 additions & 32 deletions src/pnpx.ts
@@ -1,38 +1,13 @@
import { npxSuggestions } from "./npx";

const completionSpec: Fig.Spec = {
name: "pnpx",
description: "Execute binaries from npm packages",
subcommands: [
{
name: "create-react-native-app",
icon: "https://reactnative.dev/img/pwa/manifest-icon-512.png",
loadSpec: "create-react-native-app",
},
{
name: "react-native",
icon: "https://reactnative.dev/img/pwa/manifest-icon-512.png",
loadSpec: "react-native",
},
{
name: "tailwindcss",
icon: "https://tailwindcss.com/favicon-32x32.png",
loadSpec: "tailwindcss",
},
{
name: "next",
icon: "https://nextjs.org/static/favicon/favicon-16x16.png",
loadSpec: "next",
},
{
name: "gltfjsx",
icon: "https://raw.githubusercontent.com/pmndrs/branding/master/logo.svg",
loadSpec: "gltfjsx",
},
{
name: "prisma",
icon: "https://raw.githubusercontent.com/prisma/docs/main/src/images/favicon-16x16.png",
loadSpec: "prisma",
},
],
args: {
name: "name",
isCommand: true,
suggestions: [...npxSuggestions],
},
options: [
{
name: ["--package", "-p"],
Expand Down
1 change: 1 addition & 0 deletions src/yarn.ts
Expand Up @@ -349,6 +349,7 @@ const commonOptions: Fig.Option[] = [
},
];

// TODO: Extract this to be used with bun|pnpm|npm create
export const createCLIsGenerator: Fig.Generator = {
script: function (context) {
if (context[context.length - 1] === "") return undefined;
Expand Down