@vite-hub/cli 0.7.0 → 0.9.0
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 +2 -1
- package/dist/{commands-o_CHzNS9.js → commands-CzNBAR-E.js} +308 -74
- package/dist/console-apps-Cvzc6tY4.js +271 -0
- package/dist/console-auth-BunROd7u.js +2813 -0
- package/dist/{console-env-MPzeOLqu.js → console-env-BCw039oZ.js} +4 -14
- package/dist/{deploy-CGRf9Pq-.js → deploy-DxOKht-B.js} +9 -4
- package/dist/{env-DSPUdOVC.js → env-BvSnsKMg.js} +8 -9
- package/dist/envelope-DXB-0EcA.js +99 -0
- package/dist/help-cache-BI6y5pN8.js +195 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +64 -174
- package/dist/link-B3NuFAyj.js +663 -0
- package/dist/provision-D0ct9RDq.js +1743 -0
- package/dist/provision-DVuxa-yY.js +1389 -0
- package/dist/setup/index.d.ts +5 -0
- package/dist/setup/index.js +8 -7
- package/dist/status-HXNfm9mB.js +599 -0
- package/dist/vercel-ClYz5iI7.js +701 -0
- package/package.json +4 -4
- package/dist/provision-DWEZYLWJ.js +0 -8091
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import { f as createDeployRun, h as hostCliFailed, l as abortable, m as hostCli, t as hasVercelLogin } from "./vercel-ClYz5iI7.js";
|
|
2
|
+
import { s as setupStatus, u as hostFromNitroPreset } from "./status-HXNfm9mB.js";
|
|
3
|
+
import { a as isProvisionRequestError, d as cliErrorDiagnostics, i as createVercelProvisionClient, l as resolveHostCredentials, n as createCloudflareProvisionClient, r as createNetlifyApiClient, t as createCloudflareApiClient, u as runHostCli, w as isRecord } from "./provision-D0ct9RDq.js";
|
|
4
|
+
import { c as __exportAll, i as setupFailure, r as setupExitCodes } from "./envelope-DXB-0EcA.js";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { dirname, join } from "pathe";
|
|
7
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
8
|
+
// src/setup/link.ts
|
|
9
|
+
var link_exports = /* @__PURE__ */ __exportAll({ readSetupLinks: () => readSetupLinks });
|
|
10
|
+
/** The link file, relative to the project root. It is gitignored and never holds a secret. */
|
|
11
|
+
const SETUP_LINK_FILE = ".vitehub/link.json";
|
|
12
|
+
const linkHosts = [
|
|
13
|
+
"cloudflare",
|
|
14
|
+
"vercel",
|
|
15
|
+
"netlify",
|
|
16
|
+
"deno"
|
|
17
|
+
];
|
|
18
|
+
function isLinkHost(value) {
|
|
19
|
+
return linkHosts.some((host) => host === value);
|
|
20
|
+
}
|
|
21
|
+
function optionalString(value) {
|
|
22
|
+
return typeof value === "string" && value ? value : void 0;
|
|
23
|
+
}
|
|
24
|
+
function invalidResponse(host) {
|
|
25
|
+
return cliErrorDiagnostics.CLI_HOST_RESPONSE_INVALID({ host });
|
|
26
|
+
}
|
|
27
|
+
function normalizeName(value) {
|
|
28
|
+
return value?.trim().toLowerCase().replace(/[^a-z0-9]+/gu, "-").replace(/^-|-$/gu, "") || void 0;
|
|
29
|
+
}
|
|
30
|
+
async function packageName(root) {
|
|
31
|
+
for (let folder = root;; folder = dirname(folder)) {
|
|
32
|
+
const file = join(folder, "package.json");
|
|
33
|
+
if (existsSync(file)) {
|
|
34
|
+
const value = JSON.parse(await readFile(file, "utf8"));
|
|
35
|
+
const name = isRecord(value) ? optionalString(value.name)?.trim() : void 0;
|
|
36
|
+
if (name) return name;
|
|
37
|
+
}
|
|
38
|
+
if (dirname(folder) === folder) return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function nameRequired() {
|
|
42
|
+
return setupFailure(cliErrorDiagnostics.SETUP_LINK_NAME_REQUIRED(), setupExitCodes.inputNeeded, {
|
|
43
|
+
missing: [{ field: "project" }],
|
|
44
|
+
rerun: "vitehub link --project <name>"
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async function projectName(project, option) {
|
|
48
|
+
if (option?.trim()) return option.trim();
|
|
49
|
+
const config = project.config;
|
|
50
|
+
if (config?.ok) {
|
|
51
|
+
const name = config.site.keys.name;
|
|
52
|
+
if (name?.kind === "literal" && typeof name.value === "string") {
|
|
53
|
+
const normalized = normalizeName(name.value);
|
|
54
|
+
if (normalized) return normalized;
|
|
55
|
+
}
|
|
56
|
+
if (name || config.site.spread) throw nameRequired();
|
|
57
|
+
} else if (config && config.reason !== "no-vitehub") throw nameRequired();
|
|
58
|
+
const name = normalizeName(await packageName(project.root));
|
|
59
|
+
if (name) return name;
|
|
60
|
+
throw nameRequired();
|
|
61
|
+
}
|
|
62
|
+
function linkHost(project, option, env) {
|
|
63
|
+
const chosen = readLinkHostOption(option);
|
|
64
|
+
if (chosen) return chosen;
|
|
65
|
+
const host = setupStatus(project, env).host.name;
|
|
66
|
+
if (!isLinkHost(host)) throw setupFailure(cliErrorDiagnostics.SETUP_LINK_HOST_UNSUPPORTED({ host }), setupExitCodes.conflict, {
|
|
67
|
+
missing: [{
|
|
68
|
+
field: "host",
|
|
69
|
+
choices: linkHosts.map((id) => ({ id }))
|
|
70
|
+
}],
|
|
71
|
+
rerun: "vitehub link --host <host>"
|
|
72
|
+
});
|
|
73
|
+
return host;
|
|
74
|
+
}
|
|
75
|
+
function rerunCommand(host, name, options, extra) {
|
|
76
|
+
return [
|
|
77
|
+
"vitehub link",
|
|
78
|
+
`--host ${host}`,
|
|
79
|
+
`--project ${name}`,
|
|
80
|
+
...options.account ? [`--account ${options.account}`] : [],
|
|
81
|
+
extra
|
|
82
|
+
].join(" ");
|
|
83
|
+
}
|
|
84
|
+
function notFound(host, name, options, account) {
|
|
85
|
+
if (host === "deno") return setupFailure(cliErrorDiagnostics.SETUP_LINK_PROJECT_NOT_FOUND({
|
|
86
|
+
account,
|
|
87
|
+
host,
|
|
88
|
+
project: name
|
|
89
|
+
}), setupExitCodes.conflict);
|
|
90
|
+
const rerun = rerunCommand(host, name, options, "--create");
|
|
91
|
+
return setupFailure(cliErrorDiagnostics.SETUP_LINK_PROJECT_NOT_FOUND({
|
|
92
|
+
account,
|
|
93
|
+
host,
|
|
94
|
+
project: name,
|
|
95
|
+
rerun
|
|
96
|
+
}), setupExitCodes.conflict, { rerun });
|
|
97
|
+
}
|
|
98
|
+
async function unlessNotFound(request) {
|
|
99
|
+
try {
|
|
100
|
+
return await request;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (isProvisionRequestError(error) && error.status === 404) return;
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function parseCloudflareAccounts(value) {
|
|
107
|
+
if (!Array.isArray(value)) throw invalidResponse("Cloudflare");
|
|
108
|
+
return value.flatMap((account) => {
|
|
109
|
+
const id = isRecord(account) ? optionalString(account.id) : void 0;
|
|
110
|
+
if (!id || !isRecord(account)) return [];
|
|
111
|
+
const name = optionalString(account.name);
|
|
112
|
+
return [{
|
|
113
|
+
id,
|
|
114
|
+
...name ? { name } : {}
|
|
115
|
+
}];
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function parseCloudflareWorker(value) {
|
|
119
|
+
const name = isRecord(value) ? optionalString(value.name) : void 0;
|
|
120
|
+
if (!isRecord(value) || !name) throw invalidResponse("Cloudflare");
|
|
121
|
+
const id = optionalString(value.id);
|
|
122
|
+
return {
|
|
123
|
+
...id ? { id } : {},
|
|
124
|
+
name
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async function linkCloudflare(name, options, token, accountId, fetchImpl) {
|
|
128
|
+
let account;
|
|
129
|
+
if (accountId) account = { id: accountId };
|
|
130
|
+
else {
|
|
131
|
+
const listed = await createCloudflareApiClient(token, fetchImpl)("/accounts", {
|
|
132
|
+
parse: parseCloudflareAccounts,
|
|
133
|
+
query: { per_page: "50" }
|
|
134
|
+
});
|
|
135
|
+
const accounts = listed.result ?? [];
|
|
136
|
+
const count = Math.max(accounts.length, listed.result_info?.total_count ?? 0);
|
|
137
|
+
if (count !== 1 || !accounts[0]) throw setupFailure(cliErrorDiagnostics.SETUP_LINK_ACCOUNT_REQUIRED({ count }), setupExitCodes.inputNeeded, {
|
|
138
|
+
missing: [{
|
|
139
|
+
choices: accounts,
|
|
140
|
+
field: "account"
|
|
141
|
+
}],
|
|
142
|
+
rerun: rerunCommand("cloudflare", name, {
|
|
143
|
+
...options,
|
|
144
|
+
account: void 0
|
|
145
|
+
}, "--account <id>")
|
|
146
|
+
});
|
|
147
|
+
account = accounts[0];
|
|
148
|
+
}
|
|
149
|
+
const client = createCloudflareProvisionClient({
|
|
150
|
+
accountId: account.id,
|
|
151
|
+
token
|
|
152
|
+
}, fetchImpl);
|
|
153
|
+
const found = await unlessNotFound(client(`/workers/workers/${encodeURIComponent(name)}`, { parse: parseCloudflareWorker }));
|
|
154
|
+
if (found?.result) return {
|
|
155
|
+
account,
|
|
156
|
+
created: false,
|
|
157
|
+
project: found.result,
|
|
158
|
+
verified: true
|
|
159
|
+
};
|
|
160
|
+
if (!options.create) throw notFound("cloudflare", name, options, account.id);
|
|
161
|
+
const created = await client("/workers/workers", {
|
|
162
|
+
body: { name },
|
|
163
|
+
method: "POST",
|
|
164
|
+
parse: parseCloudflareWorker
|
|
165
|
+
});
|
|
166
|
+
if (!created.result) throw invalidResponse("Cloudflare");
|
|
167
|
+
return {
|
|
168
|
+
account,
|
|
169
|
+
created: true,
|
|
170
|
+
project: created.result,
|
|
171
|
+
verified: true
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function parseVercelProject(value) {
|
|
175
|
+
const id = isRecord(value) ? optionalString(value.id) : void 0;
|
|
176
|
+
const name = isRecord(value) ? optionalString(value.name) : void 0;
|
|
177
|
+
if (!isRecord(value) || !id || !name) throw invalidResponse("Vercel");
|
|
178
|
+
const accountId = optionalString(value.accountId);
|
|
179
|
+
return {
|
|
180
|
+
id,
|
|
181
|
+
name,
|
|
182
|
+
...accountId ? { accountId } : {}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
async function linkVercel(name, options, token, teamId, fetchImpl) {
|
|
186
|
+
const client = createVercelProvisionClient({
|
|
187
|
+
teamId,
|
|
188
|
+
token
|
|
189
|
+
}, fetchImpl);
|
|
190
|
+
let created = false;
|
|
191
|
+
let project = await unlessNotFound(client(`/v9/projects/${encodeURIComponent(name)}`, { parse: parseVercelProject }));
|
|
192
|
+
if (!project) {
|
|
193
|
+
if (!options.create) throw notFound("vercel", name, options, teamId);
|
|
194
|
+
project = await client("/v11/projects", {
|
|
195
|
+
body: { name },
|
|
196
|
+
method: "POST",
|
|
197
|
+
parse: parseVercelProject
|
|
198
|
+
});
|
|
199
|
+
created = true;
|
|
200
|
+
}
|
|
201
|
+
const accountId = project.accountId ?? teamId;
|
|
202
|
+
return {
|
|
203
|
+
...accountId ? { account: { id: accountId } } : {},
|
|
204
|
+
created,
|
|
205
|
+
project: {
|
|
206
|
+
id: project.id,
|
|
207
|
+
name: project.name
|
|
208
|
+
},
|
|
209
|
+
verified: true
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
async function linkVercelLogin(root, name, options, context) {
|
|
213
|
+
const saved = (await readSetupLinks(root)).targets.find((target) => target.host === "vercel");
|
|
214
|
+
if (saved?.verified && saved.project.name === name && (options.account === void 0 || options.account === saved.account?.id)) return {
|
|
215
|
+
...saved,
|
|
216
|
+
created: false
|
|
217
|
+
};
|
|
218
|
+
const team = options.account ?? (context.env.VERCEL_TEAM_ID?.trim() || context.env.VERCEL_ORG_ID?.trim() || void 0);
|
|
219
|
+
context.stderr.write(`[vitehub] The saved vercel login gives no API token, so the link names ${name} without checking it on Vercel. \`vitehub deploy\` uses the login. Set VERCEL_TOKEN to check the link.\n`);
|
|
220
|
+
return {
|
|
221
|
+
...team ? { account: { id: team } } : {},
|
|
222
|
+
created: false,
|
|
223
|
+
project: { name },
|
|
224
|
+
verified: false
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
function parseNetlifySite(value) {
|
|
228
|
+
const id = isRecord(value) ? optionalString(value.id) : void 0;
|
|
229
|
+
const name = isRecord(value) ? optionalString(value.name) : void 0;
|
|
230
|
+
if (!isRecord(value) || !id || !name) throw invalidResponse("Netlify");
|
|
231
|
+
const accountId = optionalString(value.account_id);
|
|
232
|
+
const accountName = optionalString(value.account_name) ?? optionalString(value.account_slug);
|
|
233
|
+
return {
|
|
234
|
+
id,
|
|
235
|
+
name,
|
|
236
|
+
...accountId ? { account: {
|
|
237
|
+
id: accountId,
|
|
238
|
+
...accountName ? { name: accountName } : {}
|
|
239
|
+
} } : {}
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function parseNetlifySites(value) {
|
|
243
|
+
if (!Array.isArray(value)) throw invalidResponse("Netlify");
|
|
244
|
+
return value.map(parseNetlifySite);
|
|
245
|
+
}
|
|
246
|
+
async function linkNetlify(name, options, token, fetchImpl) {
|
|
247
|
+
const client = createNetlifyApiClient(token, fetchImpl);
|
|
248
|
+
const path = options.account ? `/${encodeURIComponent(options.account)}/sites` : "/sites";
|
|
249
|
+
let site;
|
|
250
|
+
for (let page = 1;; page++) {
|
|
251
|
+
const sites = await client(path, {
|
|
252
|
+
parse: parseNetlifySites,
|
|
253
|
+
query: {
|
|
254
|
+
name,
|
|
255
|
+
per_page: "100",
|
|
256
|
+
...options.account ? {} : { filter: "all" },
|
|
257
|
+
page: String(page)
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
site = sites.find((item) => item.name === name);
|
|
261
|
+
if (site || sites.length < 100) break;
|
|
262
|
+
}
|
|
263
|
+
let created = false;
|
|
264
|
+
if (!site) {
|
|
265
|
+
if (!options.create) throw notFound("netlify", name, options, options.account);
|
|
266
|
+
site = await client(path, {
|
|
267
|
+
body: { name },
|
|
268
|
+
method: "POST",
|
|
269
|
+
parse: parseNetlifySite
|
|
270
|
+
});
|
|
271
|
+
created = true;
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
...site.account ? { account: site.account } : {},
|
|
275
|
+
created,
|
|
276
|
+
project: {
|
|
277
|
+
id: site.id,
|
|
278
|
+
name: site.name
|
|
279
|
+
},
|
|
280
|
+
verified: true
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
async function linkDeno(name, options, token, root, context) {
|
|
284
|
+
const linked = {
|
|
285
|
+
...options.account ? { account: { id: options.account } } : {},
|
|
286
|
+
created: false,
|
|
287
|
+
project: { name }
|
|
288
|
+
};
|
|
289
|
+
const organization = options.account ?? context.env.DENO_DEPLOY_ORG?.trim();
|
|
290
|
+
if (!token || !organization) return {
|
|
291
|
+
...linked,
|
|
292
|
+
verified: false
|
|
293
|
+
};
|
|
294
|
+
const deno = hostCli("deno", "deno", root, context.env);
|
|
295
|
+
const step = "deno deploy apps get";
|
|
296
|
+
const secrets = [token];
|
|
297
|
+
const run = context.runHostCli ?? createDeployRun(context.stderr, secrets);
|
|
298
|
+
const runOptions = {
|
|
299
|
+
cwd: root,
|
|
300
|
+
env: context.env,
|
|
301
|
+
timeoutMs: 3e4
|
|
302
|
+
};
|
|
303
|
+
const result = await run(deno[0], [
|
|
304
|
+
...deno.slice(1),
|
|
305
|
+
"deploy",
|
|
306
|
+
"apps",
|
|
307
|
+
"get",
|
|
308
|
+
"--json",
|
|
309
|
+
"--non-interactive",
|
|
310
|
+
"--org",
|
|
311
|
+
organization,
|
|
312
|
+
"--app",
|
|
313
|
+
name
|
|
314
|
+
], runOptions).catch((error) => {
|
|
315
|
+
throw hostCliFailed(step, {
|
|
316
|
+
exitCode: null,
|
|
317
|
+
stderr: error instanceof Error ? error.message : String(error),
|
|
318
|
+
stdout: ""
|
|
319
|
+
}, secrets);
|
|
320
|
+
});
|
|
321
|
+
if (result.exitCode === 4) throw notFound("deno", name, options, organization);
|
|
322
|
+
if (result.exitCode !== 0) throw hostCliFailed(step, {
|
|
323
|
+
...result,
|
|
324
|
+
stderr: result.stderr ?? ""
|
|
325
|
+
}, secrets);
|
|
326
|
+
return {
|
|
327
|
+
...linked,
|
|
328
|
+
account: { id: organization },
|
|
329
|
+
verified: true
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
const hostCliFolders = {
|
|
333
|
+
cloudflare: ".wrangler",
|
|
334
|
+
netlify: ".netlify",
|
|
335
|
+
vercel: ".vercel"
|
|
336
|
+
};
|
|
337
|
+
/** The folders that a `.gitignore` must ignore: `.vitehub`, and the local folder of the host CLI, such as `.wrangler`. */
|
|
338
|
+
function ignoredFolders(host) {
|
|
339
|
+
const folder = host ? hostCliFolders[host] : void 0;
|
|
340
|
+
return [".vitehub", ...folder ? [folder] : []];
|
|
341
|
+
}
|
|
342
|
+
function gitignoreRootPattern(pattern) {
|
|
343
|
+
return pattern.replace(/^(?:\/|\*\*\/)/u, "");
|
|
344
|
+
}
|
|
345
|
+
function gitignoreCoversDefault(lines, entry) {
|
|
346
|
+
const lastOtherNegation = lines.findLastIndex((line) => line.startsWith("!") && gitignoreRootPattern(line.slice(1)) !== ".env.example");
|
|
347
|
+
const patterns = lines.slice(lastOtherNegation + 1).map(gitignoreRootPattern);
|
|
348
|
+
if (entry === ".env*") return patterns.includes(entry) || patterns.includes(".env") && patterns.includes(".env.*");
|
|
349
|
+
return patterns.includes(entry) || patterns.includes(`${entry}/`);
|
|
350
|
+
}
|
|
351
|
+
function gitignoreExemptsFile(lines, file) {
|
|
352
|
+
for (const line of lines.toReversed()) {
|
|
353
|
+
if (!line || line.startsWith("#")) continue;
|
|
354
|
+
if (line.startsWith("!")) {
|
|
355
|
+
if (gitignoreRootPattern(line.slice(1)) === file) return true;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const pattern = gitignoreRootPattern(line);
|
|
359
|
+
if (pattern.toLowerCase() === file || !/^[\w.-]+\/?$/u.test(pattern)) return false;
|
|
360
|
+
}
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
function gitignoreWith(content, entries) {
|
|
364
|
+
const lines = content.split(/\r?\n/u);
|
|
365
|
+
const missing = [];
|
|
366
|
+
for (const entry of entries) {
|
|
367
|
+
if (entry.startsWith("!") ? gitignoreExemptsFile(lines, entry.slice(1)) : gitignoreCoversDefault(lines, entry)) continue;
|
|
368
|
+
missing.push(entry);
|
|
369
|
+
lines.push(entry);
|
|
370
|
+
}
|
|
371
|
+
if (!missing.length) return;
|
|
372
|
+
return `${content}${content === "" || content.endsWith("\n") ? "" : "\n"}${missing.map((entry) => `${entry}\n`).join("")}`;
|
|
373
|
+
}
|
|
374
|
+
/** `content` of a `.gitignore` with a line for each of `ignoredFolders(host)` that it does not ignore yet, or undefined when it ignores all. */
|
|
375
|
+
function gitignoreWithViteHub(content, host) {
|
|
376
|
+
return gitignoreWith(content, ignoredFolders(host));
|
|
377
|
+
}
|
|
378
|
+
/** Appends `.vitehub` and the local folder of the host CLI to the project `.gitignore` when they are missing. */
|
|
379
|
+
async function ensureViteHubGitignore(root, host) {
|
|
380
|
+
const file = join(root, ".gitignore");
|
|
381
|
+
if (!existsSync(file)) return "no-gitignore";
|
|
382
|
+
const next = gitignoreWithViteHub(await readFile(file, "utf8"), host);
|
|
383
|
+
if (next === void 0) return "present";
|
|
384
|
+
await writeFile(file, next);
|
|
385
|
+
return "added";
|
|
386
|
+
}
|
|
387
|
+
/** Looks up the project on the host, creates it with `create`, and writes `.vitehub/link.json`. */
|
|
388
|
+
async function linkSetupProject(project, options, context) {
|
|
389
|
+
const host = linkHost(project, options.host, context.env);
|
|
390
|
+
const name = await projectName(project, options.project);
|
|
391
|
+
const credentialsContext = {
|
|
392
|
+
cwd: project.root,
|
|
393
|
+
env: context.env,
|
|
394
|
+
...context.runHostCli ? { run: context.runHostCli } : {}
|
|
395
|
+
};
|
|
396
|
+
const credentials = await resolveHostCredentials(host, credentialsContext);
|
|
397
|
+
const fetchImpl = context.fetch ?? globalThis.fetch;
|
|
398
|
+
const vercelLogin = host === "vercel" && !credentials.ok && !options.create && await hasVercelLogin(project.root, context);
|
|
399
|
+
let linked;
|
|
400
|
+
if (host === "deno") linked = await linkDeno(name, options, credentials.ok ? credentials.token : void 0, project.root, context);
|
|
401
|
+
else if (vercelLogin) linked = await linkVercelLogin(project.root, name, options, context);
|
|
402
|
+
else if (!credentials.ok) throw setupFailure(cliErrorDiagnostics.SETUP_LINK_CREDENTIALS_MISSING({
|
|
403
|
+
fix: credentials.fix,
|
|
404
|
+
host
|
|
405
|
+
}), setupExitCodes.inputNeeded, { missing: credentials.missing.map((field) => ({ field })) });
|
|
406
|
+
else if (host === "cloudflare") linked = await linkCloudflare(name, options, credentials.token, options.account ?? credentials.accountId, fetchImpl);
|
|
407
|
+
else if (host === "vercel") linked = await linkVercel(name, options, credentials.token, options.account ?? credentials.teamId, fetchImpl);
|
|
408
|
+
else linked = await linkNetlify(name, options, credentials.token, fetchImpl);
|
|
409
|
+
const link = {
|
|
410
|
+
host,
|
|
411
|
+
...linked.account ? { account: linked.account } : {},
|
|
412
|
+
project: linked.project,
|
|
413
|
+
verified: linked.verified,
|
|
414
|
+
linkedAt: linked.linkedAt ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
415
|
+
};
|
|
416
|
+
const { added, links } = await addSetupLink(project.root, link, { default: options.default === true });
|
|
417
|
+
if (added) writeSeparateResourcesNote(context, host, links);
|
|
418
|
+
return {
|
|
419
|
+
created: linked.created,
|
|
420
|
+
credentials: vercelLogin ? { source: "host-cli" } : host !== "deno" && credentials.ok ? { source: credentials.source } : null,
|
|
421
|
+
default: links.default ?? null,
|
|
422
|
+
file: SETUP_LINK_FILE,
|
|
423
|
+
gitignore: await ensureViteHubGitignore(project.root, host),
|
|
424
|
+
hosts: links.targets.map((target) => target.host),
|
|
425
|
+
link
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Says once, when a project gets a second host, that host resources are not shared. A D1 database, a KV namespace, an
|
|
430
|
+
* R2 bucket, or a libSQL database of one host is not the store of the other host.
|
|
431
|
+
*/
|
|
432
|
+
function writeSeparateResourcesNote(context, host, links) {
|
|
433
|
+
const others = links.targets.map((target) => target.host).filter((item) => item !== host);
|
|
434
|
+
if (!others.length) return;
|
|
435
|
+
context.stderr.write(`[vitehub] ${host} is a new host for this app, next to ${others.join(", ")}. Resources such as D1, KV, R2, and libSQL are separate on each host, so ${host} starts with its own empty data.\n`);
|
|
436
|
+
}
|
|
437
|
+
/** The text of `.vitehub/link.json` for `links`. */
|
|
438
|
+
function formatSetupLinkFile(links) {
|
|
439
|
+
const file = {
|
|
440
|
+
schemaVersion: 2,
|
|
441
|
+
...links.default ? { default: links.default } : {},
|
|
442
|
+
targets: Object.fromEntries(links.targets.map(({ host, ...target }) => [host, target]))
|
|
443
|
+
};
|
|
444
|
+
return `${JSON.stringify(file, null, 2)}\n`;
|
|
445
|
+
}
|
|
446
|
+
/** Writes `.vitehub/link.json`, or removes it when `links` has no target. */
|
|
447
|
+
async function writeSetupLinks(root, links) {
|
|
448
|
+
const file = join(root, SETUP_LINK_FILE);
|
|
449
|
+
if (!links.targets.length) {
|
|
450
|
+
await rm(file, { force: true });
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
await mkdir(dirname(file), { recursive: true });
|
|
454
|
+
await writeFile(file, formatSetupLinkFile(links));
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Adds the target of `link.host`, or replaces it, and keeps the other targets. The first target becomes the default,
|
|
458
|
+
* and `options.default` makes this one the default. `added` is true when the host had no target before.
|
|
459
|
+
*/
|
|
460
|
+
async function addSetupLink(root, link, options = {}) {
|
|
461
|
+
const current = await readSetupLinks(root);
|
|
462
|
+
const added = !current.targets.some((target) => target.host === link.host);
|
|
463
|
+
const targets = added ? [...current.targets, link] : current.targets.map((target) => {
|
|
464
|
+
if (target.host !== link.host) return target;
|
|
465
|
+
return target.project.name === link.project.name && target.account?.id === link.account?.id && target.resourceScope && !link.resourceScope ? {
|
|
466
|
+
...link,
|
|
467
|
+
resourceScope: target.resourceScope
|
|
468
|
+
} : link;
|
|
469
|
+
});
|
|
470
|
+
const fallback = current.default ?? (targets.length === 1 ? link.host : void 0);
|
|
471
|
+
const preferred = options.default ? link.host : fallback;
|
|
472
|
+
const links = {
|
|
473
|
+
...preferred ? { default: preferred } : {},
|
|
474
|
+
targets
|
|
475
|
+
};
|
|
476
|
+
await writeSetupLinks(root, links);
|
|
477
|
+
return {
|
|
478
|
+
added,
|
|
479
|
+
links
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Removes the target of `host`, or the whole link file without `host`. When the default target goes and one target
|
|
484
|
+
* stays, that one becomes the default. Returns false when there was nothing to remove.
|
|
485
|
+
*/
|
|
486
|
+
async function unlinkSetupProject(root, host) {
|
|
487
|
+
const file = join(root, SETUP_LINK_FILE);
|
|
488
|
+
if (!existsSync(file)) return false;
|
|
489
|
+
if (!host) {
|
|
490
|
+
await rm(file, { force: true });
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
const current = await readSetupLinks(root);
|
|
494
|
+
const targets = current.targets.filter((target) => target.host !== host);
|
|
495
|
+
if (targets.length === current.targets.length) return false;
|
|
496
|
+
const kept = current.default && current.default !== host ? current.default : targets.length === 1 ? targets[0].host : void 0;
|
|
497
|
+
await writeSetupLinks(root, {
|
|
498
|
+
...kept ? { default: kept } : {},
|
|
499
|
+
targets
|
|
500
|
+
});
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
function readTarget(host, value) {
|
|
504
|
+
if (!isRecord(value)) return;
|
|
505
|
+
const project = value.project;
|
|
506
|
+
const name = isRecord(project) ? optionalString(project.name) : void 0;
|
|
507
|
+
if (!isRecord(project) || !name) return;
|
|
508
|
+
const projectId = optionalString(project.id);
|
|
509
|
+
const account = isRecord(value.account) ? optionalString(value.account.id) : void 0;
|
|
510
|
+
const accountName = isRecord(value.account) ? optionalString(value.account.name) : void 0;
|
|
511
|
+
const resourceScope = optionalString(value.resourceScope);
|
|
512
|
+
return {
|
|
513
|
+
host,
|
|
514
|
+
...account ? { account: {
|
|
515
|
+
id: account,
|
|
516
|
+
...accountName ? { name: accountName } : {}
|
|
517
|
+
} } : {},
|
|
518
|
+
project: {
|
|
519
|
+
...projectId ? { id: projectId } : {},
|
|
520
|
+
name
|
|
521
|
+
},
|
|
522
|
+
...resourceScope ? { resourceScope } : {},
|
|
523
|
+
verified: value.verified === true,
|
|
524
|
+
linkedAt: optionalString(value.linkedAt) ?? ""
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Reads the targets of `.vitehub/link.json`. A missing or unreadable file has no target. A file of schema version 1
|
|
529
|
+
* holds one host link, so it reads as one target that is also the default. The next write saves it as version 2.
|
|
530
|
+
*/
|
|
531
|
+
async function readSetupLinks(root) {
|
|
532
|
+
const file = join(root, SETUP_LINK_FILE);
|
|
533
|
+
if (!existsSync(file)) return { targets: [] };
|
|
534
|
+
let value;
|
|
535
|
+
try {
|
|
536
|
+
value = JSON.parse(await readFile(file, "utf8"));
|
|
537
|
+
} catch {
|
|
538
|
+
return { targets: [] };
|
|
539
|
+
}
|
|
540
|
+
if (!isRecord(value)) return { targets: [] };
|
|
541
|
+
if (value.schemaVersion === 1) {
|
|
542
|
+
const host = typeof value.host === "string" && isLinkHost(value.host) ? value.host : void 0;
|
|
543
|
+
const target = host ? readTarget(host, value) : void 0;
|
|
544
|
+
return target ? {
|
|
545
|
+
default: target.host,
|
|
546
|
+
targets: [target]
|
|
547
|
+
} : { targets: [] };
|
|
548
|
+
}
|
|
549
|
+
if (value.schemaVersion !== 2 || !isRecord(value.targets)) return { targets: [] };
|
|
550
|
+
const entries = value.targets;
|
|
551
|
+
const targets = linkHosts.flatMap((host) => readTarget(host, entries[host]) ?? []);
|
|
552
|
+
const preferred = targets.find((target) => target.host === value.default)?.host;
|
|
553
|
+
return {
|
|
554
|
+
...preferred ? { default: preferred } : {},
|
|
555
|
+
targets
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
/** Picks the target of `host`. Without `host`: the default target, else the only target. */
|
|
559
|
+
function selectSetupLink(links, host) {
|
|
560
|
+
if (host) return { link: links.targets.find((target) => target.host === host) };
|
|
561
|
+
const preferred = links.default ?? (links.targets.length === 1 ? links.targets[0].host : void 0);
|
|
562
|
+
if (preferred) return { link: links.targets.find((target) => target.host === preferred) };
|
|
563
|
+
return links.targets.length ? { ambiguous: links.targets.map((target) => target.host) } : { link: void 0 };
|
|
564
|
+
}
|
|
565
|
+
/** The failure for several targets and no default. `command` is the command to run again with `--host`. */
|
|
566
|
+
function hostRequired(hosts, command) {
|
|
567
|
+
return setupFailure(cliErrorDiagnostics.SETUP_LINK_HOST_REQUIRED({
|
|
568
|
+
command,
|
|
569
|
+
hosts: hosts.join(", ")
|
|
570
|
+
}), setupExitCodes.inputNeeded, {
|
|
571
|
+
missing: [{
|
|
572
|
+
choices: hosts.map((id) => ({ id })),
|
|
573
|
+
field: "host"
|
|
574
|
+
}],
|
|
575
|
+
rerun: `${command} --host ${hosts[0]}`
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
/** Reads `--host` of a command that uses a link target. */
|
|
579
|
+
function readLinkHostOption(value) {
|
|
580
|
+
if (value === void 0) return;
|
|
581
|
+
const host = hostFromNitroPreset(value);
|
|
582
|
+
if (!host) throw cliErrorDiagnostics.CLI_R0003({ message: `[vitehub] Unknown host ${JSON.stringify(value)}. Use cloudflare, vercel, netlify, or deno.` });
|
|
583
|
+
if (!isLinkHost(host)) throw setupFailure(cliErrorDiagnostics.SETUP_LINK_HOST_UNSUPPORTED({ host }), setupExitCodes.conflict);
|
|
584
|
+
return host;
|
|
585
|
+
}
|
|
586
|
+
function summary(link) {
|
|
587
|
+
return {
|
|
588
|
+
host: link.host,
|
|
589
|
+
...link.account ? { account: link.account } : {},
|
|
590
|
+
project: link.project,
|
|
591
|
+
verified: link.verified
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
/** Reads the link and credentials, skipping CLI discovery offline and sharing the online host deadline. */
|
|
595
|
+
async function readSetupLinkState(project, host, context, options = {}) {
|
|
596
|
+
const links = await readSetupLinks(project.root);
|
|
597
|
+
const selected = selectSetupLink(links);
|
|
598
|
+
const link = "link" in selected ? selected.link : links.targets.find((target) => target.host === host);
|
|
599
|
+
const credentialHost = link?.host ?? (isLinkHost(host) ? host : void 0);
|
|
600
|
+
const credentials = credentialHost ? await readStatusCredentials(credentialHost, project.root, context, options) : void 0;
|
|
601
|
+
const vercelLogin = credentials?.ok === false && credentials.host === "vercel" && !options.offline && !options.signal?.aborted && await hasVercelLogin(project.root, context, options.signal);
|
|
602
|
+
const timedOut = credentials?.ok === false && !vercelLogin && options.signal?.aborted === true;
|
|
603
|
+
return {
|
|
604
|
+
credentials,
|
|
605
|
+
link,
|
|
606
|
+
status: {
|
|
607
|
+
credentials: vercelLogin ? {
|
|
608
|
+
host: "vercel",
|
|
609
|
+
ok: true,
|
|
610
|
+
source: "host-cli"
|
|
611
|
+
} : timedOut ? credentialsTimedOut(credentials) : credentials ? describeCredentials(credentials) : null,
|
|
612
|
+
default: links.default ?? null,
|
|
613
|
+
link: link ? summary(link) : null,
|
|
614
|
+
targets: links.targets.map(summary)
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
async function readStatusCredentials(host, cwd, context, options) {
|
|
619
|
+
const envOnly = () => resolveHostCredentials(host, {
|
|
620
|
+
cwd,
|
|
621
|
+
env: context.env,
|
|
622
|
+
run: async () => ({
|
|
623
|
+
exitCode: 1,
|
|
624
|
+
stdout: ""
|
|
625
|
+
})
|
|
626
|
+
});
|
|
627
|
+
if (options.offline) return await envOnly();
|
|
628
|
+
const signal = options.signal;
|
|
629
|
+
try {
|
|
630
|
+
return await abortable(signal, "Host credential discovery timed out", () => resolveHostCredentials(host, {
|
|
631
|
+
cwd,
|
|
632
|
+
env: context.env,
|
|
633
|
+
run: (command, args, runOptions) => {
|
|
634
|
+
signal?.throwIfAborted();
|
|
635
|
+
return (context.runHostCli ?? runHostCli)(command, args, {
|
|
636
|
+
...runOptions,
|
|
637
|
+
...signal ? { signal } : {}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
}));
|
|
641
|
+
} catch (error) {
|
|
642
|
+
if (!signal?.aborted) throw error;
|
|
643
|
+
return await envOnly();
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function credentialsTimedOut(credentials) {
|
|
647
|
+
const cli = credentials.host === "cloudflare" ? "wrangler" : credentials.host;
|
|
648
|
+
return {
|
|
649
|
+
...credentials,
|
|
650
|
+
fix: `The ${credentials.host} login check timed out, so the login is unknown. Run \`vitehub status\` again: the first \`npx ${cli}\` run can take longer than the check. Or: ${credentials.fix}`,
|
|
651
|
+
unknown: "timeout"
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
function describeCredentials(credentials) {
|
|
655
|
+
if (!credentials.ok) return credentials;
|
|
656
|
+
const { token: _token, ...description } = credentials;
|
|
657
|
+
return description;
|
|
658
|
+
}
|
|
659
|
+
/** The link and the credential status for `vitehub status`. The credentials never include the token. */
|
|
660
|
+
async function readSetupLinkStatus(project, host, context) {
|
|
661
|
+
return (await readSetupLinkState(project, host, context)).status;
|
|
662
|
+
}
|
|
663
|
+
export { hostRequired as a, link_exports as c, readSetupLinkStatus as d, readSetupLinks as f, writeSeparateResourcesNote as h, gitignoreWith as i, readLinkHostOption as l, unlinkSetupProject as m, addSetupLink as n, ignoredFolders as o, selectSetupLink as p, ensureViteHubGitignore as r, linkSetupProject as s, SETUP_LINK_FILE as t, readSetupLinkState as u };
|