@vite-hub/cli 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,38 @@
1
1
  # @vite-hub/cli
2
2
 
3
- Reserved package name for ViteHub.
3
+ <p>
4
+ <a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
5
+ <img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
6
+ <img alt="CLI" src="https://img.shields.io/badge/CLI-package%20workflows-18181b?style=flat-square">
7
+ </p>
8
+
9
+ `@vite-hub/cli` loads the local Vite config, collects package-contributed commands, and runs them from `vitehub`.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pnpm add -D @vite-hub/cli
15
+ ```
16
+
17
+ ## Minimal API
18
+
19
+ ```sh
20
+ pnpm vitehub --help
21
+ pnpm vitehub agent eval
22
+ ```
23
+
24
+ ```ts
25
+ // scripts/vitehub.ts
26
+ import { runViteHubCli } from "@vite-hub/cli"
27
+
28
+ const exitCode = await runViteHubCli({
29
+ args: ["agent", "eval"],
30
+ cwd: process.cwd(),
31
+ })
32
+ ```
33
+
34
+ ## Vite
35
+
36
+ Commands come from Vite plugins, so package CLIs can stay package-owned. The first public namespace is `vitehub agent eval` from [`@vite-hub/agent`](../agent/README.md), backed by [Evalite](https://v1.evalite.dev/).
37
+
38
+ Learn more at [vitehub.dev](https://vitehub.dev).
@@ -0,0 +1,38 @@
1
+ import { ResolvedConfig } from "vite";
2
+
3
+ //#region src/index.d.ts
4
+ interface ViteHubCliSpawnResult {
5
+ exitCode: number | null;
6
+ signal?: NodeJS.Signals | null;
7
+ }
8
+ interface ViteHubCliSpawnOptions {
9
+ cwd?: string;
10
+ env?: NodeJS.ProcessEnv;
11
+ stderr?: "inherit" | "pipe";
12
+ stdout?: "inherit" | "pipe";
13
+ }
14
+ type ViteHubCliSpawn = (command: string, args: string[], options?: ViteHubCliSpawnOptions) => Promise<ViteHubCliSpawnResult>;
15
+ interface ViteHubCliContext {
16
+ cwd: string;
17
+ env: NodeJS.ProcessEnv;
18
+ rootDir: string;
19
+ spawn: ViteHubCliSpawn;
20
+ stderr: {
21
+ write: (chunk: string | Uint8Array) => unknown;
22
+ };
23
+ stdout: {
24
+ write: (chunk: string | Uint8Array) => unknown;
25
+ };
26
+ }
27
+ interface RunViteHubCliOptions {
28
+ args?: string[];
29
+ cwd?: string;
30
+ env?: NodeJS.ProcessEnv;
31
+ loadConfig?: (rootDir: string) => Promise<Pick<ResolvedConfig, "plugins" | "root">>;
32
+ spawn?: ViteHubCliSpawn;
33
+ stderr?: ViteHubCliContext["stderr"];
34
+ stdout?: ViteHubCliContext["stdout"];
35
+ }
36
+ declare function runViteHubCli(options?: RunViteHubCliOptions): Promise<number>;
37
+ //#endregion
38
+ export { RunViteHubCliOptions, runViteHubCli };
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env node
2
+ import { spawn } from "node:child_process";
3
+ import { realpathSync } from "node:fs";
4
+ import process from "node:process";
5
+ import { fileURLToPath } from "node:url";
6
+ import { resolve } from "pathe";
7
+ //#region src/index.ts
8
+ function defaultSpawn(command, args, options = {}) {
9
+ return new Promise((resolveResult, reject) => {
10
+ const child = spawn(command, args, {
11
+ cwd: options.cwd,
12
+ env: options.env,
13
+ shell: process.platform === "win32",
14
+ stdio: [
15
+ "inherit",
16
+ options.stdout || "inherit",
17
+ options.stderr || "inherit"
18
+ ]
19
+ });
20
+ child.on("error", reject);
21
+ child.on("close", (exitCode, signal) => resolveResult({
22
+ exitCode,
23
+ signal
24
+ }));
25
+ });
26
+ }
27
+ async function loadViteConfig(rootDir) {
28
+ const { resolveConfig } = await import("vite");
29
+ return await resolveConfig({ root: rootDir }, "serve", "development");
30
+ }
31
+ async function resolveContributor(value) {
32
+ return typeof value === "function" ? await value() : value;
33
+ }
34
+ async function collectViteHubCliNamespaces(plugins) {
35
+ const namespaces = /* @__PURE__ */ new Map();
36
+ for (const plugin of plugins) {
37
+ if (!plugin || typeof plugin !== "object") continue;
38
+ const metadata = plugin.vitehub;
39
+ const contributor = await resolveContributor(metadata?.cli);
40
+ if (!contributor) continue;
41
+ for (const namespace of contributor.namespaces) {
42
+ const existing = namespaces.get(namespace.name);
43
+ if (!existing) {
44
+ namespaces.set(namespace.name, {
45
+ ...namespace,
46
+ features: [...namespace.features]
47
+ });
48
+ continue;
49
+ }
50
+ const features = new Map(existing.features.map((feature) => [feature.name, feature]));
51
+ for (const feature of namespace.features) features.set(feature.name, feature);
52
+ existing.features = [...features.values()];
53
+ }
54
+ }
55
+ return [...namespaces.values()];
56
+ }
57
+ function writeRootHelp(namespaces, stdout) {
58
+ stdout.write([
59
+ "Usage: vitehub <namespace> <feature> [args...]",
60
+ "",
61
+ "Available namespaces:",
62
+ ...namespaces.map((namespace) => ` ${namespace.name.padEnd(12)} ${namespace.description || ""}`.trimEnd()),
63
+ ""
64
+ ].join("\n"));
65
+ }
66
+ function writeNamespaceHelp(namespace, stdout) {
67
+ stdout.write([
68
+ `Usage: vitehub ${namespace.name} <feature> [args...]`,
69
+ "",
70
+ namespace.description || "",
71
+ "",
72
+ "Available features:",
73
+ ...namespace.features.map((feature) => ` ${feature.name.padEnd(12)} ${feature.description || ""}`.trimEnd()),
74
+ ""
75
+ ].filter(Boolean).join("\n"));
76
+ }
77
+ function isRootHelp(args) {
78
+ return args[0] === "-h" || args[0] === "--help";
79
+ }
80
+ async function runViteHubCli(options = {}) {
81
+ const args = options.args || process.argv.slice(2);
82
+ const cwd = options.cwd || process.cwd();
83
+ const env = options.env || process.env;
84
+ const stdout = options.stdout || process.stdout;
85
+ const stderr = options.stderr || process.stderr;
86
+ const config = await (options.loadConfig || loadViteConfig)(cwd);
87
+ const rootDir = resolve(config.root || cwd);
88
+ const namespaces = await collectViteHubCliNamespaces(config.plugins);
89
+ const context = {
90
+ cwd,
91
+ env,
92
+ rootDir,
93
+ spawn: options.spawn || defaultSpawn,
94
+ stderr,
95
+ stdout
96
+ };
97
+ if (!args.length || isRootHelp(args)) {
98
+ writeRootHelp(namespaces, stdout);
99
+ return 0;
100
+ }
101
+ const namespaceName = args[0];
102
+ const namespace = namespaces.find((item) => item.name === namespaceName);
103
+ if (!namespace) {
104
+ stderr.write(`Unknown ViteHub CLI namespace: ${namespaceName}\n`);
105
+ writeRootHelp(namespaces, stderr);
106
+ return 1;
107
+ }
108
+ const featureName = args[1];
109
+ if (!featureName || args[1] === "-h" || args[1] === "--help") {
110
+ writeNamespaceHelp(namespace, stdout);
111
+ return 0;
112
+ }
113
+ const feature = namespace.features.find((item) => item.name === featureName);
114
+ if (!feature) {
115
+ stderr.write(`Unknown ViteHub CLI feature: ${namespace.name} ${featureName}\n`);
116
+ writeNamespaceHelp(namespace, stderr);
117
+ return 1;
118
+ }
119
+ const result = await feature.run(args.slice(2), context);
120
+ return typeof result === "number" ? result : 0;
121
+ }
122
+ function isCliEntrypoint() {
123
+ if (!process.argv[1]) return false;
124
+ try {
125
+ return realpathSync(fileURLToPath(import.meta.url)) === realpathSync(process.argv[1]);
126
+ } catch {
127
+ return false;
128
+ }
129
+ }
130
+ if (isCliEntrypoint()) runViteHubCli().then((exitCode) => {
131
+ process.exit(exitCode);
132
+ }).catch((error) => {
133
+ console.error(error instanceof Error ? error.message : error);
134
+ process.exit(1);
135
+ });
136
+ //#endregion
137
+ export { runViteHubCli };
package/package.json CHANGED
@@ -1,9 +1,54 @@
1
1
  {
2
2
  "name": "@vite-hub/cli",
3
- "version": "0.0.0",
4
- "description": "Reserved package name for ViteHub.",
3
+ "version": "0.0.1",
4
+ "description": "Command-line workflows for ViteHub projects.",
5
+ "author": "onmax <maximogarciamtnez@gmail.com>",
5
6
  "license": "Apache-2.0",
6
- "publishConfig": {
7
- "access": "public"
7
+ "homepage": "https://github.com/vite-hub/vitehub#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/vite-hub/vitehub.git",
11
+ "directory": "packages/cli"
12
+ },
13
+ "bugs": "https://github.com/vite-hub/vitehub/issues",
14
+ "keywords": [
15
+ "vitehub",
16
+ "cli",
17
+ "agent",
18
+ "eval"
19
+ ],
20
+ "bin": {
21
+ "vitehub": "dist/index.js"
22
+ },
23
+ "sideEffects": false,
24
+ "type": "module",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": "./dist/index.js",
28
+ "./package.json": "./package.json"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "package.json"
33
+ ],
34
+ "dependencies": {
35
+ "pathe": "^2.0.3"
36
+ },
37
+ "peerDependencies": {
38
+ "vite": ">=8.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^25.6.0",
42
+ "tsdown": "^0.21.8",
43
+ "typescript": "^6.0.2",
44
+ "vite": "^8.0.8",
45
+ "vitest": "^4.1.4",
46
+ "@vite-hub/agent": "0.0.1"
47
+ },
48
+ "scripts": {
49
+ "build": "tsdown --tsconfig tsconfig.build.json",
50
+ "dev": "tsdown --watch",
51
+ "typecheck": "tsc --noEmit -p ./tsconfig.json",
52
+ "test": "pnpm --filter @vite-hub/internal build && pnpm --filter @vite-hub/agent build && pnpm --dir . build && vitest run"
8
53
  }
9
- }
54
+ }