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

fix: fix generic type issues #38

Merged
merged 1 commit into from
Jun 18, 2023
Merged
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
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