refactor(cli): move help related functions to help.ts to keep

cli/src/index.ts entry point narrow
This commit is contained in:
ZTatman 2026-04-08 10:28:59 -04:00
parent ee85b079ad
commit c4c41e0e31
3 changed files with 74 additions and 72 deletions

View file

@ -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"

64
packages/cli/src/help.ts Normal file
View file

@ -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 <command> [options/flags] [arguments]",
},
],
colWidth,
),
"",
formatSection("Commands", commandRows, colWidth),
"",
formatSection("Global flags", globalFlagRows, colWidth),
"",
].join("\n");
}

View file

@ -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 <command> [options/flags] [arguments]";
import { getHelpText } from "./help.js";
type CommandName = "init";
interface Option {
name: CommandName | string | string[];
desc: string;
}
type CommandHandler = (args: string[]) => Promise<void>;
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<CommandName, CommandHandler> = {
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<void> {
const [command, ...options] = process.argv.slice(2);
const flags = options.filter((o) => o.startsWith("-"));
@ -82,19 +28,11 @@ async function main(): Promise<void> {
}
if (!command || flags.includes("-h") || flags.includes("--help")) {
process.stdout.write(`${HELP_TEXT}`);
process.stdout.write(getHelpText());
process.exitCode = 0;
return;
}
const knownCommands: Record<CommandName, (args: string[]) => Promise<void>> =
{
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<void> {
}
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`);