Skip to content

Commit

Permalink
Add moonx autocomplete support
Browse files Browse the repository at this point in the history
  • Loading branch information
maastrich committed Nov 12, 2023
1 parent 0d8146b commit 900372c
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 45 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type": "module",
"main": "src/index.ts",
"bin": {
"mx": "bin/moonx",
"moonx": "bin/moonx"
},
"files": [
Expand All @@ -39,6 +40,7 @@
"@trivago/prettier-plugin-sort-imports": "^4.2.1",
"@types/nunjucks": "^3.2.6",
"@types/semver": "^7.5.5",
"@withfig/autocomplete-types": "^1.29.0",
"bun-types": "^1.0.11",
"prettier": "^3.0.3",
"semver": "^7.5.4",
Expand Down
31 changes: 31 additions & 0 deletions scripts/fig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const generator: Fig.Generator = {
script(args) {
console.log(args);
return [
"moonx",
"_moonx_list",
...args.filter(
(arg) => !arg.startsWith("-") && arg.length && "moonx" !== arg,
),
];
},
trigger: "moonx",
postProcess(out) {
console.log("here");
const lines = out.split("\n");
return lines.map((line) => {
return {
name: line,
};
});
},
};

const completionSpec: Fig.Spec = {
name: "moonx",
args: {
isVariadic: true,
generators: generator,
},
};
export default completionSpec;
16 changes: 16 additions & 0 deletions src/cli/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function exec(
command: string,
wss: Array<string>,
commands: Map<string, string[]>,
) {
const wokspaces = commands.get(command);
if (!wokspaces) {
throw new Error(`task ${command} does not exist`);
}
if (!wss.length) {
return [`:${command}`];
}
return wss
.filter((ws) => wokspaces.includes(ws))
.map((ws) => `${ws}:${command}`);
}
17 changes: 17 additions & 0 deletions src/cli/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function list(
[command, ...wss]: Array<string>,
commands: Map<string, string[]>,
) {
if (!commands.has(command) && wss.length) {
console.log(`task ${command} does not exist`);
return [];
}
if (!commands.has(command)) {
return Array.from(commands.keys());
}
const workspaces = commands.get(command)!;
if (!wss.length) {
return workspaces;
}
return wss.filter((ws) => workspaces.includes(ws));
}
56 changes: 12 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cac } from "cac";
import { spawnSync } from "child_process";

import { exec } from "./cli/exec.js";
import { list } from "./cli/list.js";
import { help } from "./utils/help.js";
import { logger } from "./utils/logger.js";
import { scan } from "./utils/scan-moon.js";
Expand All @@ -18,57 +20,23 @@ const commands = await scan();
const cli = cac("moonx");

cli
.command("_moonx_list [...items]", "List all available tasks")
.action(([command, ...wss]) => {
const workspaces = commands.get(command);

if (!command) {
return console.log(Array.from(commands.keys()).join("\n"));
}
if (!workspaces) {
return;
}
if (wss.length > 0) {
return console.log(
workspaces.filter((ws) => !wss.includes(ws)).join("\n"),
);
}
console.log(workspaces.join("\n"));
.command("_moonx_list [...params]", "List all available tasks")
.action((arg) => {
const result = list(arg, commands);
const stdout = Bun.stdout.writer();
stdout.write(result.join("\n"));
});

for (const [name, workspaces] of commands) {
for (const name in commands) {
cli
.command(`${name} [...workspaces]`, "", { allowUnknownOptions: true })
.usage(`${name} [...workspaces] [options]`)
.usage(`${name} [...workspaces] [MOONX_OPTIONS] -- [OPTIONS]`)
.action(async (wss: Array<string>, options) => {
const args = ["moon", "run"];
console.log(wss);
const workspaces = exec(name, wss, commands);
const rest = ["--", ...options["--"]];
if (wss.length === 0) {
return Bun.spawnSync({
cmd: [args, `:${name}`, rest].flat(),
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
onExit(_, exitCode) {
if (exitCode) {
logger.error(`task ${name} failed`);
process.exit(exitCode);
}
},
});
}
wss = wss.filter((ws) => {
if (!workspaces.includes(ws)) {
logger.warn(`task ${name} does not exist on workspace ${ws}`);
return false;
}
return true;
});
if (wss.length === 0) {
return;
}
return Bun.spawnSync({
cmd: [args, wss.map((ws) => `${name}:${ws}`), rest].flat(),
cmd: ["moon", "run", workspaces, rest].flat(),
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target": "ESNext",
"module": "NodeNext",
"skipLibCheck": true,
"types": ["bun-types"],
"types": ["bun-types", "@withfig/autocomplete-types"],
"resolveJsonModule": true,
"moduleResolution": "NodeNext"
},
Expand Down

0 comments on commit 900372c

Please sign in to comment.