|
4 | 4 | * options, and hooks for logging, theming, and configuration loading. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import {Command} from "commander"; |
8 | | -import {getVersion} from "./version"; |
9 | | -import {loadConfig} from "./config/loader.ts"; |
10 | | -import {buildDynamicCommands} from "./cli-helpers/builder"; |
11 | | -import {createGenerateCommand} from "./cli-helpers/commands/generate"; |
12 | | -import {createThemeCommand} from "./cli-helpers/commands/theme"; |
13 | | -import {pretty, configurePretty, LogLevel, setTheme} from "./utils/pretty"; |
14 | | -import {themes, type ThemeName} from "./utils/themes"; |
15 | | -import type {ZodAxogenConfig} from "./config/types"; |
16 | | - |
17 | | -/** |
18 | | - * Creates and configures the main CLI application with all commands and options. |
19 | | - * Sets up global options, pre-action hooks for configuration, and dynamic commands. |
20 | | - * @returns Promise that resolves to the configured Command instance |
21 | | - */ |
22 | | -async function createCLI(): Promise<Command> { |
23 | | - const cli = new Command(); |
24 | | - |
25 | | - cli.name("axogen") |
26 | | - .description("TypeScript-native configuration and task management") |
27 | | - .version(getVersion()); |
28 | | - |
29 | | - // Global options |
30 | | - cli.option("--verbose", "Enable verbose logging"); |
31 | | - cli.option("--quiet", "Suppress non-essential output"); |
32 | | - cli.option("--no-color", "Disable colored output"); |
33 | | - cli.option( |
34 | | - "--theme <name>", |
35 | | - `Color theme to use (${Object.keys(themes).join(", ")})`, |
36 | | - process.env.AXOGEN_THEME || "doom-one" // Default theme |
37 | | - ); |
38 | | - |
39 | | - // Configure pretty printing based on global options early |
40 | | - cli.hook("preAction", (thisCommand) => { |
41 | | - const opts = thisCommand.optsWithGlobals(); |
42 | | - |
43 | | - // Validate and set theme |
44 | | - if (opts.theme && !(opts.theme in themes)) { |
45 | | - console.error( |
46 | | - `❌ Invalid theme "${opts.theme}". Available themes: ${Object.keys(themes).join(", ")}` |
47 | | - ); |
48 | | - process.exit(1); |
49 | | - } |
50 | | - |
51 | | - configurePretty({ |
52 | | - verbose: opts.verbose || false, |
53 | | - logLevel: opts.quiet |
54 | | - ? LogLevel.WARN |
55 | | - : opts.verbose |
56 | | - ? LogLevel.DEBUG |
57 | | - : LogLevel.INFO, |
58 | | - colorEnabled: |
59 | | - !opts.noColor && !process.env.NO_COLOR && process.stdout.isTTY, |
60 | | - theme: opts.theme as ThemeName, |
61 | | - }); |
62 | | - |
63 | | - if (opts.theme) { |
64 | | - setTheme(opts.theme as ThemeName); |
65 | | - } |
66 | | - }); |
67 | | - |
68 | | - // Load config once |
69 | | - let config: ZodAxogenConfig; |
70 | | - try { |
71 | | - config = await loadConfig(); |
72 | | - } catch (error) { |
73 | | - // If config fails to load, still allow basic commands |
74 | | - pretty.warn( |
75 | | - `Could not load config: ${error instanceof Error ? error.message : error}` |
76 | | - ); |
77 | | - config = {}; |
78 | | - } |
79 | | - |
80 | | - // Add built-in commands (pass config to them) |
81 | | - cli.addCommand(createGenerateCommand(config)); |
82 | | - cli.addCommand(createThemeCommand()); |
83 | | - |
84 | | - // Add dynamic commands from config to the "run" command |
85 | | - const runCommand = new Command("run").description( |
86 | | - "Run custom commands defined in configuration" |
87 | | - ); |
88 | | - |
89 | | - if (config.commands && Object.keys(config.commands).length > 0) { |
90 | | - buildDynamicCommands(runCommand, config); |
91 | | - } else { |
92 | | - // Add a default action to show available commands or a helpful message |
93 | | - runCommand.action(() => { |
94 | | - pretty.info("No commands defined in config"); |
95 | | - }); |
96 | | - } |
97 | | - |
98 | | - cli.addCommand(runCommand); |
99 | | - |
100 | | - return cli; |
101 | | -} |
| 7 | +import {pretty} from "./utils/pretty"; |
| 8 | +import {createCLI} from "./cli-helpers"; |
102 | 9 |
|
103 | 10 | // Run CLI |
104 | 11 | createCLI() |
|
0 commit comments