Skip to content

Commit

Permalink
fix: fix generic type issues (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
yidingww authored Jun 18, 2023
1 parent d181698 commit 1b57ba3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { parseRawArgs } from "./_parser";
import type { Arg, ArgsDef, ParsedArgs } from "./types";
import { CLIError, toArray } from "./_utils";

export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs {
export function parseArgs<T extends ArgsDef = ArgsDef>(
rawArgs: string[],
argsDef: ArgsDef
): ParsedArgs<T> {
const parseOptions = {
boolean: [] as string[],
string: [] as string[],
Expand Down Expand Up @@ -58,7 +61,7 @@ export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs {
}
}

return parsedArgsProxy;
return parsedArgsProxy as ParsedArgs<T>;
}

export function resolveArgs(argsDef: ArgsDef): Arg[] {
Expand Down
16 changes: 8 additions & 8 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export interface RunCommandOptions {
showUsage?: boolean;
}

export async function runCommand(
cmd: CommandDef,
export async function runCommand<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
opts: RunCommandOptions
): Promise<void> {
const cmdArgs = await resolveValue(cmd.args || {});
const parsedArgs = parseArgs(opts.rawArgs, cmdArgs);
const parsedArgs = parseArgs<T>(opts.rawArgs, cmdArgs);

const context: CommandContext = {
const context: CommandContext<T> = {
rawArgs: opts.rawArgs,
args: parsedArgs,
cmd,
Expand Down Expand Up @@ -64,11 +64,11 @@ export async function runCommand(
}
}

export async function resolveSubCommand(
cmd: CommandDef,
export async function resolveSubCommand<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
rawArgs: string[],
parent?: CommandDef
): Promise<[CommandDef, CommandDef?]> {
parent?: CommandDef<T>
): Promise<[CommandDef<T>, CommandDef<T>?]> {
const subCommands = await resolveValue(cmd.subCommands);
if (subCommands && Object.keys(subCommands).length > 0) {
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
Expand Down
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bgRed } from "colorette";
import type { CommandDef } from "./types";
import type { ArgsDef, CommandDef } from "./types";
import { resolveSubCommand, runCommand } from "./command";
import { CLIError } from "./_utils";
import { showUsage } from "./usage";
Expand All @@ -8,7 +8,10 @@ export interface RunMainOptions {
rawArgs?: string[];
}

export async function runMain(cmd: CommandDef, opts: RunMainOptions = {}) {
export async function runMain<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
opts: RunMainOptions = {}
) {
const rawArgs = opts.rawArgs || process.argv.slice(2);
try {
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type CommandDef<T extends ArgsDef = ArgsDef> = {
export type CommandContext<T extends ArgsDef = ArgsDef> = {
rawArgs: string[];
args: ParsedArgs<T>;
cmd: CommandDef;
cmd: CommandDef<T>;
subCommand?: CommandDef<T>;
};

Expand Down
12 changes: 9 additions & 3 deletions src/usage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { formatLineColumns, resolveValue } from "./_utils";
import type { CommandDef } from "./types";
import type { ArgsDef, CommandDef } from "./types";
import { resolveArgs } from "./args";

export async function showUsage(cmd: CommandDef, parent?: CommandDef) {
export async function showUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>
) {
try {
console.log((await renderUsage(cmd, parent)) + "\n");
} catch (error) {
console.error(error);
}
}

export async function renderUsage(cmd: CommandDef, parent?: CommandDef) {
export async function renderUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>
) {
const cmdMeta = await resolveValue(cmd.meta || {});
const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
const parentMeta = await resolveValue(parent?.meta || {});
Expand Down

0 comments on commit 1b57ba3

Please sign in to comment.