@drizzle-team/brocli
Advanced tools
Comparing version 0.0.7 to 0.1.0
@@ -84,12 +84,6 @@ /** | ||
type CommandHandler<TOpts extends Record<string, GenericBuilderInternals> | undefined = Record<string, GenericBuilderInternals> | undefined> = (options: TOpts extends Record<string, GenericBuilderInternals> ? TypeOf<TOpts> : undefined) => any; | ||
type HelpHandler = (commands: Command[]) => any; | ||
type CommandHelpHandler = (command: Command) => any; | ||
type VersionHelpHandler = (name: string, version: string) => any; | ||
type BroCliConfig = { | ||
name?: string; | ||
version?: string; | ||
argSource?: string[]; | ||
help?: HelpHandler; | ||
commandHelp?: CommandHelpHandler; | ||
versionHelp?: VersionHelpHandler; | ||
help?: string | Function; | ||
version?: string | Function; | ||
}; | ||
@@ -103,2 +97,3 @@ type GenericCommandHandler = (options?: Record<string, OutputType> | undefined) => any; | ||
options?: TOpts; | ||
help?: string | Function; | ||
handler: CommandHandler<TOpts>; | ||
@@ -112,2 +107,3 @@ }; | ||
options?: GenericProcessedOptions; | ||
help?: string | Function; | ||
handler: GenericCommandHandler; | ||
@@ -124,3 +120,4 @@ }; | ||
declare const runCli: (commands: Command[], config?: BroCliConfig) => void; | ||
declare const handler: <TOpts extends Record<string, GenericBuilderInternals>>(options: TOpts, handler: CommandHandler<TOpts>) => CommandHandler<TOpts>; | ||
export { type AssignConfigName, type BroCliConfig, BroCliError as BrocliError, type BuilderConfig, type Command, type CommandHandler, type CommandHelpHandler, type GenericBuilderConfig, type GenericBuilderInternals, type GenericBuilderInternalsFields, type GenericCommandHandler, type GenericProcessedOptions, type HelpHandler, OptionBuilderBase, type OptionType, type OutputType, type ProcessedOptions, type RawCommand, type Simplify, type TypeOf, type VersionHelpHandler, boolean, command, runCli, string }; | ||
export { type AssignConfigName, type BroCliConfig, BroCliError as BrocliError, type BuilderConfig, type Command, type CommandHandler, type GenericBuilderConfig, type GenericBuilderInternals, type GenericBuilderInternalsFields, type GenericCommandHandler, type GenericProcessedOptions, OptionBuilderBase, type OptionType, type OutputType, type ProcessedOptions, type RawCommand, type Simplify, type TypeOf, boolean, command, handler, runCli, string }; |
57
index.js
@@ -66,3 +66,3 @@ // src/brocli-error.ts | ||
// src/command-core.ts | ||
var help = (commands) => { | ||
var help = (commands) => () => { | ||
const cmds = commands.filter((cmd) => !cmd.hidden); | ||
@@ -83,22 +83,2 @@ const tableCmds = cmds.map((cmd) => ({ | ||
}; | ||
var commandHelp = (command2) => { | ||
const options = command2.options ? Object.values(command2.options).filter((opt) => !opt.config?.isHidden).map( | ||
({ config: opt }) => ({ | ||
name: opt.name, | ||
aliases: opt.aliases.length ? `${opt.aliases.join(", ")}` : "-", | ||
description: opt.description ?? "-", | ||
type: opt.type, | ||
required: opt.isRequired ? "\u2713" : "\u2717" | ||
}) | ||
) : void 0; | ||
console.log( | ||
`Command: ${command2.name}${command2.aliases ? ` [${command2.aliases.join(", ")}]` : ""}${command2.description ? ` - ${command2.description}` : ""}` | ||
); | ||
if (!options?.length) return; | ||
console.log("\nOptions:"); | ||
console.table(options); | ||
}; | ||
var versionHelp = (name, version) => { | ||
console.log(`${name} - ${version}`); | ||
}; | ||
var missingRequired = (command2, missingOpts) => { | ||
@@ -300,3 +280,7 @@ const msg = `Command '${command2.name}' is missing following required options: ${missingOpts.map((opt) => { | ||
}; | ||
var helpCommand = (commands, helpHandler, commandHelpHandler) => command({ | ||
var executeOrLog = async (target) => { | ||
if (!target || typeof target === "string") console.log(target); | ||
else await target(); | ||
}; | ||
var helpCommand = (commands, helpHandler) => command({ | ||
name: "help", | ||
@@ -308,8 +292,12 @@ description: "List commands or command details", | ||
hidden: true, | ||
handler: (options) => { | ||
handler: async (options) => { | ||
const { command: command2 } = options; | ||
if (command2 === void 0) return helpHandler(commands); | ||
if (command2 === void 0) { | ||
return typeof helpHandler === "string" ? console.log(helpHandler) : helpHandler(commands); | ||
} | ||
const cmd = commands.find((e) => e.name === command2); | ||
if (cmd) return commandHelpHandler(cmd); | ||
helpHandler(commands); | ||
if (cmd) { | ||
return executeOrLog(cmd.help); | ||
} | ||
return typeof helpHandler === "string" ? console.log(helpHandler) : helpHandler(commands); | ||
} | ||
@@ -346,10 +334,7 @@ }); | ||
let cmd; | ||
const rawCmds = validateCommands(commands); | ||
const argSource = config?.argSource ?? process.argv; | ||
const version = config?.version ?? "0.1.0"; | ||
const name = config?.name ?? "brocli"; | ||
const versionHelpHandler = config?.versionHelp ?? versionHelp; | ||
const helpHandler = config?.help ?? help; | ||
const commandHelpHandler = config?.commandHelp ?? commandHelp; | ||
const rawCmds = validateCommands(commands); | ||
const cmds = [...rawCmds, helpCommand(rawCmds, helpHandler, commandHelpHandler)]; | ||
const version = config?.version; | ||
const helpHandler = config?.help ?? help(rawCmds); | ||
const cmds = [...rawCmds, helpCommand(rawCmds, helpHandler)]; | ||
let args = argSource.slice(2, argSource.length); | ||
@@ -370,7 +355,7 @@ if (!args.length) return help(cmds); | ||
} | ||
return command3 ? commandHelpHandler(command3) : helpHandler(cmds); | ||
return command3 ? executeOrLog(command3.help) : executeOrLog(helpHandler); | ||
} | ||
const versionIndex = args.findIndex((arg) => arg === "--version" || arg === "-v"); | ||
if (versionIndex !== -1 && (versionIndex > 0 ? args[versionIndex - 1]?.startsWith("-") ? false : true : true)) { | ||
return versionHelpHandler(name, version); | ||
return executeOrLog(version); | ||
} | ||
@@ -394,2 +379,3 @@ const { command: command2, index } = getCommand(cmds, args); | ||
}; | ||
var handler = (options, handler2) => handler2; | ||
export { | ||
@@ -399,2 +385,3 @@ BroCliError as BrocliError, | ||
command, | ||
handler, | ||
runCli, | ||
@@ -401,0 +388,0 @@ string |
@@ -5,3 +5,3 @@ { | ||
"author": "Drizzle Team", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"description": "Typed CLI command runner", | ||
@@ -8,0 +8,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
117939
913