Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Jul 5, 2023
1 parent 2cfd134 commit 188d60c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function toVal(out, key, val, opts) {

export function parseRawArgs<T = Default>(
args: string[] = [],
opts: Options = {}
opts: Options = {},
): Argv<T> {
let k;
let arr;
Expand Down
10 changes: 7 additions & 3 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export function formatLineColumns(lines: string[][], linePrefix = "") {
.map((l) =>
l
.map(
(c, i) => linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLengh[i])
(c, i) =>
linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLengh[i]),
)
.join(" ")
.join(" "),
)
.join("\n");
}
Expand All @@ -30,7 +31,10 @@ export function resolveValue<T>(input: Resolvable<T>): T | Promise<T> {
}

export class CLIError extends Error {
constructor(message: string, public code?: string) {
constructor(
message: string,
public code?: string,
) {
super(message);
this.name = "CLIError";
}
Expand Down
4 changes: 2 additions & 2 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CLIError, toArray } from "./_utils";

export function parseArgs<T extends ArgsDef = ArgsDef>(
rawArgs: string[],
argsDef: ArgsDef
argsDef: ArgsDef,
): ParsedArgs<T> {
const parseOptions = {
boolean: [] as string[],
Expand Down Expand Up @@ -51,7 +51,7 @@ export function parseArgs<T extends ArgsDef = ArgsDef>(
} else if (arg.default === undefined) {
throw new CLIError(
`Missing required positional argument: ${arg.name.toUpperCase()}`,
"EARG"
"EARG",
);
} else {
parsedArgsProxy[arg.name] = arg.default;
Expand Down
14 changes: 7 additions & 7 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CLIError, resolveValue } from "./_utils";
import { parseArgs } from "./args";

export function defineCommand<T extends ArgsDef = ArgsDef>(
def: CommandDef<T>
def: CommandDef<T>,
): CommandDef<T> {
return def;
}
Expand All @@ -15,7 +15,7 @@ export interface RunCommandOptions {

export async function runCommand<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
opts: RunCommandOptions
opts: RunCommandOptions,
): Promise<void> {
const cmdArgs = await resolveValue(cmd.args || {});
const parsedArgs = parseArgs<T>(opts.rawArgs, cmdArgs);
Expand All @@ -35,19 +35,19 @@ export async function runCommand<T extends ArgsDef = ArgsDef>(
const subCommands = await resolveValue(cmd.subCommands);
if (subCommands && Object.keys(subCommands).length > 0) {
const subCommandArgIndex = opts.rawArgs.findIndex(
(arg) => !arg.startsWith("-")
(arg) => !arg.startsWith("-"),
);
const subCommandName = opts.rawArgs[subCommandArgIndex];
if (!subCommandName && !cmd.run) {
throw new CLIError(
`Missing sub command. Use --help to see available sub commands.`,
"ESUBCOMMAND"
"ESUBCOMMAND",
);
}
if (!subCommands[subCommandName]) {
throw new CLIError(
`Unknown sub command: ${subCommandName}`,
"ESUBCOMMAND"
"ESUBCOMMAND",
);
}
const subCommand = await resolveValue(subCommands[subCommandName]);
Expand All @@ -67,7 +67,7 @@ export async function runCommand<T extends ArgsDef = ArgsDef>(
export async function resolveSubCommand<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
rawArgs: string[],
parent?: CommandDef<T>
parent?: CommandDef<T>,
): Promise<[CommandDef<T>, CommandDef<T>?]> {
const subCommands = await resolveValue(cmd.subCommands);
if (subCommands && Object.keys(subCommands).length > 0) {
Expand All @@ -78,7 +78,7 @@ export async function resolveSubCommand<T extends ArgsDef = ArgsDef>(
return resolveSubCommand(
subCommand,
rawArgs.slice(subCommandArgIndex + 1),
cmd
cmd,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface RunMainOptions {

export async function runMain<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
opts: RunMainOptions = {}
opts: RunMainOptions = {},
) {
const rawArgs = opts.rawArgs || process.argv.slice(2);
try {
Expand All @@ -26,7 +26,7 @@ export async function runMain<T extends ArgsDef = ArgsDef>(
console.error(error, "\n");
}
console.error(
`\n${bgRed(` ${error.code || error.name} `)} ${error.message}\n`
`\n${bgRed(` ${error.code || error.name} `)} ${error.message}\n`,
);
if (isCLIError) {
await showUsage(...(await resolveSubCommand(cmd, rawArgs)));
Expand Down
14 changes: 7 additions & 7 deletions src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolveArgs } from "./args";

export async function showUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>
parent?: CommandDef<T>,
) {
try {
console.log((await renderUsage(cmd, parent)) + "\n");
Expand All @@ -15,7 +15,7 @@ export async function showUsage<T extends ArgsDef = ArgsDef>(

export async function renderUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>
parent?: CommandDef<T>,
) {
const cmdMeta = await resolveValue(cmd.meta || {});
const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
`--no-${arg.name}`,
].join(", ")
: [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(
", "
", ",
)) +
(arg.type === "string" && (arg.valueHint || arg.default)
? `=${
Expand Down Expand Up @@ -82,15 +82,15 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
usageLines.push(
commandName + (version ? ` v${version}` : ""),
cmdMeta.description,
""
"",
);

const hasOptions = argLines.length > 0 || posLines.length > 0;
usageLines.push(
`USAGE: ${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(
" "
" ",
)}`,
""
"",
);

if (posLines.length > 0) {
Expand All @@ -110,7 +110,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
usageLines.push(formatLineColumns(commandsLines, " "));
usageLines.push(
"",
`Use \`${commandName} <command> --help\` for more information about a command.`
`Use \`${commandName} <command> --help\` for more information about a command.`,
);
}

Expand Down

0 comments on commit 188d60c

Please sign in to comment.