Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineBuildConfig } from "obuild/config";

export default defineBuildConfig({
entries: ["src/index.ts"],
entries: ["src/index.ts", "src/tab.ts"],
});
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"sideEffects": false,
"type": "module",
"exports": {
".": "./dist/index.mjs"
".": "./dist/index.mjs",
"./tab": "./dist/tab.mjs"
},
"types": "./dist/index.d.mts",
"files": [
Expand All @@ -28,6 +29,7 @@
"consola": "^3.4.2"
},
"devDependencies": {
"@bomb.sh/tab": "^0.0.11",
"@types/node": "^25.0.9",
"@vitest/coverage-v8": "^4.0.17",
"automd": "^0.4.2",
Expand All @@ -40,5 +42,13 @@
"typescript": "^5.9.3",
"vitest": "^4.0.17"
},
"peerDependencies": {
"@bomb.sh/tab": "*"
},
"peerDependenciesMeta": {
"@bomb.sh/tab": {
"optional": true
}
},
"packageManager": "pnpm@10.28.1"
}
3 changes: 3 additions & 0 deletions playground/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineCommand, runMain } from "../src/index.ts";
import { registerTabCompletions } from "../src/tab.ts";

const main = defineCommand({
meta: {
Expand All @@ -19,4 +20,6 @@ const main = defineCommand({
},
});

await registerTabCompletions(main);

runMain(main);
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import consola from "consola";
import type { ArgsDef, CommandDef } from "./types.ts";
import { resolveSubCommand, runCommand } from "./command.ts";
import { CLIError } from "./_utils.ts";
import { showUsage as _showUsage } from "./usage.ts";

import type { ArgsDef, CommandDef } from "./types.ts";

export interface RunMainOptions {
rawArgs?: string[];
showUsage?: typeof _showUsage;
Expand All @@ -15,6 +16,7 @@ export async function runMain<T extends ArgsDef = ArgsDef>(
) {
const rawArgs = opts.rawArgs || process.argv.slice(2);
const showUsage = opts.showUsage || _showUsage;

try {
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
await showUsage(...(await resolveSubCommand(cmd, rawArgs)));
Expand Down
83 changes: 83 additions & 0 deletions src/tab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import tab from "@bomb.sh/tab";
import { resolveValue } from "./_utils.ts";

import type { ArgsDef, CommandDef, ArgDef } from "./types.ts";
import type { Command, RootCommand } from "@bomb.sh/tab";

export type TabCommand = Command | RootCommand;

export async function registerTabCompletions(
cmd: CommandDef<ArgsDef>,
): Promise<void> {
await registerCommand(cmd, tab);

const meta = await resolveValue(cmd.meta);
const name = meta?.name || "cli";
const subCommands = await resolveValue(cmd.subCommands);

if (subCommands && !subCommands.complete) {
subCommands.complete = {
meta: {
name: "complete",
description: "Generate shell completion scripts",
},
args: {
shell: {
type: "positional",
description: "Shell type (zsh, bash, fish, powershell)",
required: false,
},
},
run({ rawArgs }) {
const shell = rawArgs[0];
if (shell === "--") {
tab.parse(rawArgs.slice(1));
} else if (shell) {
const exec = `${process.execPath} ${process.argv[1]}`;
tab.setup(name, exec, shell);
}
},
} satisfies CommandDef<ArgsDef>;
}
}

async function registerCommand(
cmd: CommandDef<ArgsDef>,
tabCmd: TabCommand,
path = "",
): Promise<void> {
const args = await resolveValue(cmd.args);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue is that this eagerly loads all sub-commands, defining the point of lazy-loading commands.

Could tab somehow accept a lazy evaluated meta resolver and call it when needed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Salam @pi0!
Thanks for reviewing this.
I will have to experiment and see if I can add that as an option 👀

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mer30! Feel free to DM in Discord if wanted to brainstorm!

if (args) {
for (const name in args) {
const arg = args[name] as ArgDef;
const handler = arg.complete
? (complete: (value: string, description: string) => void) =>
arg.complete!(complete)
: undefined;

if (arg.type === "positional") {
tabCmd.argument(name, handler, arg.required === false);
} else {
const alias =
"alias" in arg && arg.alias
? // eslint-disable-next-line unicorn/no-nested-ternary
typeof arg.alias === "string"
? arg.alias
: arg.alias[0]
: undefined;
tabCmd.option(name, arg.description || "", handler || alias, alias);
}
}
}

const subCommands = await resolveValue(cmd.subCommands);
if (!subCommands) return;
for (const name in subCommands) {
const sub = await resolveValue(subCommands[name]);
if (!sub) continue;
const meta = await resolveValue(sub.meta);
const subPath = path ? `${path} ${name}` : name;
const subTab = tab.command(subPath, meta?.description || "");
await registerCommand(sub, subTab, subPath);
}
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ export type ArgType = "boolean" | "string" | "enum" | "positional" | undefined;

// Args: Definition

export type CompletionHandler = (
complete: (value: string, description: string) => void,
) => void | Promise<void>;

export type _ArgDef<T extends ArgType, VT extends boolean | number | string> = {
type?: T;
description?: string;
valueHint?: string;
alias?: string | string[];
default?: VT;
required?: boolean;
complete?: CompletionHandler;
options?: string[];
};

Expand Down