Skip to content

Commit dc106b0

Browse files
committed
citty demo
1 parent 4c675c4 commit dc106b0

File tree

6 files changed

+142
-85
lines changed

6 files changed

+142
-85
lines changed

citty.ts

Whitespace-only changes.

demo.ts renamed to demo.cac.ts

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,6 @@ import {
1010
import path from "path";
1111
import tab from "./cac";
1212

13-
// const cli = cac("cac");
14-
15-
// cli.option("--type [type]", "Choose a project type", {
16-
// default: "node",
17-
// });
18-
// cli.option("--name <name>", "Provide your name");
19-
// cli.option("--name23 <name>", "Provide your name23");
20-
21-
// cli.command("start").option("--port <port>", "your port");
22-
23-
// cli.command("help");
24-
25-
// cli.command("help config").option("--foo", "foo option");
26-
27-
// cli.command("test dev").option("--foo", "foo option");
28-
29-
// cli.command("deploy <environment> [version] [...files]");
30-
31-
// cli.version("0.0.0");
32-
// cli.help();
33-
34-
// for (const c of [cli.globalCommand, ...cli.commands]) {
35-
// for (const o of [...cli.globalCommand.options, ...c.options]) {
36-
// if (o.rawName === "--type [type]") {
37-
// flagMap.set(`${c.name} ${o.name}`, () => {
38-
// return [
39-
// {
40-
// action: "standalone",
41-
// description: "Standalone type",
42-
// },
43-
// {
44-
// action: "complex",
45-
// description: "Complex type",
46-
// },
47-
// ];
48-
// });
49-
// }
50-
// }
51-
52-
// // console.log(c.args);
53-
// if (c.name === "deploy") {
54-
// const positionals: Positional[] = [];
55-
// positionalMap.set(c.name, positionals);
56-
// for (const arg of c.args) {
57-
// const completion: Callback = async () => {
58-
// if (arg.value === "environment") {
59-
// return [
60-
// {
61-
// action: "node",
62-
// },
63-
// {
64-
// action: "deno",
65-
// },
66-
// ];
67-
// }
68-
// if (arg.value === "version") {
69-
// return [
70-
// {
71-
// action: "0.0.0",
72-
// },
73-
// {
74-
// action: "0.0.1",
75-
// },
76-
// ];
77-
// }
78-
// if (arg.value === "files") {
79-
// const currentDir = process.cwd();
80-
// const files = await fs.readdir(currentDir);
81-
// const jsonFiles = files.filter(
82-
// (file) => path.extname(file).toLowerCase() === ".json",
83-
// );
84-
// return jsonFiles.map((file) => ({ action: file }));
85-
// }
86-
// return [];
87-
// };
88-
89-
// positionals.push({
90-
// required: arg.required,
91-
// variadic: arg.variadic,
92-
// completion,
93-
// });
94-
// }
95-
// }
96-
// }
97-
9813
const cli = cac("vite"); // Using 'vite' as the CLI tool name
9914

10015
// Custom converters (placeholders)

demo.citty.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { defineCommand, createMain } from "citty";
2+
// import { tab } from "./citty";
3+
const main = defineCommand({
4+
meta: {
5+
name: "vite",
6+
description: "Vite CLI tool",
7+
},
8+
args: {
9+
config: {
10+
type: "string",
11+
description: "Use specified config file",
12+
alias: "c",
13+
},
14+
base: {
15+
type: "string",
16+
description: "Public base path (default: /)",
17+
},
18+
logLevel: {
19+
type: "string",
20+
description: "info | warn | error | silent",
21+
alias: "l",
22+
},
23+
clearScreen: {
24+
type: "boolean",
25+
description: "Allow/disable clear screen when logging",
26+
},
27+
debug: {
28+
type: "string",
29+
description: "Show debug logs",
30+
alias: "d",
31+
},
32+
filter: {
33+
type: "string",
34+
description: "Filter debug logs",
35+
alias: "f",
36+
},
37+
mode: {
38+
type: "string",
39+
description: "Set env mode",
40+
alias: "m",
41+
},
42+
},
43+
run(ctx) {
44+
if (ctx.args._?.[0] !== "complete" && devCommand.run) {
45+
devCommand.run(ctx);
46+
} else if (!devCommand.run) {
47+
console.error("Error: dev command is not defined.");
48+
}
49+
},
50+
});
51+
52+
const devCommand = defineCommand({
53+
meta: {
54+
name: "dev",
55+
description: "Start dev server",
56+
},
57+
args: {
58+
root: {
59+
type: "positional",
60+
description: "Root directory",
61+
required: false,
62+
default: ".",
63+
},
64+
host: {
65+
type: "string",
66+
description: "Specify hostname",
67+
},
68+
port: {
69+
type: "string",
70+
description: "Specify port",
71+
},
72+
open: {
73+
type: "boolean",
74+
description: "Open browser on startup",
75+
},
76+
cors: {
77+
type: "boolean",
78+
description: "Enable CORS",
79+
},
80+
strictPort: {
81+
type: "boolean",
82+
description: "Exit if specified port is already in use",
83+
},
84+
force: {
85+
type: "boolean",
86+
description: "Force the optimizer to ignore the cache and re-bundle",
87+
},
88+
},
89+
run({ args }) {
90+
const { root, port, ...options } = args;
91+
const parsedPort = port ? parseInt(port, 10) : undefined;
92+
93+
if (args._?.[0] !== "complete") {
94+
if (!root || root === ".") {
95+
console.log("Suggested root directories:");
96+
console.log("- src/");
97+
console.log("- ./");
98+
}
99+
100+
const formattedOptions = { ...options, port: parsedPort };
101+
if (formattedOptions._) {
102+
formattedOptions["--"] = formattedOptions._;
103+
delete formattedOptions._;
104+
}
105+
106+
const cleanedOptions = Object.fromEntries(
107+
Object.entries(formattedOptions).filter(([_, v]) => v !== undefined)
108+
);
109+
110+
console.log(
111+
`Starting dev server at ${root || "."} with options:`,
112+
cleanedOptions
113+
);
114+
}
115+
},
116+
});
117+
118+
main.subCommands = {
119+
dev: devCommand,
120+
};
121+
122+
// tab(main);
123+
124+
const cli = createMain(main);
125+
cli();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@types/node": "^22.7.4",
1515
"cac": "^6.7.14",
16+
"citty": "^0.1.6",
1617
"tsx": "^4.19.1",
1718
"vitest": "^2.1.3"
1819
},

pnpm-lock.yaml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

powershell.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ShellCompDirective } from "./shared";
22

3+
// TODO: issue with -- -- completions
4+
35
export function generate(
46
name: string,
57
exec: string,

0 commit comments

Comments
 (0)