|
| 1 | +import { Command } from "commander"; |
| 2 | +import { runAuthCommand, type AuthCommandOptions } from "./commands/auth"; |
| 3 | +import { runBuildCommand, type BuildCommandOptions } from "./commands/build"; |
| 4 | +import { runLogoutCommand, type LogoutCommandOptions } from "./commands/logout"; |
| 5 | +import { runPublishCommand, type PublishCommandOptions } from "./commands/publish"; |
| 6 | +import { DEFAULT_CLIENT_ID } from "./lib/constants"; |
| 7 | +import { EXIT_CODES, normalizeError } from "./utils/errors"; |
| 8 | +import { createLogger, formatErrorMessage } from "./utils/logger"; |
| 9 | + |
| 10 | +function collectString(value: string, previous: string[]): string[] { |
| 11 | + return [...previous, value]; |
| 12 | +} |
| 13 | + |
| 14 | +function toHint(error: ReturnType<typeof normalizeError>): string | undefined { |
| 15 | + if (error.status === 401) { |
| 16 | + return "Run `claws-supply auth` again and ensure your session is still valid."; |
| 17 | + } |
| 18 | + |
| 19 | + if (error.status === 409) { |
| 20 | + return "Pick a different slug and run `claws-supply build` again."; |
| 21 | + } |
| 22 | + |
| 23 | + if (error.status === 422) { |
| 24 | + return "Rebuild the artifact and verify manifest/title/slug integrity."; |
| 25 | + } |
| 26 | + |
| 27 | + if (error.status === 429) { |
| 28 | + return "Rate limit reached. Wait briefly and retry."; |
| 29 | + } |
| 30 | + |
| 31 | + return undefined; |
| 32 | +} |
| 33 | + |
| 34 | +function wrapCommand<T extends { json?: boolean }>(handler: (options: T) => Promise<void>) { |
| 35 | + return async (options: T) => { |
| 36 | + try { |
| 37 | + await handler(options); |
| 38 | + } catch (error) { |
| 39 | + const normalized = normalizeError(error); |
| 40 | + const logger = createLogger({ json: options.json ?? false }); |
| 41 | + const hint = toHint(normalized); |
| 42 | + |
| 43 | + if (normalized.exitCode === EXIT_CODES.SUCCESS) { |
| 44 | + if (options.json) { |
| 45 | + logger.json({ |
| 46 | + success: false, |
| 47 | + canceled: true, |
| 48 | + message: normalized.message, |
| 49 | + exitCode: normalized.exitCode, |
| 50 | + }); |
| 51 | + } else { |
| 52 | + logger.warn(normalized.message); |
| 53 | + } |
| 54 | + process.exitCode = normalized.exitCode; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (options.json) { |
| 59 | + logger.json({ |
| 60 | + success: false, |
| 61 | + error: { |
| 62 | + message: normalized.message, |
| 63 | + code: normalized.code, |
| 64 | + status: normalized.status, |
| 65 | + exitCode: normalized.exitCode, |
| 66 | + hint, |
| 67 | + }, |
| 68 | + }); |
| 69 | + } else { |
| 70 | + logger.error(formatErrorMessage(normalized.message, hint)); |
| 71 | + } |
| 72 | + |
| 73 | + process.exitCode = normalized.exitCode; |
| 74 | + } |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +export function createProgram(): Command { |
| 79 | + const program = new Command(); |
| 80 | + |
| 81 | + program |
| 82 | + .name("claws-supply") |
| 83 | + .description("claws.supply template creator CLI") |
| 84 | + .showHelpAfterError(true); |
| 85 | + |
| 86 | + program |
| 87 | + .command("auth") |
| 88 | + .description("Authenticate via Better Auth device authorization") |
| 89 | + .option("-D, --dev", "Use local API at http://localhost:3039") |
| 90 | + .option("--client-id <id>", "Device auth client ID", DEFAULT_CLIENT_ID) |
| 91 | + .option("--no-open", "Do not auto-open browser") |
| 92 | + .option("--json", "Emit machine-readable JSON output") |
| 93 | + .action(wrapCommand<AuthCommandOptions>(runAuthCommand)); |
| 94 | + |
| 95 | + program |
| 96 | + .command("logout") |
| 97 | + .description("Clear local CLI auth state") |
| 98 | + .option("--json", "Emit machine-readable JSON output") |
| 99 | + .action(wrapCommand<LogoutCommandOptions>(runLogoutCommand)); |
| 100 | + |
| 101 | + program |
| 102 | + .command("build") |
| 103 | + .description("Build and sign a local template artifact") |
| 104 | + .option("-D, --dev", "Use local API at http://localhost:3039") |
| 105 | + .option("--source <path>", "Source directory", process.cwd()) |
| 106 | + .option("--title <title>", "Template title") |
| 107 | + .option("--slug <slug>", "Template slug") |
| 108 | + .option("--include <glob>", "Additional include pattern", collectString, []) |
| 109 | + .option("--exclude <glob>", "Additional exclude pattern", collectString, []) |
| 110 | + .option("--yes", "Use defaults and skip interactive prompts") |
| 111 | + .option("--json", "Emit machine-readable JSON output") |
| 112 | + .action(wrapCommand<BuildCommandOptions>(runBuildCommand)); |
| 113 | + |
| 114 | + program |
| 115 | + .command("publish") |
| 116 | + .description("Upload and publish the latest build artifact as draft") |
| 117 | + .option("-D, --dev", "Use local API at http://localhost:3039") |
| 118 | + .option("--artifact <path>", "Path to a built zip artifact") |
| 119 | + .option("--json", "Emit machine-readable JSON output") |
| 120 | + .action(wrapCommand<PublishCommandOptions>(runPublishCommand)); |
| 121 | + |
| 122 | + return program; |
| 123 | +} |
| 124 | + |
| 125 | +export async function runCli(argv = process.argv): Promise<void> { |
| 126 | + const program = createProgram(); |
| 127 | + await program.parseAsync(argv); |
| 128 | +} |
0 commit comments