|
1 | | -import { |
2 | | - defineCommand, |
3 | | - createMain, |
4 | | - type CommandDef, |
5 | | - type ArgsDef, |
6 | | -} from 'citty'; |
| 1 | +import { defineCommand, createMain } from 'citty'; |
7 | 2 | import tab from '../src/citty'; |
8 | 3 |
|
9 | | -const main = defineCommand({ |
| 4 | +const cli = defineCommand({ |
10 | 5 | meta: { |
11 | | - name: 'vite', |
12 | | - version: '0.0.0', |
13 | | - description: 'Vite CLI', |
| 6 | + name: 'mycli', |
| 7 | + version: '1.0.0', |
| 8 | + description: 'My CLI app', |
14 | 9 | }, |
15 | 10 | args: { |
16 | | - project: { |
17 | | - type: 'positional', |
18 | | - description: 'Project name', |
19 | | - required: true, |
| 11 | + debug: { |
| 12 | + type: 'boolean', |
| 13 | + description: 'Enable debug mode', |
20 | 14 | }, |
21 | | - config: { |
22 | | - type: 'string', |
23 | | - description: 'Use specified config file', |
24 | | - alias: 'c', |
| 15 | + verbose: { |
| 16 | + type: 'boolean', |
| 17 | + description: 'Enable verbose output', |
25 | 18 | }, |
26 | | - mode: { |
27 | | - type: 'string', |
28 | | - description: 'Set env mode', |
29 | | - alias: 'm', |
30 | | - }, |
31 | | - logLevel: { |
32 | | - type: 'string', |
33 | | - description: 'info | warn | error | silent', |
34 | | - alias: 'l', |
35 | | - }, |
36 | | - }, |
37 | | - run: (_ctx) => {}, |
38 | | -}); |
39 | | - |
40 | | -const devCommand = defineCommand({ |
41 | | - meta: { |
42 | | - name: 'dev', |
43 | | - description: 'Start dev server', |
44 | | - }, |
45 | | - args: { |
46 | | - host: { |
47 | | - type: 'string', |
48 | | - description: 'Specify hostname', |
49 | | - alias: 'H', |
50 | | - }, |
51 | | - port: { |
| 19 | + config: { |
52 | 20 | type: 'string', |
53 | | - description: 'Specify port', |
54 | | - alias: 'p', |
55 | | - }, |
56 | | - }, |
57 | | - run: () => {}, |
58 | | -}); |
59 | | - |
60 | | -const buildCommand = defineCommand({ |
61 | | - meta: { |
62 | | - name: 'build', |
63 | | - description: 'Build project', |
64 | | - }, |
65 | | - run: () => {}, |
66 | | -}); |
67 | | - |
68 | | -const copyCommand = defineCommand({ |
69 | | - meta: { |
70 | | - name: 'copy', |
71 | | - description: 'Copy files', |
72 | | - }, |
73 | | - args: { |
74 | | - source: { |
75 | | - type: 'positional', |
76 | | - description: 'Source file or directory', |
77 | | - required: true, |
78 | | - }, |
79 | | - destination: { |
80 | | - type: 'positional', |
81 | | - description: 'Destination file or directory', |
82 | | - required: true, |
83 | | - }, |
84 | | - }, |
85 | | - run: () => {}, |
86 | | -}); |
87 | | - |
88 | | -const lintCommand = defineCommand({ |
89 | | - meta: { |
90 | | - name: 'lint', |
91 | | - description: 'Lint project', |
92 | | - }, |
93 | | - args: { |
94 | | - files: { |
95 | | - type: 'positional', |
96 | | - description: 'Files to lint', |
97 | | - required: false, |
| 21 | + description: 'Config file path', |
98 | 22 | }, |
99 | 23 | }, |
100 | | - run: () => {}, |
101 | | -}); |
102 | | - |
103 | | -main.subCommands = { |
104 | | - dev: devCommand, |
105 | | - build: buildCommand, |
106 | | - copy: copyCommand, |
107 | | - lint: lintCommand, |
108 | | -} as Record<string, CommandDef<ArgsDef>>; |
109 | | - |
110 | | -const completion = await tab(main, { |
111 | | - args: { |
112 | | - project: function (complete) { |
113 | | - complete('my-app', 'My application'); |
114 | | - complete('my-lib', 'My library'); |
115 | | - complete('my-tool', 'My tool'); |
116 | | - }, |
117 | | - }, |
118 | | - options: { |
119 | | - config: function (this: any, complete) { |
120 | | - complete('vite.config.ts', 'Vite config file'); |
121 | | - complete('vite.config.js', 'Vite config file'); |
122 | | - }, |
123 | | - mode: function (this: any, complete) { |
124 | | - complete('development', 'Development mode'); |
125 | | - complete('production', 'Production mode'); |
126 | | - }, |
127 | | - logLevel: function (this: any, complete) { |
128 | | - complete('info', 'Info level'); |
129 | | - complete('warn', 'Warn level'); |
130 | | - complete('error', 'Error level'); |
131 | | - complete('silent', 'Silent level'); |
132 | | - }, |
133 | | - }, |
134 | | - |
135 | 24 | subCommands: { |
136 | | - copy: { |
| 25 | + dev: { |
| 26 | + meta: { |
| 27 | + name: 'dev', |
| 28 | + description: 'Start development server', |
| 29 | + }, |
137 | 30 | args: { |
138 | | - source: function (complete) { |
139 | | - complete('src/', 'Source directory'); |
140 | | - complete('dist/', 'Distribution directory'); |
141 | | - complete('public/', 'Public assets'); |
| 31 | + port: { |
| 32 | + type: 'string', |
| 33 | + description: 'Port number', |
| 34 | + }, |
| 35 | + host: { |
| 36 | + type: 'string', |
| 37 | + description: 'Host to bind to', |
142 | 38 | }, |
143 | | - destination: function (complete) { |
144 | | - complete('build/', 'Build output'); |
145 | | - complete('release/', 'Release directory'); |
146 | | - complete('backup/', 'Backup location'); |
| 39 | + watch: { |
| 40 | + type: 'boolean', |
| 41 | + description: 'Watch for changes', |
147 | 42 | }, |
| 43 | + hmr: { |
| 44 | + type: 'boolean', |
| 45 | + description: 'Enable hot module replacement', |
| 46 | + default: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + run: () => { |
| 50 | + console.log('Starting development server...'); |
148 | 51 | }, |
149 | 52 | }, |
150 | | - lint: { |
| 53 | + build: { |
| 54 | + meta: { |
| 55 | + name: 'build', |
| 56 | + description: 'Build the project', |
| 57 | + }, |
151 | 58 | args: { |
152 | | - files: function (complete) { |
153 | | - complete('main.ts', 'Main file'); |
154 | | - complete('index.ts', 'Index file'); |
| 59 | + minify: { |
| 60 | + type: 'boolean', |
| 61 | + description: 'Enable minification', |
| 62 | + default: true, |
| 63 | + }, |
| 64 | + sourcemap: { |
| 65 | + type: 'boolean', |
| 66 | + description: 'Generate source maps', |
155 | 67 | }, |
| 68 | + cache: { |
| 69 | + type: 'boolean', |
| 70 | + description: 'Enable build cache', |
| 71 | + default: true, |
| 72 | + }, |
| 73 | + }, |
| 74 | + run: () => { |
| 75 | + console.log('Building project...'); |
156 | 76 | }, |
157 | 77 | }, |
158 | | - dev: { |
159 | | - options: { |
160 | | - port: function (this: any, complete) { |
161 | | - complete('3000', 'Development server port'); |
162 | | - complete('8080', 'Alternative port'); |
| 78 | + serve: { |
| 79 | + meta: { |
| 80 | + name: 'serve', |
| 81 | + description: 'Serve the application', |
| 82 | + }, |
| 83 | + args: { |
| 84 | + port: { |
| 85 | + type: 'string', |
| 86 | + description: 'Port number', |
| 87 | + }, |
| 88 | + https: { |
| 89 | + type: 'boolean', |
| 90 | + description: 'Enable HTTPS', |
163 | 91 | }, |
164 | | - host: function (this: any, complete) { |
165 | | - complete('localhost', 'Localhost'); |
166 | | - complete('0.0.0.0', 'All interfaces'); |
| 92 | + cors: { |
| 93 | + type: 'boolean', |
| 94 | + description: 'Enable CORS', |
167 | 95 | }, |
168 | 96 | }, |
| 97 | + run: () => { |
| 98 | + console.log('Serving application...'); |
| 99 | + }, |
169 | 100 | }, |
170 | 101 | }, |
| 102 | + run: () => { |
| 103 | + console.log('Running CLI...'); |
| 104 | + }, |
171 | 105 | }); |
172 | 106 |
|
173 | | -void completion; |
| 107 | +await tab(cli, { |
| 108 | + options: { |
| 109 | + port: (complete) => { |
| 110 | + complete('3000', 'Default port'); |
| 111 | + complete('8080', 'Alternative port'); |
| 112 | + }, |
| 113 | + host: (complete) => { |
| 114 | + complete('localhost', 'Local development'); |
| 115 | + complete('0.0.0.0', 'All interfaces'); |
| 116 | + }, |
| 117 | + config: (complete) => { |
| 118 | + complete('config.json', 'JSON config file'); |
| 119 | + complete('config.yaml', 'YAML config file'); |
| 120 | + }, |
| 121 | + }, |
| 122 | +}); |
174 | 123 |
|
175 | | -const cli = createMain(main); |
176 | | -cli(); |
| 124 | +// Create and run the CLI |
| 125 | +const main = createMain(cli); |
| 126 | +main(); |
0 commit comments