|
1 | | -// import fs from "fs/promises"; |
2 | | -// import cac from "cac"; |
3 | | -// import { |
4 | | -// Callback, |
5 | | -// Completion, |
6 | | -// flagMap, |
7 | | -// Positional, |
8 | | -// positionalMap, |
9 | | -// } from "./shared"; |
10 | | -// import path from "path"; |
11 | | -// import tab from "./cac"; |
| 1 | +import cac from "cac"; |
| 2 | +import tab from "./src/cac"; |
12 | 3 |
|
13 | | -// const cli = cac("vite"); // Using 'vite' as the CLI tool name |
| 4 | +const cli = cac("vite"); |
14 | 5 |
|
15 | | -// // Custom converters (placeholders) |
16 | | -// function convertBase(value) { |
17 | | -// return value; |
18 | | -// } |
| 6 | +cli |
| 7 | + .option("-c, --config <file>", `Use specified config file`) |
| 8 | + .option("-m, --mode <mode>", `Set env mode`) |
| 9 | + .option("-l, --logLevel <level>", `info | warn | error | silent`); |
19 | 10 |
|
20 | | -// function convertHost(value) { |
21 | | -// return value; |
22 | | -// } |
| 11 | +cli |
| 12 | + .command("dev", "Start dev server") |
| 13 | + .option("--host [host]", `Specify hostname`) |
| 14 | + .option("--port <port>", `Specify port`) |
| 15 | + .action((options) => {}); |
23 | 16 |
|
24 | | -// // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/cli.ts |
25 | | -// // Global options |
26 | | -// cli |
27 | | -// .option("-c, --config <file>", `[string] use specified config file`) |
28 | | -// .option("--base <path>", `[string] public base path (default: /)`, { |
29 | | -// type: [convertBase], |
30 | | -// }) |
31 | | -// .option("-l, --logLevel <level>", `[string] info | warn | error | silent`) |
32 | | -// .option("--clearScreen", `[boolean] allow/disable clear screen when logging`) |
33 | | -// .option("-d, --debug [feat]", `[string | boolean] show debug logs`) |
34 | | -// .option("-f, --filter <filter>", `[string] filter debug logs`) |
35 | | -// .option("-m, --mode <mode>", `[string] set env mode`); |
| 17 | +cli |
| 18 | + .command("dev build", "Build project") |
| 19 | + .action((options) => {}); |
36 | 20 |
|
37 | | -// // Dev command |
38 | | -// cli |
39 | | -// .command("[root]", "start dev server") // default command |
40 | | -// .alias("serve") // the command is called 'serve' in Vite's API |
41 | | -// .alias("dev") // alias to align with the script name |
42 | | -// .option("--host [host]", `[string] specify hostname`, { type: [convertHost] }) |
43 | | -// .option("--port <port>", `[number] specify port`) |
44 | | -// .option("--open [path]", `[boolean | string] open browser on startup`) |
45 | | -// .option("--cors", `[boolean] enable CORS`) |
46 | | -// .option("--strictPort", `[boolean] exit if specified port is already in use`) |
47 | | -// .option( |
48 | | -// "--force", |
49 | | -// `[boolean] force the optimizer to ignore the cache and re-bundle` |
50 | | -// ) |
51 | | -// .action((root, options) => { |
52 | | -// console.log(`Starting dev server at ${root || "."} with options:`, options); |
53 | | -// }); |
54 | | -// // Build positional completions for each command using command.args |
55 | | -// for (const c of [cli.globalCommand, ...cli.commands]) { |
56 | | -// // Handle options |
57 | | -// for (const o of [...cli.globalCommand.options, ...c.options]) { |
58 | | -// const optionKey = `${c.name} ${o.name}`; |
| 21 | +cli |
| 22 | + .command("lint [...files]", "Lint project") |
| 23 | + .action((files, options) => {}); |
59 | 24 |
|
60 | | -// if (o.rawName.includes("--logLevel <level>")) { |
61 | | -// // Completion for --logLevel |
62 | | -// flagMap.set(optionKey, async (previousArgs, toComplete) => { |
63 | | -// return [ |
64 | | -// { action: "info", description: "Info level logging" }, |
65 | | -// { action: "warn", description: "Warning level logging" }, |
66 | | -// { action: "error", description: "Error level logging" }, |
67 | | -// { action: "silent", description: "No logging" }, |
68 | | -// ].filter((comp) => comp.action.startsWith(toComplete)); |
69 | | -// }); |
70 | | -// } |
| 25 | +const completion = await tab(cli); |
71 | 26 |
|
72 | | -// if (o.rawName.includes("--mode <mode>")) { |
73 | | -// // Completion for --mode |
74 | | -// flagMap.set(optionKey, async (previousArgs, toComplete) => { |
75 | | -// return [ |
76 | | -// { action: "production", description: "Production mode" }, |
77 | | -// { action: "development", description: "Development mode" }, |
78 | | -// { action: "staging", description: "Staging mode" }, |
79 | | -// ].filter((comp) => comp.action.startsWith(toComplete)); |
80 | | -// }); |
81 | | -// } |
| 27 | +for (const command of completion.commands.values()) { |
| 28 | + if (command.name === 'lint') { |
| 29 | + command.handler = () => { |
| 30 | + return [ |
| 31 | + { value: 'main.ts', description: 'Main file' }, |
| 32 | + { value: 'index.ts', description: 'Index file' }, |
| 33 | + ]; |
| 34 | + }; |
| 35 | + } |
82 | 36 |
|
83 | | -// if (o.rawName.includes("--port <port>")) { |
84 | | -// // Completion for --port |
85 | | -// flagMap.set(optionKey, async (previousArgs, toComplete) => { |
86 | | -// return [ |
87 | | -// { action: "3000", description: "Development server port" }, |
88 | | -// { action: "8080", description: "Alternative port" }, |
89 | | -// { action: "80", description: "HTTP port" }, |
90 | | -// { action: "443", description: "HTTPS port" }, |
91 | | -// { action: "5000", description: "Common backend port" }, |
92 | | -// ].filter((comp) => comp.action.startsWith(toComplete)); |
93 | | -// }); |
94 | | -// } |
| 37 | + for (const [o, config] of command.options.entries()) { |
| 38 | + if (o === '--port') { |
| 39 | + config.handler = () => { |
| 40 | + return [ |
| 41 | + { value: '3000', description: 'Development server port' }, |
| 42 | + { value: '8080', description: 'Alternative port' }, |
| 43 | + ]; |
| 44 | + }; |
| 45 | + } |
| 46 | + if (o === '--host') { |
| 47 | + config.handler = () => { |
| 48 | + return [ |
| 49 | + { value: 'localhost', description: 'Localhost' }, |
| 50 | + { value: '0.0.0.0', description: 'All interfaces' }, |
| 51 | + ]; |
| 52 | + }; |
| 53 | + } |
| 54 | + if (o === '--config') { |
| 55 | + config.handler = () => { |
| 56 | + return [ |
| 57 | + { value: 'vite.config.ts', description: 'Vite config file' }, |
| 58 | + { value: 'vite.config.js', description: 'Vite config file' }, |
| 59 | + ]; |
| 60 | + }; |
| 61 | + } |
| 62 | + if (o === '--mode') { |
| 63 | + config.handler = () => { |
| 64 | + return [ |
| 65 | + { value: 'development', description: 'Development mode' }, |
| 66 | + { value: 'production', description: 'Production mode' }, |
| 67 | + ]; |
| 68 | + }; |
| 69 | + } |
| 70 | + if (o === '--logLevel') { |
| 71 | + config.handler = () => { |
| 72 | + return [ |
| 73 | + { value: 'info', description: 'Info level' }, |
| 74 | + { value: 'warn', description: 'Warn level' }, |
| 75 | + { value: 'error', description: 'Error level' }, |
| 76 | + { value: 'silent', description: 'Silent level' }, |
| 77 | + ]; |
| 78 | + }; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
95 | 82 |
|
96 | | -// if (o.rawName.includes("--host [host]")) { |
97 | | -// // Completion for --host |
98 | | -// flagMap.set(optionKey, async (previousArgs, toComplete) => { |
99 | | -// return [ |
100 | | -// { action: "localhost", description: "Localhost" }, |
101 | | -// { action: "0.0.0.0", description: "All interfaces" }, |
102 | | -// { action: "127.0.0.1", description: "Loopback interface" }, |
103 | | -// ].filter((comp) => comp.action.startsWith(toComplete)); |
104 | | -// }); |
105 | | -// } |
106 | | - |
107 | | -// if (o.rawName.includes("--config <file>")) { |
108 | | -// // Completion for --config |
109 | | -// flagMap.set(optionKey, async (previousArgs, toComplete) => { |
110 | | -// const configFiles = ["vite.config.ts", "vite.config.js"].filter( |
111 | | -// (file) => file.startsWith(toComplete) |
112 | | -// ); |
113 | | -// return configFiles.map((file) => ({ action: file })); |
114 | | -// }); |
115 | | -// } |
116 | | - |
117 | | -// // Add more option completions as needed |
118 | | -// } |
119 | | - |
120 | | -// // Handle positional arguments |
121 | | -// if (c.args && c.args.length > 0) { |
122 | | -// const positionals = c.args.map((arg) => ({ |
123 | | -// required: arg.required, |
124 | | -// variadic: arg.variadic, |
125 | | -// value: arg.value, |
126 | | -// completion: async (previousArgs, toComplete) => { |
127 | | -// if (arg.value === "root") { |
128 | | -// return [ |
129 | | -// { action: "src/", description: "💣️.sh loves vite!" }, |
130 | | -// { action: "./", description: "This one is better." }, |
131 | | -// ]; |
132 | | -// } |
133 | | -// return []; |
134 | | -// }, |
135 | | -// })); |
136 | | - |
137 | | -// positionalMap.set(c.name, positionals); |
138 | | -// } |
139 | | -// } |
140 | | - |
141 | | -// tab(cli); |
142 | | - |
143 | | -// cli.parse(); |
| 83 | +cli.parse(); |
0 commit comments