|
1 | 1 | import cac from 'cac'; |
2 | 2 | import tab from '../src/cac'; |
| 3 | +import type { Command, Option, OptionsMap } from '../src/t'; |
3 | 4 |
|
4 | 5 | const cli = cac('vite'); |
5 | 6 |
|
|
8 | 9 | .option('-m, --mode <mode>', `Set env mode`) |
9 | 10 | .option('-l, --logLevel <level>', `info | warn | error | silent`); |
10 | 11 |
|
| 12 | + |
| 13 | + |
11 | 14 | cli |
12 | 15 | .command('dev', 'Start dev server') |
13 | 16 | .option('-H, --host [host]', `Specify hostname`) |
|
22 | 25 |
|
23 | 26 | cli.command('dev build', 'Build project').action((options) => {}); |
24 | 27 |
|
25 | | -cli.command('lint [...files]', 'Lint project').action((files, options) => {}); |
| 28 | +cli.command('copy <source> <destination>', 'Copy files').action((source, destination, options) => {}); |
26 | 29 |
|
27 | | -const completion = await tab(cli); |
28 | | - |
29 | | -for (const command of completion.commands.values()) { |
30 | | - if (command.name === 'lint') { |
31 | | - command.handler = () => { |
32 | | - return [ |
33 | | - { value: 'main.ts', description: 'Main file' }, |
34 | | - { value: 'index.ts', description: 'Index file' }, |
35 | | - ]; |
36 | | - }; |
37 | | - } |
| 30 | +cli.command('lint [...files]', 'Lint project').action((files, options) => {}); |
38 | 31 |
|
39 | | - for (const [o, config] of command.options.entries()) { |
40 | | - if (o === '--port') { |
41 | | - config.handler = () => { |
42 | | - return [ |
43 | | - { value: '3000', description: 'Development server port' }, |
44 | | - { value: '8080', description: 'Alternative port' }, |
45 | | - ]; |
46 | | - }; |
47 | | - } |
48 | | - if (o === '--host') { |
49 | | - config.handler = () => { |
50 | | - return [ |
51 | | - { value: 'localhost', description: 'Localhost' }, |
52 | | - { value: '0.0.0.0', description: 'All interfaces' }, |
53 | | - ]; |
54 | | - }; |
55 | | - } |
56 | | - if (o === '--config') { |
57 | | - config.handler = () => { |
58 | | - return [ |
59 | | - { value: 'vite.config.ts', description: 'Vite config file' }, |
60 | | - { value: 'vite.config.js', description: 'Vite config file' }, |
61 | | - ]; |
62 | | - }; |
63 | | - } |
64 | | - if (o === '--mode') { |
65 | | - config.handler = () => { |
66 | | - return [ |
67 | | - { value: 'development', description: 'Development mode' }, |
68 | | - { value: 'production', description: 'Production mode' }, |
69 | | - ]; |
70 | | - }; |
71 | | - } |
72 | | - if (o === '--logLevel') { |
73 | | - config.handler = () => { |
74 | | - return [ |
75 | | - { value: 'info', description: 'Info level' }, |
76 | | - { value: 'warn', description: 'Warn level' }, |
77 | | - { value: 'error', description: 'Error level' }, |
78 | | - { value: 'silent', description: 'Silent level' }, |
79 | | - ]; |
80 | | - }; |
81 | | - } |
82 | | - } |
83 | | -} |
| 32 | +// Note: With the new t.ts API, handlers are configured through the completionConfig parameter |
| 33 | +// rather than by modifying the returned completion object directly |
| 34 | +await tab(cli, { |
| 35 | + subCommands: { |
| 36 | + copy: { |
| 37 | + args: { |
| 38 | + source: function(complete) { |
| 39 | + complete('src/', 'Source directory'); |
| 40 | + complete('dist/', 'Distribution directory'); |
| 41 | + complete('public/', 'Public assets'); |
| 42 | + }, |
| 43 | + destination: function(complete) { |
| 44 | + complete('build/', 'Build output'); |
| 45 | + complete('release/', 'Release directory'); |
| 46 | + complete('backup/', 'Backup location'); |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + lint: { |
| 51 | + args: { |
| 52 | + files: function(complete) { |
| 53 | + complete('main.ts', 'Main file'); |
| 54 | + complete('index.ts', 'Index file'); |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + dev: { |
| 59 | + options: { |
| 60 | + port: function(this: Option, complete: (value: string, description: string) => void, options: OptionsMap) { |
| 61 | + complete('3000', 'Development server port'); |
| 62 | + complete('8080', 'Alternative port'); |
| 63 | + }, |
| 64 | + host: function(this: Option, complete: (value: string, description: string) => void, options: OptionsMap) { |
| 65 | + complete('localhost', 'Localhost'); |
| 66 | + complete('0.0.0.0', 'All interfaces'); |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + options: { |
| 72 | + config: function(this: Option, complete: (value: string, description: string) => void, options: OptionsMap) { |
| 73 | + complete('vite.config.ts', 'Vite config file'); |
| 74 | + complete('vite.config.js', 'Vite config file'); |
| 75 | + }, |
| 76 | + mode: function(this: Option, complete: (value: string, description: string) => void, options: OptionsMap) { |
| 77 | + complete('development', 'Development mode'); |
| 78 | + complete('production', 'Production mode'); |
| 79 | + }, |
| 80 | + logLevel: function(this: Option, complete: (value: string, description: string) => void, options: OptionsMap) { |
| 81 | + complete('info', 'Info level'); |
| 82 | + complete('warn', 'Warn level'); |
| 83 | + complete('error', 'Error level'); |
| 84 | + complete('silent', 'Silent level'); |
| 85 | + }, |
| 86 | + }, |
| 87 | +}); |
84 | 88 |
|
85 | 89 | cli.parse(); |
0 commit comments