main.ts (3820B)
1 #!/usr/bin/env node 2 3 import yargs, { Argv } from "yargs"; 4 import { hideBin } from "yargs/helpers"; 5 import { z } from "zod"; 6 import { handle_css_layout } from "./handle-css-layout.js"; 7 import { handle_css_styles } from "./handle-css-styles.js"; 8 import { handle_css_themes } from "./handle-css-themes.js"; 9 10 const layout_args_schema = z.object({ 11 dir_out: z.string(), 12 presets: z.array(z.string()).optional() 13 }); 14 15 const styles_args_schema = z.object({ 16 dir_out: z.string(), 17 presets: z.array(z.string()).optional() 18 }); 19 20 const themes_args_schema = z.object({ 21 dir_out: z.string(), 22 presets: z.array(z.string()).optional() 23 }); 24 25 export type LayoutArgs = z.infer<typeof layout_args_schema>; 26 export type StylesArgs = z.infer<typeof styles_args_schema>; 27 export type ThemesArgs = z.infer<typeof themes_args_schema>; 28 29 export type CliArgs = 30 | { command: "layout"; args: LayoutArgs } 31 | { command: "styles"; args: StylesArgs } 32 | { command: "themes"; args: ThemesArgs }; 33 34 export const parse_args = (): CliArgs => { 35 const argv = yargs(hideBin(process.argv)) 36 .command( 37 "layout", 38 "Generate CSS layout", 39 (y): Argv<LayoutArgs> => 40 y 41 .option("dir_out", { 42 type: "string", 43 demandOption: false, 44 default: "styles" 45 }) 46 .option("presets", { 47 type: "string", 48 array: true, 49 demandOption: false 50 }) 51 ) 52 .command( 53 "styles", 54 "Generate CSS styles", 55 (y): Argv<StylesArgs> => 56 y 57 .option("dir_out", { 58 type: "string", 59 demandOption: false, 60 default: "styles" 61 }) 62 .option("presets", { 63 type: "string", 64 array: true, 65 demandOption: false 66 }) 67 ) 68 .command( 69 "themes", 70 "Generate CSS themes", 71 (y): Argv<ThemesArgs> => 72 y 73 .option("dir_out", { 74 type: "string", 75 demandOption: false, 76 default: "styles" 77 }) 78 .option("presets", { 79 type: "string", 80 array: true, 81 demandOption: false 82 }) 83 ) 84 .strict() 85 .help() 86 .parseSync(); 87 88 const command = String(argv._[0]); 89 90 switch (command) { 91 case "layout": 92 return { 93 command, 94 args: layout_args_schema.parse(argv) 95 }; 96 case "styles": 97 return { 98 command, 99 args: styles_args_schema.parse(argv) 100 }; 101 case "themes": 102 return { 103 command, 104 args: themes_args_schema.parse(argv) 105 }; 106 default: 107 throw new Error(`Unknown command: ${command}`); 108 } 109 }; 110 111 export const main = (): void => { 112 try { 113 const cli = parse_args(); 114 115 switch (cli.command) { 116 case "layout": 117 return void handle_css_layout(cli.args); 118 case "styles": 119 return void handle_css_styles(cli.args); 120 case "themes": 121 return void handle_css_themes(cli.args); 122 } 123 } catch (error) { 124 const message = 125 error instanceof Error ? error.message : "Unexpected error"; 126 console.error(`Error: ${message}`); 127 process.exit(1); 128 } 129 }; 130 131 main();