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

Add command to list available tasks #3

Merged
merged 15 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "@maastrich/moonx",
"version": "0.1.1",
"version": "0.1.2",
"description": "A CLI tool to help you with moon syntax",
"keywords": [
"moon",
"moonrepo",
"cli",
"proto"
],
"license": "ISC",
"license": "MIT",
"author": "Maastrich @maastrich",
"type": "module",
"main": "src/index.ts",
"bin": {
"moonx": "bin/moonx"
"moonx": "bin/moonx",
"mx": "bin/moonx"
},
"files": [
"src",
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);
maastrich marked this conversation as resolved.
Show resolved Hide resolved
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));
}
61 changes: 33 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { cac } from "cac";
import { spawnSync } from "child_process";

import pkg from "../package.json";

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 @@ -17,39 +21,32 @@ const commands = await scan();

const cli = cac("moonx");

for (const [name, workspaces] of commands) {
cli.option("cache", "");
cli.option("color", "");
cli.option("concurrency", "");
cli.option("c", "");
cli.option("log", "");
cli.option("logFile", "");
cli.option("moon-help", "");
cli.option("moon-version", "");

cli
.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"));
stdout.end();
});

for (const name of commands.keys()) {
cli
.command(`${name} [...workspaces]`, "", { allowUnknownOptions: true })
.usage(`${name} [...workspaces] [options]`)
.action(async (wss: Array<string>, options) => {
const args = ["moon", "run"];
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 Expand Up @@ -80,6 +77,14 @@ cli.help((sections) => {
return [{ body: help.task(workspaces) }];
});

cli.on("command:*", () => {
logger.error(`Invalid command: ${cli.args.join(" ")}`);
cli.outputHelp();
process.exit(1);
});

cli.version(pkg.version);

try {
cli.parse(Bun.argv, { run: false });
await cli.runMatchedCommand();
Expand Down
4 changes: 3 additions & 1 deletion src/utils/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ ${chalk.bold("Moon option:")}
--log <LOG> Lowest log level to output [env: MOON_LOG=] [default: info] [possible values: off, error, warn, info, debug, trace]
--logFile <LOG_FILE> Path to a file to dump the moon logs [env: MOON_LOG_FILE=]
-h, --help Print help
-V, --version Print version
-v, --version Print moonx version
--moon-version Print moon version
--moon-help Print moon help

For more info, run any command with the --help flag
e.g. ${chalk.yellow("moonx <command> --help")}
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