@vite-hub/cli 0.0.1 → 0.0.3
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 +7 -1
- package/dist/index.d.ts +3 -7
- package/dist/index.js +198 -26
- package/package.json +29 -26
package/README.md
CHANGED
|
@@ -18,7 +18,13 @@ pnpm add -D @vite-hub/cli
|
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
20
|
pnpm vitehub --help
|
|
21
|
+
pnpm vitehub agent info
|
|
22
|
+
pnpm vitehub agent dev
|
|
21
23
|
pnpm vitehub agent eval
|
|
24
|
+
pnpm vitehub db generate
|
|
25
|
+
pnpm vitehub db migrate
|
|
26
|
+
pnpm vitehub workspace dev docs
|
|
27
|
+
pnpm vitehub provision run --provider cloudflare --dry-run
|
|
22
28
|
```
|
|
23
29
|
|
|
24
30
|
```ts
|
|
@@ -33,6 +39,6 @@ const exitCode = await runViteHubCli({
|
|
|
33
39
|
|
|
34
40
|
## Vite
|
|
35
41
|
|
|
36
|
-
Commands come from Vite plugins, so package CLIs
|
|
42
|
+
Commands come from the active Vite plugins, so package CLIs stay package-owned. The Agent integration contributes `agent info`, `agent dev`, and Evalite-backed `agent eval`; Database contributes `db generate` and `db migrate`; Workspace contributes `workspace dev`. The CLI also owns `provision run` for executing package-contributed provider provisioning steps.
|
|
37
43
|
|
|
38
44
|
Learn more at [vitehub.dev](https://vitehub.dev).
|
package/dist/index.d.ts
CHANGED
|
@@ -12,11 +12,7 @@ interface ViteHubCliSpawnOptions {
|
|
|
12
12
|
stdout?: "inherit" | "pipe";
|
|
13
13
|
}
|
|
14
14
|
type ViteHubCliSpawn = (command: string, args: string[], options?: ViteHubCliSpawnOptions) => Promise<ViteHubCliSpawnResult>;
|
|
15
|
-
interface
|
|
16
|
-
cwd: string;
|
|
17
|
-
env: NodeJS.ProcessEnv;
|
|
18
|
-
rootDir: string;
|
|
19
|
-
spawn: ViteHubCliSpawn;
|
|
15
|
+
interface ViteHubCliStreams {
|
|
20
16
|
stderr: {
|
|
21
17
|
write: (chunk: string | Uint8Array) => unknown;
|
|
22
18
|
};
|
|
@@ -30,8 +26,8 @@ interface RunViteHubCliOptions {
|
|
|
30
26
|
env?: NodeJS.ProcessEnv;
|
|
31
27
|
loadConfig?: (rootDir: string) => Promise<Pick<ResolvedConfig, "plugins" | "root">>;
|
|
32
28
|
spawn?: ViteHubCliSpawn;
|
|
33
|
-
stderr?:
|
|
34
|
-
stdout?:
|
|
29
|
+
stderr?: ViteHubCliStreams["stderr"];
|
|
30
|
+
stdout?: ViteHubCliStreams["stdout"];
|
|
35
31
|
}
|
|
36
32
|
declare function runViteHubCli(options?: RunViteHubCliOptions): Promise<number>;
|
|
37
33
|
//#endregion
|
package/dist/index.js
CHANGED
|
@@ -3,31 +3,9 @@ import { spawn } from "node:child_process";
|
|
|
3
3
|
import { realpathSync } from "node:fs";
|
|
4
4
|
import process from "node:process";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { resolve } from "pathe";
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
}
|
|
6
|
+
import { dirname, resolve } from "pathe";
|
|
7
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
8
|
+
//#region ../internal/dist/cli.js
|
|
31
9
|
async function resolveContributor(value) {
|
|
32
10
|
return typeof value === "function" ? await value() : value;
|
|
33
11
|
}
|
|
@@ -54,6 +32,200 @@ async function collectViteHubCliNamespaces(plugins) {
|
|
|
54
32
|
}
|
|
55
33
|
return [...namespaces.values()];
|
|
56
34
|
}
|
|
35
|
+
async function collectViteHubProvisionSteps(plugins) {
|
|
36
|
+
const steps = /* @__PURE__ */ new Map();
|
|
37
|
+
for (const plugin of plugins) {
|
|
38
|
+
if (!plugin || typeof plugin !== "object") continue;
|
|
39
|
+
const contributor = await resolveContributor(plugin.vitehub?.cli);
|
|
40
|
+
for (const step of contributor?.provision ?? []) steps.set(step.id, step);
|
|
41
|
+
}
|
|
42
|
+
return [...steps.values()];
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region ../internal/dist/provision-state.js
|
|
46
|
+
const PROVISION_STATE_FILE = ".vitehub/provision.json";
|
|
47
|
+
function getProvisionStatePath(rootDir) {
|
|
48
|
+
return resolve(rootDir, PROVISION_STATE_FILE);
|
|
49
|
+
}
|
|
50
|
+
async function readProvisionState(rootDir) {
|
|
51
|
+
try {
|
|
52
|
+
return parseProvisionState(await readFile(getProvisionStatePath(rootDir), "utf8"));
|
|
53
|
+
} catch {
|
|
54
|
+
return {};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function writeProvisionState(rootDir, next) {
|
|
58
|
+
const path = getProvisionStatePath(rootDir);
|
|
59
|
+
const merged = mergeProvisionState(await readProvisionState(rootDir), next);
|
|
60
|
+
await mkdir(dirname(path), { recursive: true });
|
|
61
|
+
await writeFile(path, `${JSON.stringify(sortState(merged), null, 2)}\n`, "utf8");
|
|
62
|
+
}
|
|
63
|
+
function mergeProvisionState(base, next) {
|
|
64
|
+
const merged = structuredClone(base);
|
|
65
|
+
for (const provider of Object.keys(next)) {
|
|
66
|
+
const categories = next[provider];
|
|
67
|
+
if (!categories) continue;
|
|
68
|
+
const target = merged[provider] ??= {};
|
|
69
|
+
for (const [category, ids] of Object.entries(categories)) target[category] = {
|
|
70
|
+
...target[category],
|
|
71
|
+
...ids
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return merged;
|
|
75
|
+
}
|
|
76
|
+
function parseProvisionState(raw) {
|
|
77
|
+
const parsed = JSON.parse(raw);
|
|
78
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
79
|
+
}
|
|
80
|
+
function sortState(state) {
|
|
81
|
+
return sortKeys(state);
|
|
82
|
+
}
|
|
83
|
+
function sortKeys(value) {
|
|
84
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return value;
|
|
85
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, sortKeys(value[key])]));
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region ../internal/dist/provision.js
|
|
89
|
+
function resolveCloudflareProvisionConfig(env) {
|
|
90
|
+
const accountId = trimmedEnv(env.CLOUDFLARE_ACCOUNT_ID);
|
|
91
|
+
const token = trimmedEnv(env.CLOUDFLARE_API_TOKEN);
|
|
92
|
+
if (!accountId || !token) return;
|
|
93
|
+
return {
|
|
94
|
+
accountId,
|
|
95
|
+
token
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function resolveVercelProvisionConfig(env) {
|
|
99
|
+
const token = trimmedEnv(env.VERCEL_TOKEN);
|
|
100
|
+
if (!token) return;
|
|
101
|
+
return {
|
|
102
|
+
token,
|
|
103
|
+
teamId: trimmedEnv(env.VERCEL_TEAM_ID) ?? trimmedEnv(env.VERCEL_ORG_ID)
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function trimmedEnv(value) {
|
|
107
|
+
const trimmed = value?.trim();
|
|
108
|
+
return trimmed ? trimmed : void 0;
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/provision.ts
|
|
112
|
+
function parseArgs(args) {
|
|
113
|
+
const parsed = {
|
|
114
|
+
dryRun: false,
|
|
115
|
+
help: false
|
|
116
|
+
};
|
|
117
|
+
for (let index = 0; index < args.length; index++) {
|
|
118
|
+
const arg = args[index];
|
|
119
|
+
if (arg === "-h" || arg === "--help") parsed.help = true;
|
|
120
|
+
else if (arg === "--dry-run") parsed.dryRun = true;
|
|
121
|
+
else if (arg === "--provider") parsed.provider = args[++index];
|
|
122
|
+
else if (arg?.startsWith("--provider=")) parsed.provider = arg.slice(11);
|
|
123
|
+
}
|
|
124
|
+
return parsed;
|
|
125
|
+
}
|
|
126
|
+
function writeUsage(context) {
|
|
127
|
+
context.stdout.write([
|
|
128
|
+
"Usage: vitehub provision run --provider <cloudflare|vercel> [--dry-run]",
|
|
129
|
+
"",
|
|
130
|
+
"Idempotently creates missing provider resources for the app's Definitions.",
|
|
131
|
+
"Never deletes or mutates existing resources.",
|
|
132
|
+
"",
|
|
133
|
+
"Options:",
|
|
134
|
+
" --provider <name> Target provider: cloudflare or vercel (required).",
|
|
135
|
+
" --dry-run Print the plan without applying it.",
|
|
136
|
+
" -h, --help Show this help.",
|
|
137
|
+
""
|
|
138
|
+
].join("\n"));
|
|
139
|
+
}
|
|
140
|
+
async function runProvision(args, context, options) {
|
|
141
|
+
const parsed = parseArgs(args);
|
|
142
|
+
if (parsed.help) {
|
|
143
|
+
writeUsage(context);
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
if (parsed.provider !== "cloudflare" && parsed.provider !== "vercel") {
|
|
147
|
+
context.stderr.write("Provision requires --provider cloudflare|vercel.\n");
|
|
148
|
+
writeUsage(context);
|
|
149
|
+
return 1;
|
|
150
|
+
}
|
|
151
|
+
const provider = parsed.provider;
|
|
152
|
+
if (!parsed.dryRun) {
|
|
153
|
+
if (!(provider === "cloudflare" ? resolveCloudflareProvisionConfig(context.env) : resolveVercelProvisionConfig(context.env))) {
|
|
154
|
+
const required = provider === "cloudflare" ? "CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN" : "VERCEL_TOKEN";
|
|
155
|
+
context.stderr.write(`Provision requires ${required} to be set. Dry-run preview does not require credentials (--dry-run).\n`);
|
|
156
|
+
return 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const steps = (await options.collectSteps()).filter((step) => step.provider === provider);
|
|
160
|
+
const provisionContext = {
|
|
161
|
+
env: context.env,
|
|
162
|
+
fetch: globalThis.fetch,
|
|
163
|
+
logger: {
|
|
164
|
+
log: (message) => context.stdout.write(`${message}\n`),
|
|
165
|
+
warn: (message) => context.stderr.write(`${message}\n`)
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const actions = [];
|
|
169
|
+
for (const step of steps) for (const action of await step.plan(provisionContext)) actions.push({
|
|
170
|
+
stepId: step.id,
|
|
171
|
+
action
|
|
172
|
+
});
|
|
173
|
+
if (!actions.length) {
|
|
174
|
+
context.stdout.write(`provision: no ${provider} resources to create.\n`);
|
|
175
|
+
return 0;
|
|
176
|
+
}
|
|
177
|
+
for (const { action } of actions) context.stdout.write(`${action.exists ? "exists" : "create"}\t${action.kind}\t${action.name}\n`);
|
|
178
|
+
if (parsed.dryRun) return 0;
|
|
179
|
+
let state = {};
|
|
180
|
+
for (const { action } of actions) {
|
|
181
|
+
const result = await action.apply();
|
|
182
|
+
if (result.ids) state = mergeProvisionState(state, result.ids);
|
|
183
|
+
}
|
|
184
|
+
if (Object.keys(state).length) {
|
|
185
|
+
await writeProvisionState(context.rootDir, state);
|
|
186
|
+
context.stdout.write(`provision: wrote ids to .vitehub/provision.json\n`);
|
|
187
|
+
}
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/index.ts
|
|
192
|
+
function defaultSpawn(command, args, options = {}) {
|
|
193
|
+
return new Promise((resolveResult, reject) => {
|
|
194
|
+
const child = spawn(command, args, {
|
|
195
|
+
cwd: options.cwd,
|
|
196
|
+
env: options.env,
|
|
197
|
+
shell: process.platform === "win32",
|
|
198
|
+
stdio: [
|
|
199
|
+
"inherit",
|
|
200
|
+
options.stdout || "inherit",
|
|
201
|
+
options.stderr || "inherit"
|
|
202
|
+
]
|
|
203
|
+
});
|
|
204
|
+
child.on("error", reject);
|
|
205
|
+
child.on("close", (exitCode, signal) => resolveResult({
|
|
206
|
+
exitCode,
|
|
207
|
+
signal
|
|
208
|
+
}));
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
async function loadViteConfig(rootDir) {
|
|
212
|
+
const { resolveConfig } = await import("vite");
|
|
213
|
+
return await resolveConfig({ root: rootDir }, "serve", "development");
|
|
214
|
+
}
|
|
215
|
+
function createProvisionNamespace(plugins) {
|
|
216
|
+
const collectSteps = () => collectViteHubProvisionSteps(plugins);
|
|
217
|
+
const run = (args, context) => runProvision(args, context, { collectSteps });
|
|
218
|
+
return {
|
|
219
|
+
description: "Idempotently create missing provider resources.",
|
|
220
|
+
features: [{
|
|
221
|
+
description: "Create missing provider resources for the app's Definitions.",
|
|
222
|
+
name: "run",
|
|
223
|
+
run,
|
|
224
|
+
usage: "vitehub provision run --provider <cloudflare|vercel> [--dry-run]"
|
|
225
|
+
}],
|
|
226
|
+
name: "provision"
|
|
227
|
+
};
|
|
228
|
+
}
|
|
57
229
|
function writeRootHelp(namespaces, stdout) {
|
|
58
230
|
stdout.write([
|
|
59
231
|
"Usage: vitehub <namespace> <feature> [args...]",
|
|
@@ -85,7 +257,7 @@ async function runViteHubCli(options = {}) {
|
|
|
85
257
|
const stderr = options.stderr || process.stderr;
|
|
86
258
|
const config = await (options.loadConfig || loadViteConfig)(cwd);
|
|
87
259
|
const rootDir = resolve(config.root || cwd);
|
|
88
|
-
const namespaces = await collectViteHubCliNamespaces(config.plugins);
|
|
260
|
+
const namespaces = [...await collectViteHubCliNamespaces(config.plugins), createProvisionNamespace(config.plugins)];
|
|
89
261
|
const context = {
|
|
90
262
|
cwd,
|
|
91
263
|
env,
|
package/package.json
CHANGED
|
@@ -1,54 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Command-line workflows for ViteHub projects.",
|
|
5
|
-
"
|
|
6
|
-
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agent",
|
|
7
|
+
"cli",
|
|
8
|
+
"eval",
|
|
9
|
+
"vitehub"
|
|
10
|
+
],
|
|
7
11
|
"homepage": "https://github.com/vite-hub/vitehub#readme",
|
|
12
|
+
"bugs": "https://github.com/vite-hub/vitehub/issues",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "onmax <maximogarciamtnez@gmail.com>",
|
|
8
15
|
"repository": {
|
|
9
16
|
"type": "git",
|
|
10
17
|
"url": "git+https://github.com/vite-hub/vitehub.git",
|
|
11
18
|
"directory": "packages/cli"
|
|
12
19
|
},
|
|
13
|
-
"bugs": "https://github.com/vite-hub/vitehub/issues",
|
|
14
|
-
"keywords": [
|
|
15
|
-
"vitehub",
|
|
16
|
-
"cli",
|
|
17
|
-
"agent",
|
|
18
|
-
"eval"
|
|
19
|
-
],
|
|
20
20
|
"bin": {
|
|
21
|
-
"vitehub": "dist/index.js"
|
|
21
|
+
"vitehub": "./dist/index.js"
|
|
22
22
|
},
|
|
23
|
-
"
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"package.json"
|
|
26
|
+
],
|
|
24
27
|
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
25
29
|
"types": "./dist/index.d.ts",
|
|
26
30
|
"exports": {
|
|
27
31
|
".": "./dist/index.js",
|
|
28
32
|
"./package.json": "./package.json"
|
|
29
33
|
},
|
|
30
|
-
"files": [
|
|
31
|
-
"dist",
|
|
32
|
-
"package.json"
|
|
33
|
-
],
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"pathe": "^2.0.3"
|
|
36
36
|
},
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"vite": ">=8.0.0"
|
|
39
|
-
},
|
|
40
37
|
"devDependencies": {
|
|
41
|
-
"@types/node": "^
|
|
42
|
-
"
|
|
43
|
-
"typescript": "^6.0.2",
|
|
38
|
+
"@types/node": "^24.13.3",
|
|
39
|
+
"typescript": "^6.0.3",
|
|
44
40
|
"vite": "^8.0.8",
|
|
41
|
+
"vite-plus": "^0.1.24",
|
|
45
42
|
"vitest": "^4.1.4",
|
|
46
|
-
"@vite-hub/
|
|
43
|
+
"@vite-hub/internal": "0.0.1"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"vite": ">=8.0.0"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=24.0.0"
|
|
47
50
|
},
|
|
48
51
|
"scripts": {
|
|
49
|
-
"build": "
|
|
50
|
-
"dev": "
|
|
52
|
+
"build": "vp pack",
|
|
53
|
+
"dev": "vp pack --watch",
|
|
51
54
|
"typecheck": "tsc --noEmit -p ./tsconfig.json",
|
|
52
|
-
"test": "
|
|
55
|
+
"test": "vp run -t @vite-hub/cli#build && vp test"
|
|
53
56
|
}
|
|
54
57
|
}
|