diff --git a/packages/cli/package.json b/packages/cli/package.json index 5eff730..fb3ea13 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,10 +1,10 @@ { "name": "@myst/cli", - "description": "A CLI tool that helps you wire Myst into an existing Forgejo instance with expiring grant access controls for repos you choose to share.", + "description": "CLI for configuring and verifying Myst with Forgejo", "version": "0.1.0", "author": { "name": "Zach Tatman", - "email": "zach.c.tatman@gmail.com" + "email": "zach.tatman.dev@gmail.com" }, "bugs": { "url": "https://github.com/ZTatman/myst/issues" diff --git a/packages/cli/src/help.ts b/packages/cli/src/help.ts new file mode 100644 index 0000000..2de79fe --- /dev/null +++ b/packages/cli/src/help.ts @@ -0,0 +1,64 @@ +import * as c from "yoctocolors"; +import pkg from "../package.json" with { type: "json" }; + +const PADDING = 4; +const INDENT = " "; + +type HelpRow = { + name: string; + desc?: string; +}; + +const commandRows: HelpRow[] = [ + { name: "init", desc: "Generate config files with the proper env variables" }, +]; + +const globalFlagRows: HelpRow[] = [ + { name: "-h, --help", desc: "Display help information" }, + { name: "-v, --version", desc: "Display package version number" }, +]; + +function formatSection( + header: string, + rows: HelpRow[], + colWidth: number, +): string { + return [ + c.bold(`${header}:`), + ...rows.map((row) => + row.desc + ? `${INDENT}${row.name.padEnd(colWidth)}${row.desc}` + : `${INDENT}${row.name}`, + ), + ].join("\n"); +} + +/** + * Returns the help text for the CLI. + * @returns The help text. + */ +export function getHelpText(): string { + const allRows = [...commandRows, ...globalFlagRows]; + const colWidth = Math.max(...allRows.map((row) => row.name.length)) + PADDING; + + return [ + c.yellow("myst"), + "", + `${INDENT}${pkg.description}`, + "", + formatSection( + "Usage", + [ + { + name: "myst [options/flags] [arguments]", + }, + ], + colWidth, + ), + "", + formatSection("Commands", commandRows, colWidth), + "", + formatSection("Global flags", globalFlagRows, colWidth), + "", + ].join("\n"); +} diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 88a6a00..e4f9b7d 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -3,74 +3,20 @@ import * as c from "yoctocolors"; import pkg from "../package.json" with { type: "json" }; import { initCommand } from "./commands/init.js"; - -const PADDING = 4; -const INDENT = " "; -const USAGE = "myst [options/flags] [arguments]"; +import { getHelpText } from "./help.js"; type CommandName = "init"; -interface Option { - name: CommandName | string | string[]; - desc: string; -} +type CommandHandler = (args: string[]) => Promise; -interface CLIConfig { - commands: Option[]; - globalFlags: Option[]; -} - -const config: CLIConfig = { - commands: [ - { name: "init", desc: "Bootstrap Myst and generate config/env files" }, - ], - globalFlags: [ - { name: ["-h", "--help"], desc: "Display help information" }, - { name: ["-v", "--version"], desc: "Display package version number" }, - ], +const commandHandlers: Record = { + init: initCommand, }; -const splitCamelCaseWords = (text: string): string => - text.replace(/([a-z])([A-Z])/g, "$1 $2"); - -const toTitleCase = (text: string): string => - text.replace(/\b\w/g, (c) => c.toUpperCase()); - -function formatSection( - header: string, - options: Option[], - colWidth: number, -): string { - const rows = options.map((o) => { - const name = Array.isArray(o.name) ? o.name.join(", ") : o.name; - return `${INDENT}${name.padEnd(colWidth)}${o.desc}`; - }); - const title = toTitleCase(splitCamelCaseWords(header)); - return [c.bold(title + ":"), ...rows].join("\n"); +function isKnownCommand(value: string): value is CommandName { + return value in commandHandlers; } -const allOptions = Object.values(config).flat(); -const COL_WIDTH = - Math.max( - ...allOptions.map((o) => - Array.isArray(o.name) ? o.name.join(", ").length : o.name.length, - ), - ) + PADDING; - -const HELP_TEXT = ` -${c.yellow(pkg.name)} - - -${INDENT}${USAGE} - - -${Object.entries(config) - .map(([header, options]) => - formatSection(header, options as Option[], COL_WIDTH), - ) - .join("\n\n")} -`; - async function main(): Promise { const [command, ...options] = process.argv.slice(2); const flags = options.filter((o) => o.startsWith("-")); @@ -82,19 +28,11 @@ async function main(): Promise { } if (!command || flags.includes("-h") || flags.includes("--help")) { - process.stdout.write(`${HELP_TEXT}`); + process.stdout.write(getHelpText()); process.exitCode = 0; return; } - const knownCommands: Record Promise> = - { - init: initCommand, - }; - - const isKnownCommand = (cmd: string): cmd is CommandName => - cmd in knownCommands; - if (!isKnownCommand(command)) { process.stderr.write(`Unknown command: ${command}\n`); process.exitCode = 1; @@ -102,7 +40,7 @@ async function main(): Promise { } try { - await knownCommands[command](options); + await commandHandlers[command](options); } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; process.stderr.write(`${c.red("Error:")} ${message}\n`);