Skip to content

Commit

Permalink
Add command to list available tasks (#3)
Browse files Browse the repository at this point in the history
* Add command to list available tasks

* Update license from ISC to MIT

* Add moonx autocomplete support

* Reorder bin commands in package.json

* Update version number in package.json

* Add stdout.end() to fix CLI output

* Remove unnecessary usage of command in index.ts

* Add error handling for invalid commands in CLI

* Fix for-in loop in index.ts

* Fix for-of loop in CLI commands iteration

* Remove console.log statement in index.ts

* Add options and version to CLI

* Remove console logs and update Fig.Generator in
fig.ts

* Fix workspace filtering in list function

* Remove pnpm setup from check.yml workflow
  • Loading branch information
maastrich authored Nov 16, 2024
1 parent c03edaa commit 24c7661
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 36 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
- uses: pnpm/action-setup@v2
with:
version: 8
- run: bun install
- run: bun run prettier
- run: bun run build
Expand Down
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
29 changes: 29 additions & 0 deletions scripts/fig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const generator: Fig.Generator = {
script(args) {
return [
"moonx",
"_moonx_list",
...args.filter(
(arg) => !arg.startsWith("-") && arg.length && "moonx" !== arg,
),
];
},
trigger: "moonx",
postProcess(out) {
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 workspaces.filter((ws) => !wss.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

0 comments on commit 24c7661

Please sign in to comment.