From f40376f9202c07c839147ae622f57d99c95a833e Mon Sep 17 00:00:00 2001 From: Wang Yiding Date: Tue, 30 May 2023 03:48:29 +0000 Subject: [PATCH] fix: ts error --- src/args.ts | 7 +++++-- src/command.ts | 16 ++++++++-------- src/main.ts | 7 +++++-- src/types.ts | 2 +- src/usage.ts | 12 +++++++++--- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/args.ts b/src/args.ts index 3ad11e3..d0de40a 100644 --- a/src/args.ts +++ b/src/args.ts @@ -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( + rawArgs: string[], + argsDef: ArgsDef +): ParsedArgs { const parseOptions = { boolean: [] as string[], string: [] as string[], @@ -58,7 +61,7 @@ export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs { } } - return parsedArgsProxy; + return parsedArgsProxy as ParsedArgs; } export function resolveArgs(argsDef: ArgsDef): Arg[] { diff --git a/src/command.ts b/src/command.ts index 8777f83..6cd1bf8 100644 --- a/src/command.ts +++ b/src/command.ts @@ -13,14 +13,14 @@ export interface RunCommandOptions { showUsage?: boolean; } -export async function runCommand( - cmd: CommandDef, +export async function runCommand( + cmd: CommandDef, opts: RunCommandOptions ): Promise { const cmdArgs = await resolveValue(cmd.args || {}); - const parsedArgs = parseArgs(opts.rawArgs, cmdArgs); + const parsedArgs = parseArgs(opts.rawArgs, cmdArgs); - const context: CommandContext = { + const context: CommandContext = { rawArgs: opts.rawArgs, args: parsedArgs, cmd, @@ -64,11 +64,11 @@ export async function runCommand( } } -export async function resolveSubCommand( - cmd: CommandDef, +export async function resolveSubCommand( + cmd: CommandDef, rawArgs: string[], - parent?: CommandDef -): Promise<[CommandDef, CommandDef?]> { + parent?: CommandDef +): Promise<[CommandDef, CommandDef?]> { const subCommands = await resolveValue(cmd.subCommands); if (subCommands && Object.keys(subCommands).length > 0) { const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-")); diff --git a/src/main.ts b/src/main.ts index a1e5e68..a99d404 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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"; @@ -8,7 +8,10 @@ export interface RunMainOptions { rawArgs?: string[]; } -export async function runMain(cmd: CommandDef, opts: RunMainOptions = {}) { +export async function runMain( + cmd: CommandDef, + opts: RunMainOptions = {} +) { const rawArgs = opts.rawArgs || process.argv.slice(2); try { if (rawArgs.includes("--help") || rawArgs.includes("-h")) { diff --git a/src/types.ts b/src/types.ts index ea6527a..13df918 100644 --- a/src/types.ts +++ b/src/types.ts @@ -62,7 +62,7 @@ export type CommandDef = { export type CommandContext = { rawArgs: string[]; args: ParsedArgs; - cmd: CommandDef; + cmd: CommandDef; subCommand?: CommandDef; }; diff --git a/src/usage.ts b/src/usage.ts index 6c6524f..2950b6b 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -1,8 +1,11 @@ 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( + cmd: CommandDef, + parent?: CommandDef +) { try { console.log((await renderUsage(cmd, parent)) + "\n"); } catch (error) { @@ -10,7 +13,10 @@ export async function showUsage(cmd: CommandDef, parent?: CommandDef) { } } -export async function renderUsage(cmd: CommandDef, parent?: CommandDef) { +export async function renderUsage( + cmd: CommandDef, + parent?: CommandDef +) { const cmdMeta = await resolveValue(cmd.meta || {}); const cmdArgs = resolveArgs(await resolveValue(cmd.args || {})); const parentMeta = await resolveValue(parent?.meta || {});