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

feat: command aliases #170

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions playground/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { defineCommand } from "../../src";
export default defineCommand({
meta: {
name: "build",
aliases: ["b"],
description: "Build the project from current directory",
},
args: {
Expand Down
36 changes: 32 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { CommandContext, CommandDef, ArgsDef } from "./types";
import type {
CommandContext,
CommandDef,
ArgsDef,
SubCommandsDef,
} from "./types";
import { CLIError, resolveValue } from "./_utils";
import { parseArgs } from "./args";

Expand All @@ -14,6 +19,27 @@ export interface RunCommandOptions {
showUsage?: boolean;
}

async function getSubCommand(
subCommandsList: SubCommandsDef,
subCommandName: string,
): Promise<CommandDef<any> | undefined> {
const subCommand = await resolveValue(subCommandsList[subCommandName]);

if (subCommand) {
return subCommand;
}

for (const _subCmd of Object.values(subCommandsList)) {
const subCmd = await resolveValue(_subCmd);
const subCmdMeta = await resolveValue(subCmd.meta);
if ((subCmdMeta?.aliases || []).includes(subCommandName)) {
return subCmd;
}
}

return undefined;
}

export async function runCommand<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
opts: RunCommandOptions,
Expand Down Expand Up @@ -43,13 +69,14 @@ export async function runCommand<T extends ArgsDef = ArgsDef>(
);
const subCommandName = opts.rawArgs[subCommandArgIndex];
if (subCommandName) {
if (!subCommands[subCommandName]) {
const subCommand = await getSubCommand(subCommands, subCommandName);

if (!subCommand) {
throw new CLIError(
`Unknown command \`${subCommandName}\``,
"E_UNKNOWN_COMMAND",
);
}
const subCommand = await resolveValue(subCommands[subCommandName]);
if (subCommand) {
await runCommand(subCommand, {
rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1),
Expand Down Expand Up @@ -81,7 +108,8 @@ export async function resolveSubCommand<T extends ArgsDef = ArgsDef>(
if (subCommands && Object.keys(subCommands).length > 0) {
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
const subCommandName = rawArgs[subCommandArgIndex];
const subCommand = await resolveValue(subCommands[subCommandName]);
const subCommand = await getSubCommand(subCommands, subCommandName);

if (subCommand) {
return resolveSubCommand(
subCommand,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type ParsedArgs<T extends ArgsDef = ArgsDef> = { _: string[] } & Record<

export interface CommandMeta {
name?: string;
aliases?: string[];
version?: string;
description?: string;
hidden?: boolean;
Expand Down
5 changes: 4 additions & 1 deletion src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
if (meta?.hidden) {
continue;
}
commandsLines.push([`\`${name}\``, meta?.description || ""]);
commandsLines.push([
`\`${[name, ...(meta?.aliases || [])].join(", ")}\``,
meta?.description || "",
]);
commandNames.push(name);
}
usageLine.push(commandNames.join("|"));
Expand Down