@moku-labs/worker 0.5.0 → 0.6.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 +10 -9
- package/dist/cli-Dc0q0hIy.cjs +2988 -0
- package/dist/cli-DgZv5A0G.mjs +2888 -0
- package/dist/cli.cjs +3 -1900
- package/dist/cli.d.cts +1 -270
- package/dist/cli.d.mts +1 -270
- package/dist/cli.mjs +1 -1875
- package/dist/index-VZ99IAMv.d.cts +353 -0
- package/dist/index-VZ99IAMv.d.mts +353 -0
- package/dist/index.cjs +61 -63
- package/dist/index.d.cts +8 -4
- package/dist/index.d.mts +8 -4
- package/dist/index.mjs +49 -53
- package/package.json +1 -1
- package/dist/config-BYPJvEbl.d.cts +0 -88
- package/dist/config-BYPJvEbl.d.mts +0 -88
- package/dist/storage-COo-F38H.mjs +0 -884
- package/dist/storage-CgXl-dUA.cjs +0 -949
package/dist/cli.cjs
CHANGED
|
@@ -1,1901 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: ((k) => from[k]).bind(null, key),
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
-
value: mod,
|
|
21
|
-
enumerable: true
|
|
22
|
-
}) : target, mod));
|
|
23
|
-
//#endregion
|
|
24
|
-
const require_storage = require("./storage-CgXl-dUA.cjs");
|
|
25
|
-
let _moku_labs_common_cli = require("@moku-labs/common/cli");
|
|
26
|
-
let node_child_process = require("node:child_process");
|
|
27
|
-
let node_fs = require("node:fs");
|
|
28
|
-
let node_path = require("node:path");
|
|
29
|
-
node_path = __toESM(node_path, 1);
|
|
30
|
-
let node_fs_promises = require("node:fs/promises");
|
|
31
|
-
//#region src/plugins/deploy/auth/permissions.ts
|
|
32
|
-
/** Permission groups every deploy needs, regardless of resources. */
|
|
33
|
-
const ALWAYS = [{
|
|
34
|
-
group: "Account · Workers Scripts",
|
|
35
|
-
scope: "Edit",
|
|
36
|
-
reason: "deploy",
|
|
37
|
-
inBaseTemplate: true
|
|
38
|
-
}, {
|
|
39
|
-
group: "Account · Account Settings",
|
|
40
|
-
scope: "Read",
|
|
41
|
-
reason: "account",
|
|
42
|
-
inBaseTemplate: true
|
|
43
|
-
}];
|
|
44
|
-
/**
|
|
45
|
-
* Per-resource-kind permission group. `do` needs nothing extra (Durable Objects ship with the
|
|
46
|
-
* Worker script, covered by Workers Scripts · Edit). `d1`/`queue` are NOT in the stock template.
|
|
47
|
-
*/
|
|
48
|
-
const BY_KIND = {
|
|
49
|
-
kv: {
|
|
50
|
-
group: "Account · Workers KV Storage",
|
|
51
|
-
scope: "Edit",
|
|
52
|
-
reason: "kv",
|
|
53
|
-
inBaseTemplate: true
|
|
54
|
-
},
|
|
55
|
-
r2: {
|
|
56
|
-
group: "Account · Workers R2 Storage",
|
|
57
|
-
scope: "Edit",
|
|
58
|
-
reason: "r2",
|
|
59
|
-
inBaseTemplate: true
|
|
60
|
-
},
|
|
61
|
-
d1: {
|
|
62
|
-
group: "Account · D1",
|
|
63
|
-
scope: "Edit",
|
|
64
|
-
reason: "d1",
|
|
65
|
-
inBaseTemplate: false
|
|
66
|
-
},
|
|
67
|
-
queue: {
|
|
68
|
-
group: "Account · Queues",
|
|
69
|
-
scope: "Edit",
|
|
70
|
-
reason: "queue",
|
|
71
|
-
inBaseTemplate: false
|
|
72
|
-
},
|
|
73
|
-
do: void 0
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* Derive the Cloudflare API token requirement from an app manifest: the full permission set plus
|
|
77
|
-
* the subset that must be ADDED to the stock "Edit Cloudflare Workers" template.
|
|
78
|
-
*
|
|
79
|
-
* @param manifest - The assembled deploy manifest.
|
|
80
|
-
* @returns The token requirement (base template, full required set, and groups to add).
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* const { toAdd } = requiredToken({ name: "w", compatibilityDate: "…", resources: [{ kind: "d1", binding: "DB" }] });
|
|
84
|
-
* // toAdd → [{ group: "Account · D1", scope: "Edit", … }]
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
const requiredToken = (manifest) => {
|
|
88
|
-
const required = [...ALWAYS];
|
|
89
|
-
const seen = new Set(required.map((permission) => permission.group));
|
|
90
|
-
for (const resource of manifest.resources) {
|
|
91
|
-
const permission = BY_KIND[resource.kind];
|
|
92
|
-
if (permission !== void 0 && !seen.has(permission.group)) {
|
|
93
|
-
required.push(permission);
|
|
94
|
-
seen.add(permission.group);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
base: "Edit Cloudflare Workers",
|
|
99
|
-
required,
|
|
100
|
-
toAdd: required.filter((permission) => !permission.inBaseTemplate)
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
/** Permission every CI/automation redeploy needs: ship the Worker script. */
|
|
104
|
-
const CI_ALWAYS = [{
|
|
105
|
-
group: "Account · Workers Scripts",
|
|
106
|
-
scope: "Edit",
|
|
107
|
-
reason: "deploy",
|
|
108
|
-
inBaseTemplate: true
|
|
109
|
-
}];
|
|
110
|
-
/**
|
|
111
|
-
* Per-resource-kind permission for the CI/automation token. After a first LOCAL deploy has
|
|
112
|
-
* provisioned everything, CI only needs to LIST existing infra (the idempotent preflight) and
|
|
113
|
-
* ship — so data resources drop to `Read`; R2 stays `Edit` because asset upload writes objects.
|
|
114
|
-
*/
|
|
115
|
-
const CI_BY_KIND = {
|
|
116
|
-
kv: {
|
|
117
|
-
group: "Account · Workers KV Storage",
|
|
118
|
-
scope: "Read",
|
|
119
|
-
reason: "kv (preflight)",
|
|
120
|
-
inBaseTemplate: true
|
|
121
|
-
},
|
|
122
|
-
r2: {
|
|
123
|
-
group: "Account · Workers R2 Storage",
|
|
124
|
-
scope: "Edit",
|
|
125
|
-
reason: "r2 (asset upload)",
|
|
126
|
-
inBaseTemplate: true
|
|
127
|
-
},
|
|
128
|
-
d1: {
|
|
129
|
-
group: "Account · D1",
|
|
130
|
-
scope: "Read",
|
|
131
|
-
reason: "d1 (preflight)",
|
|
132
|
-
inBaseTemplate: false
|
|
133
|
-
},
|
|
134
|
-
queue: {
|
|
135
|
-
group: "Account · Queues",
|
|
136
|
-
scope: "Read",
|
|
137
|
-
reason: "queue (preflight)",
|
|
138
|
-
inBaseTemplate: false
|
|
139
|
-
},
|
|
140
|
-
do: void 0
|
|
141
|
-
};
|
|
142
|
-
/**
|
|
143
|
-
* Derive the REDUCED Cloudflare API token for CI/automation redeploys, from the same manifest.
|
|
144
|
-
* Assumes a prior LOCAL deploy already provisioned the infra, so CI never creates: data resources
|
|
145
|
-
* need only `Read` (the idempotent preflight lists them), R2 keeps `Edit` for asset upload, and no
|
|
146
|
-
* `Account Settings · Read` is needed because CI pins `CLOUDFLARE_ACCOUNT_ID`. Pure: no network.
|
|
147
|
-
*
|
|
148
|
-
* @param manifest - The assembled deploy manifest.
|
|
149
|
-
* @returns The minimum permission groups for a CI redeploy token (deduped, manifest-scoped).
|
|
150
|
-
* @example
|
|
151
|
-
* ```ts
|
|
152
|
-
* const groups = ciToken({ name: "w", compatibilityDate: "…", resources: [{ kind: "d1", binding: "DB" }] });
|
|
153
|
-
* // → [Workers Scripts·Edit, D1·Read]
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
const ciToken = (manifest) => {
|
|
157
|
-
const groups = [...CI_ALWAYS];
|
|
158
|
-
const seen = new Set(groups.map((permission) => permission.group));
|
|
159
|
-
for (const resource of manifest.resources) {
|
|
160
|
-
const permission = CI_BY_KIND[resource.kind];
|
|
161
|
-
if (permission !== void 0 && !seen.has(permission.group)) {
|
|
162
|
-
groups.push(permission);
|
|
163
|
-
seen.add(permission.group);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return groups;
|
|
167
|
-
};
|
|
168
|
-
//#endregion
|
|
169
|
-
//#region src/plugins/deploy/auth/setup.ts
|
|
170
|
-
/** Cloudflare's dashboard path for creating API tokens. */
|
|
171
|
-
const TOKENS_URL = "https://dash.cloudflare.com/profile/api-tokens";
|
|
172
|
-
/**
|
|
173
|
-
* Render the FULL local-first token section (the deploy that provisions everything): the permission
|
|
174
|
-
* table flagging template-missing rows, the template + "add these" steps, and the `.env.local` lines.
|
|
175
|
-
*
|
|
176
|
-
* @param requirement - The full token requirement (from requiredToken()).
|
|
177
|
-
* @returns The local-first section lines.
|
|
178
|
-
* @example
|
|
179
|
-
* ```ts
|
|
180
|
-
* const lines = localSection(requiredToken(manifest));
|
|
181
|
-
* ```
|
|
182
|
-
*/
|
|
183
|
-
const localSection = (requirement) => {
|
|
184
|
-
const permissionRows = requirement.required.map((permission) => {
|
|
185
|
-
const flag = permission.inBaseTemplate ? "" : " <- add to template";
|
|
186
|
-
return ` - ${permission.group} : ${permission.scope} (${permission.reason})${flag}`;
|
|
187
|
-
});
|
|
188
|
-
const step3 = requirement.toAdd.length > 0 ? [` 3. Under Permissions, ADD: ${requirement.toAdd.map((permission) => `${permission.group.replace("Account · ", "")} -> ${permission.scope}`).join(", ")}`, " (the template omits these; everything else is already included)"] : [` 3. The "${requirement.base}" template covers everything — no changes needed.`];
|
|
189
|
-
return [
|
|
190
|
-
"LOCAL — first deploy (provisions infra). A Cloudflare API token with these permissions:",
|
|
191
|
-
"",
|
|
192
|
-
...permissionRows,
|
|
193
|
-
"",
|
|
194
|
-
"Fastest path:",
|
|
195
|
-
` 1. ${TOKENS_URL} -> Create Token`,
|
|
196
|
-
` 2. Start from the "${requirement.base}" template.`,
|
|
197
|
-
...step3,
|
|
198
|
-
" 4. Account Resources -> Include -> your account.",
|
|
199
|
-
" 5. Create the token, copy it, then add it to .env.local:",
|
|
200
|
-
" CLOUDFLARE_API_TOKEN=<paste your token>",
|
|
201
|
-
" CLOUDFLARE_ACCOUNT_ID=<your account id>",
|
|
202
|
-
" 6. Verify it with `auth` (app.deploy.verifyAuth())."
|
|
203
|
-
];
|
|
204
|
-
};
|
|
205
|
-
/**
|
|
206
|
-
* Render the REDUCED CI/automation token section (redeploy-only): the scoped permission table plus
|
|
207
|
-
* the CI-secret + account-pin steps.
|
|
208
|
-
*
|
|
209
|
-
* @param groups - The CI permission groups (from ciToken()).
|
|
210
|
-
* @returns The CI section lines.
|
|
211
|
-
* @example
|
|
212
|
-
* ```ts
|
|
213
|
-
* const lines = ciSection(ciToken(manifest));
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
const ciSection = (groups) => {
|
|
217
|
-
return [
|
|
218
|
-
"CI — automation redeploy (infra already provisioned by a local deploy). A SCOPED token with:",
|
|
219
|
-
"",
|
|
220
|
-
...groups.map((permission) => ` - ${permission.group} : ${permission.scope} (${permission.reason})`),
|
|
221
|
-
"",
|
|
222
|
-
` 1. ${TOKENS_URL} -> Create Token -> Create Custom Token.`,
|
|
223
|
-
" 2. Add exactly the permissions above (Read, not Edit, on data resources — CI never creates).",
|
|
224
|
-
" 3. Account Resources -> Include -> your account.",
|
|
225
|
-
" 4. Store it as the CLOUDFLARE_API_TOKEN secret in CI, and PIN the account so no account",
|
|
226
|
-
" lookup (and no Account Settings -> Read) is needed:",
|
|
227
|
-
" CLOUDFLARE_ACCOUNT_ID=<your account id>",
|
|
228
|
-
" CI reuses the same idempotent pipeline — it lists existing infra and ships. To let CI also",
|
|
229
|
-
" CREATE missing infra (self-heal), give it the LOCAL token above instead."
|
|
230
|
-
];
|
|
231
|
-
};
|
|
232
|
-
/**
|
|
233
|
-
* Render the `auth setup` instructions from the app manifest: the FULL local-first token (provisions
|
|
234
|
-
* everything) followed by the REDUCED CI/automation token (redeploy-only).
|
|
235
|
-
*
|
|
236
|
-
* @param manifest - The assembled deploy manifest.
|
|
237
|
-
* @returns A multi-line instruction string covering both tokens.
|
|
238
|
-
* @example
|
|
239
|
-
* ```ts
|
|
240
|
-
* const text = tokenInstructions(manifest);
|
|
241
|
-
* ```
|
|
242
|
-
*/
|
|
243
|
-
const tokenInstructions = (manifest) => [
|
|
244
|
-
...localSection(requiredToken(manifest)),
|
|
245
|
-
"",
|
|
246
|
-
...ciSection(ciToken(manifest))
|
|
247
|
-
].join("\n");
|
|
248
|
-
//#endregion
|
|
249
|
-
//#region src/plugins/deploy/infra/cloudflare.ts
|
|
250
|
-
/**
|
|
251
|
-
* @file deploy plugin — Cloudflare REST discovery client (infra preflight).
|
|
252
|
-
*
|
|
253
|
-
* Lists what already exists in a Cloudflare account so the deploy pipeline can create only the
|
|
254
|
-
* missing resources (idempotent provisioning) and recover real ids for existing kv/d1 bindings.
|
|
255
|
-
* Authenticated with the `.env` API token (CLOUDFLARE_API_TOKEN) — never an interactive login.
|
|
256
|
-
* Uses the global `fetch`; node-only, never imported by the runtime Worker bundle.
|
|
257
|
-
*/
|
|
258
|
-
const API_BASE = "https://api.cloudflare.com/client/v4";
|
|
259
|
-
/**
|
|
260
|
-
* GET a Cloudflare API path with the bearer token and unwrap the `result`.
|
|
261
|
-
*
|
|
262
|
-
* @param token - The Cloudflare API token (CLOUDFLARE_API_TOKEN).
|
|
263
|
-
* @param path - API path beneath the v4 base (e.g. "/accounts").
|
|
264
|
-
* @returns The unwrapped `result` payload, typed by the caller.
|
|
265
|
-
* @throws {Error} When the HTTP request fails or the API reports `success: false`.
|
|
266
|
-
* @example
|
|
267
|
-
* ```ts
|
|
268
|
-
* const accounts = await cfGet<Array<{ id: string }>>(token, "/accounts");
|
|
269
|
-
* ```
|
|
270
|
-
*/
|
|
271
|
-
const cfGet = async (token, path) => {
|
|
272
|
-
const response = await fetch(`${API_BASE}${path}`, { headers: {
|
|
273
|
-
Authorization: `Bearer ${token}`,
|
|
274
|
-
"Content-Type": "application/json"
|
|
275
|
-
} });
|
|
276
|
-
const body = await response.json();
|
|
277
|
-
if (!response.ok || !body.success) {
|
|
278
|
-
const detail = body.errors?.map((error) => error.message).join("; ") || `HTTP ${response.status}`;
|
|
279
|
-
throw new Error(`[moku-worker] Cloudflare API request failed (${path}): ${detail}`);
|
|
280
|
-
}
|
|
281
|
-
return body.result;
|
|
282
|
-
};
|
|
283
|
-
/**
|
|
284
|
-
* Resolve the Cloudflare account (id + display name) accessible to the token. Used when the
|
|
285
|
-
* consumer did not pin CLOUDFLARE_ACCOUNT_ID; the first accessible account is chosen.
|
|
286
|
-
*
|
|
287
|
-
* @param token - The Cloudflare API token.
|
|
288
|
-
* @returns The resolved account id and name.
|
|
289
|
-
* @throws {Error} When the token can access no account.
|
|
290
|
-
* @example
|
|
291
|
-
* ```ts
|
|
292
|
-
* const { id, name } = await resolveAccount(token);
|
|
293
|
-
* ```
|
|
294
|
-
*/
|
|
295
|
-
const resolveAccount = async (token) => {
|
|
296
|
-
const first = (await cfGet(token, "/accounts"))[0];
|
|
297
|
-
if (!first) throw new Error("[moku-worker] No Cloudflare account is accessible with this API token.");
|
|
298
|
-
return {
|
|
299
|
-
id: first.id,
|
|
300
|
-
name: first.name
|
|
301
|
-
};
|
|
302
|
-
};
|
|
303
|
-
/**
|
|
304
|
-
* Verify a Cloudflare API token via `GET /user/tokens/verify`. Returns its status (`"active"` for
|
|
305
|
-
* a usable token); throws (via cfGet) when the token is rejected outright (401/invalid).
|
|
306
|
-
*
|
|
307
|
-
* @param token - The Cloudflare API token to verify.
|
|
308
|
-
* @returns The token status string reported by Cloudflare.
|
|
309
|
-
* @throws {Error} When the verify request fails (invalid/expired token).
|
|
310
|
-
* @example
|
|
311
|
-
* ```ts
|
|
312
|
-
* const { status } = await verifyToken(token); // status === "active"
|
|
313
|
-
* ```
|
|
314
|
-
*/
|
|
315
|
-
const verifyToken = async (token) => {
|
|
316
|
-
return { status: (await cfGet(token, "/user/tokens/verify")).status };
|
|
317
|
-
};
|
|
318
|
-
/**
|
|
319
|
-
* List the resources that already exist in the account, querying ONLY the kinds the app declares
|
|
320
|
-
* (one request per declared kind, in parallel), indexed for the preflight diff. Scoping to the
|
|
321
|
-
* declared kinds keeps the API token minimal — an app with only KV never lists (and so never needs
|
|
322
|
-
* read permission on) D1, R2, or Queues.
|
|
323
|
-
*
|
|
324
|
-
* @param token - The Cloudflare API token.
|
|
325
|
-
* @param accountId - The Cloudflare account id to scope the listings to.
|
|
326
|
-
* @param kinds - The resource kinds present in the manifest (the only kinds queried).
|
|
327
|
-
* @returns The existing resources, indexed by kind (un-queried kinds resolve empty).
|
|
328
|
-
* @throws {Error} When any listing request fails.
|
|
329
|
-
* @example
|
|
330
|
-
* ```ts
|
|
331
|
-
* const existing = await listExisting(token, accountId, new Set(["kv", "d1"]));
|
|
332
|
-
* if (existing.kv.has("SESSIONS")) { ... }
|
|
333
|
-
* ```
|
|
334
|
-
*/
|
|
335
|
-
const listExisting = async (token, accountId, kinds) => {
|
|
336
|
-
const base = `/accounts/${accountId}`;
|
|
337
|
-
const [kv, d1, r2, queues] = await Promise.all([
|
|
338
|
-
kinds.has("kv") ? cfGet(token, `${base}/storage/kv/namespaces`) : Promise.resolve([]),
|
|
339
|
-
kinds.has("d1") ? cfGet(token, `${base}/d1/database`) : Promise.resolve([]),
|
|
340
|
-
kinds.has("r2") ? cfGet(token, `${base}/r2/buckets`) : Promise.resolve({}),
|
|
341
|
-
kinds.has("queue") ? cfGet(token, `${base}/queues`) : Promise.resolve([])
|
|
342
|
-
]);
|
|
343
|
-
return {
|
|
344
|
-
kv: new Map(kv.map((namespace) => [namespace.title, namespace.id])),
|
|
345
|
-
d1: new Map(d1.map((database) => [database.name, database.uuid])),
|
|
346
|
-
r2: new Set((r2.buckets ?? []).map((bucket) => bucket.name)),
|
|
347
|
-
queue: new Set(queues.map((queue) => queue.queue_name))
|
|
348
|
-
};
|
|
349
|
-
};
|
|
350
|
-
//#endregion
|
|
351
|
-
//#region src/plugins/deploy/auth/verify.ts
|
|
352
|
-
/**
|
|
353
|
-
* @file deploy plugin — `.env` token verification + account resolution.
|
|
354
|
-
*
|
|
355
|
-
* Reads CLOUDFLARE_API_TOKEN via ctx.env, verifies it is active against the Cloudflare API, and
|
|
356
|
-
* resolves the account. Emits auth:verified. Throws a branded, actionable error (pointing at
|
|
357
|
-
* `auth setup`) when the token is absent, invalid, or inactive — never an interactive login.
|
|
358
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
359
|
-
*/
|
|
360
|
-
/** Branded hint appended to every auth failure so the user knows the next step. */
|
|
361
|
-
const SETUP_HINT = "Run `auth setup` for the exact token to create.";
|
|
362
|
-
/**
|
|
363
|
-
* Verify the `.env` Cloudflare API token and resolve its account.
|
|
364
|
-
*
|
|
365
|
-
* @param ctx - The deploy plugin context (env + emit).
|
|
366
|
-
* @returns The verified auth status (account + id).
|
|
367
|
-
* @throws {Error} When the token is absent, invalid/expired, or not active.
|
|
368
|
-
* @example
|
|
369
|
-
* ```ts
|
|
370
|
-
* const { account, accountId } = await verifyAuth(ctx);
|
|
371
|
-
* ```
|
|
372
|
-
*/
|
|
373
|
-
const verifyAuth = async (ctx) => {
|
|
374
|
-
const token = ctx.env.get("CLOUDFLARE_API_TOKEN");
|
|
375
|
-
if (token === void 0 || token === "") throw new Error(`[moku-worker] CLOUDFLARE_API_TOKEN is not set. ${SETUP_HINT}`);
|
|
376
|
-
let status;
|
|
377
|
-
try {
|
|
378
|
-
({status} = await verifyToken(token));
|
|
379
|
-
} catch (error) {
|
|
380
|
-
throw new Error(`[moku-worker] Cloudflare API token is invalid or expired. ${SETUP_HINT}`, { cause: error });
|
|
381
|
-
}
|
|
382
|
-
if (status !== "active") throw new Error(`[moku-worker] Cloudflare API token is "${status}", not active. ${SETUP_HINT}`);
|
|
383
|
-
const pinnedAccountId = ctx.env.get("CLOUDFLARE_ACCOUNT_ID");
|
|
384
|
-
const account = pinnedAccountId === void 0 || pinnedAccountId === "" ? await resolveAccount(token) : {
|
|
385
|
-
id: pinnedAccountId,
|
|
386
|
-
name: pinnedAccountId
|
|
387
|
-
};
|
|
388
|
-
ctx.emit("auth:verified", {
|
|
389
|
-
account: account.name,
|
|
390
|
-
accountId: account.id,
|
|
391
|
-
scopes: []
|
|
392
|
-
});
|
|
393
|
-
return {
|
|
394
|
-
ok: true,
|
|
395
|
-
account: account.name,
|
|
396
|
-
accountId: account.id,
|
|
397
|
-
scopes: []
|
|
398
|
-
};
|
|
399
|
-
};
|
|
400
|
-
//#endregion
|
|
401
|
-
//#region src/plugins/deploy/runner.ts
|
|
402
|
-
/**
|
|
403
|
-
* @file deploy plugin — wrangler subprocess wrapper (node:child_process).
|
|
404
|
-
*
|
|
405
|
-
* Spawns `wrangler` with the given args and resolves the deployed URL
|
|
406
|
-
* (extracted from stdout for `wrangler deploy`), or the full stdout for other verbs.
|
|
407
|
-
* This module is node-only; never imported by the runtime Worker bundle.
|
|
408
|
-
*/
|
|
409
|
-
/**
|
|
410
|
-
* Extract the deployed URL from `wrangler deploy` stdout.
|
|
411
|
-
* Wrangler prints a line like: "Published my-worker (1.23 sec) https://..."
|
|
412
|
-
* or "Deployed my-worker (1.23 sec) https://...".
|
|
413
|
-
*
|
|
414
|
-
* @param output - The combined stdout from wrangler deploy.
|
|
415
|
-
* @returns The deployed URL, or empty string when not found.
|
|
416
|
-
* @example
|
|
417
|
-
* ```ts
|
|
418
|
-
* extractDeployedUrl("Deployed my-worker (0.5 sec) https://my-worker.workers.dev");
|
|
419
|
-
* // "https://my-worker.workers.dev"
|
|
420
|
-
* ```
|
|
421
|
-
*/
|
|
422
|
-
const extractDeployedUrl = (output) => {
|
|
423
|
-
return /https:\/\/[^\s]+\.workers\.dev[^\s]*/u.exec(output)?.[0] ?? "";
|
|
424
|
-
};
|
|
425
|
-
/**
|
|
426
|
-
* Spawn `wrangler` with the given args and resolve the output string.
|
|
427
|
-
* For `wrangler deploy`, the resolved value is the deployed URL parsed from stdout.
|
|
428
|
-
* For all other verbs (dev, kv namespace create, etc.), the resolved value is stdout.
|
|
429
|
-
*
|
|
430
|
-
* @param args - Wrangler CLI arguments (e.g. ["deploy", "--config", "wrangler.jsonc"]).
|
|
431
|
-
* @returns Resolves with the deployed URL (deploy verb) or full stdout (other verbs).
|
|
432
|
-
* @throws {Error} When wrangler exits with a non-zero code.
|
|
433
|
-
* @example
|
|
434
|
-
* ```ts
|
|
435
|
-
* const url = await runWrangler(["deploy", "--config", "wrangler.jsonc"]);
|
|
436
|
-
* await runWrangler(["kv", "namespace", "create", "CACHE"]);
|
|
437
|
-
* ```
|
|
438
|
-
*/
|
|
439
|
-
const runWrangler = (args) => new Promise((resolve, reject) => {
|
|
440
|
-
const chunks = [];
|
|
441
|
-
const errChunks = [];
|
|
442
|
-
const child = (0, node_child_process.spawn)("wrangler", args, {
|
|
443
|
-
env: { ...process.env },
|
|
444
|
-
stdio: [
|
|
445
|
-
"ignore",
|
|
446
|
-
"pipe",
|
|
447
|
-
"pipe"
|
|
448
|
-
]
|
|
449
|
-
});
|
|
450
|
-
child.stdout.on("data", (chunk) => {
|
|
451
|
-
chunks.push(chunk);
|
|
452
|
-
});
|
|
453
|
-
child.stderr.on("data", (chunk) => {
|
|
454
|
-
errChunks.push(chunk);
|
|
455
|
-
});
|
|
456
|
-
child.on("error", (err) => {
|
|
457
|
-
reject(/* @__PURE__ */ new Error(`[moku-worker] Failed to spawn wrangler.\n ${err.message}`));
|
|
458
|
-
});
|
|
459
|
-
child.on("close", (code) => {
|
|
460
|
-
const stdout = Buffer.concat(chunks).toString("utf8");
|
|
461
|
-
const stderr = Buffer.concat(errChunks).toString("utf8");
|
|
462
|
-
if (code !== 0) {
|
|
463
|
-
reject(/* @__PURE__ */ new Error(`[moku-worker] wrangler exited with code ${String(code)}.\n ${stderr || stdout}`));
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
resolve(args[0] === "deploy" ? extractDeployedUrl(stdout) : stdout);
|
|
467
|
-
});
|
|
468
|
-
});
|
|
469
|
-
/**
|
|
470
|
-
* Spawn `wrangler` with the given args, inheriting stdio so its output streams live to the user's
|
|
471
|
-
* terminal (used by the generic passthrough and long-lived commands like `tail`).
|
|
472
|
-
*
|
|
473
|
-
* @param args - Wrangler CLI arguments (e.g. ["kv", "namespace", "list"]).
|
|
474
|
-
* @returns Resolves once wrangler exits successfully.
|
|
475
|
-
* @throws {Error} When wrangler cannot be spawned or exits non-zero.
|
|
476
|
-
* @example
|
|
477
|
-
* ```ts
|
|
478
|
-
* await runWranglerInherit(["kv", "namespace", "list"]);
|
|
479
|
-
* ```
|
|
480
|
-
*/
|
|
481
|
-
const runWranglerInherit = (args) => {
|
|
482
|
-
return new Promise((resolve, reject) => {
|
|
483
|
-
const child = (0, node_child_process.spawn)("wrangler", args, { stdio: "inherit" });
|
|
484
|
-
child.on("error", (error) => {
|
|
485
|
-
reject(/* @__PURE__ */ new Error(`[moku-worker] Failed to spawn wrangler.\n ${error.message}`));
|
|
486
|
-
});
|
|
487
|
-
child.on("close", (code) => {
|
|
488
|
-
if (code === 0) {
|
|
489
|
-
resolve();
|
|
490
|
-
return;
|
|
491
|
-
}
|
|
492
|
-
reject(/* @__PURE__ */ new Error(`[moku-worker] wrangler exited with code ${String(code)}.`));
|
|
493
|
-
});
|
|
494
|
-
});
|
|
495
|
-
};
|
|
496
|
-
//#endregion
|
|
497
|
-
//#region src/plugins/deploy/dev/build.ts
|
|
498
|
-
/**
|
|
499
|
-
* @file deploy plugin — dev site-rebuild resolution.
|
|
500
|
-
*
|
|
501
|
-
* Resolves HOW to rebuild the Moku web site on change: the in-process `webBuild` hook (preferred,
|
|
502
|
-
* fast, typed — passed call-time from the consumer's script or set as a config default) → a
|
|
503
|
-
* `buildCommand` shell string → an auto-detected `scripts/build.ts`. When nothing is configured,
|
|
504
|
-
* dev serves the worker only and says so. Subprocesses inherit the parent env by default.
|
|
505
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
506
|
-
*/
|
|
507
|
-
/** Convention build script auto-detected when no webBuild/buildCommand is configured. */
|
|
508
|
-
const AUTO_DETECT = "scripts/build.ts";
|
|
509
|
-
/**
|
|
510
|
-
* Run a shell build command, resolving on a zero exit and rejecting otherwise.
|
|
511
|
-
*
|
|
512
|
-
* @param command - The shell command to run (the consumer's own configured build).
|
|
513
|
-
* @returns Resolves once the command exits successfully.
|
|
514
|
-
* @throws {Error} When the command fails to start or exits non-zero.
|
|
515
|
-
* @example
|
|
516
|
-
* ```ts
|
|
517
|
-
* await runShellBuild("bun run scripts/build.ts");
|
|
518
|
-
* ```
|
|
519
|
-
*/
|
|
520
|
-
const runShellBuild = (command) => {
|
|
521
|
-
return new Promise((resolve, reject) => {
|
|
522
|
-
const child = (0, node_child_process.spawn)(command, {
|
|
523
|
-
shell: true,
|
|
524
|
-
stdio: "inherit"
|
|
525
|
-
});
|
|
526
|
-
child.on("error", (error) => {
|
|
527
|
-
reject(/* @__PURE__ */ new Error(`[moku-worker] site build failed to start.\n ${error.message}`));
|
|
528
|
-
});
|
|
529
|
-
child.on("close", (code) => {
|
|
530
|
-
if (code === 0) {
|
|
531
|
-
resolve();
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
reject(/* @__PURE__ */ new Error(`[moku-worker] site build exited with code ${String(code)}.`));
|
|
535
|
-
});
|
|
536
|
-
});
|
|
537
|
-
};
|
|
538
|
-
/**
|
|
539
|
-
* Rebuild the Moku web site using the resolved strategy: the call-time `webBuild` hook (the
|
|
540
|
-
* script-driven path), else the `webBuild` config default, else the `buildCommand` shell string,
|
|
541
|
-
* else an auto-detected `scripts/build.ts`. A hook's result is normalized to a `{ files }` count
|
|
542
|
-
* (0 when the hook reports none, and for the shell path where it is unknown).
|
|
543
|
-
*
|
|
544
|
-
* @param ctx - The deploy plugin context (config + emit).
|
|
545
|
-
* @param webBuild - Optional call-time web build hook (takes precedence over `ctx.config.webBuild`).
|
|
546
|
-
* @returns The rebuilt file count (0 for the shell path / a countless hook).
|
|
547
|
-
* @throws {Error} When the resolved shell build fails.
|
|
548
|
-
* @example
|
|
549
|
-
* ```ts
|
|
550
|
-
* const { files } = await buildSite(ctx, () => web.cli.build());
|
|
551
|
-
* ```
|
|
552
|
-
*/
|
|
553
|
-
const buildSite = async (ctx, webBuild) => {
|
|
554
|
-
const hook = webBuild ?? ctx.config.webBuild;
|
|
555
|
-
if (hook !== void 0) return { files: (await hook())?.files ?? 0 };
|
|
556
|
-
const command = ctx.config.buildCommand || ((0, node_fs.existsSync)(AUTO_DETECT) ? `bun run ${AUTO_DETECT}` : "");
|
|
557
|
-
if (command === "") {
|
|
558
|
-
ctx.emit("dev:error", { message: "No site build configured (pass webBuild or set buildCommand); serving worker only." });
|
|
559
|
-
return { files: 0 };
|
|
560
|
-
}
|
|
561
|
-
await runShellBuild(command);
|
|
562
|
-
return { files: 0 };
|
|
563
|
-
};
|
|
564
|
-
//#endregion
|
|
565
|
-
//#region src/plugins/deploy/dev/watch.ts
|
|
566
|
-
/**
|
|
567
|
-
* @file deploy plugin — debounced filesystem watcher for dev.
|
|
568
|
-
*
|
|
569
|
-
* Watches the top-level directories implied by the config globs (recursive) and fires a debounced
|
|
570
|
-
* change callback with the last changed path. Uses node:fs.watch — no extra dependency.
|
|
571
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
572
|
-
*/
|
|
573
|
-
/**
|
|
574
|
-
* Derive the set of top-level directories to watch from glob patterns.
|
|
575
|
-
*
|
|
576
|
-
* @param globs - Watch globs (e.g. ["src/**\/*.ts", "public/**\/*"]).
|
|
577
|
-
* @returns The distinct top-level directories (e.g. ["src", "public"]).
|
|
578
|
-
* @example
|
|
579
|
-
* ```ts
|
|
580
|
-
* watchDirectories(["src/**\/*.ts", "public/**\/*"]); // ["src", "public"]
|
|
581
|
-
* ```
|
|
582
|
-
*/
|
|
583
|
-
const watchDirectories = (globs) => {
|
|
584
|
-
const directories = /* @__PURE__ */ new Set();
|
|
585
|
-
for (const glob of globs) {
|
|
586
|
-
const globStart = glob.search(/[*?[{]/u);
|
|
587
|
-
const top = (globStart === -1 ? node_path.default.dirname(glob) : glob.slice(0, globStart)).split(/[/\\]/u).find((segment) => segment !== "") ?? ".";
|
|
588
|
-
directories.add(top);
|
|
589
|
-
}
|
|
590
|
-
return [...directories];
|
|
591
|
-
};
|
|
592
|
-
/**
|
|
593
|
-
* Watch the directories implied by `globs` and fire `onChange` (debounced by `debounceMs`) with
|
|
594
|
-
* the last changed path. Missing directories are skipped silently.
|
|
595
|
-
*
|
|
596
|
-
* @param globs - Watch globs.
|
|
597
|
-
* @param debounceMs - Coalesce rapid changes into one callback within this window.
|
|
598
|
-
* @param onChange - Called with the last changed path after the debounce settles.
|
|
599
|
-
* @returns A handle whose close() stops all watchers and cancels any pending callback.
|
|
600
|
-
* @example
|
|
601
|
-
* ```ts
|
|
602
|
-
* const handle = watchPaths(["src/**\/*.ts"], 120, p => rebuild(p));
|
|
603
|
-
* handle.close();
|
|
604
|
-
* ```
|
|
605
|
-
*/
|
|
606
|
-
const watchPaths = (globs, debounceMs, onChange) => {
|
|
607
|
-
let timer;
|
|
608
|
-
let lastPath = "";
|
|
609
|
-
const fire = (changedPath) => {
|
|
610
|
-
lastPath = changedPath;
|
|
611
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
612
|
-
timer = setTimeout(() => {
|
|
613
|
-
onChange(lastPath);
|
|
614
|
-
}, debounceMs);
|
|
615
|
-
};
|
|
616
|
-
const watchers = [];
|
|
617
|
-
for (const directory of watchDirectories(globs)) {
|
|
618
|
-
if (!(0, node_fs.existsSync)(directory)) continue;
|
|
619
|
-
watchers.push((0, node_fs.watch)(directory, { recursive: true }, (_event, filename) => {
|
|
620
|
-
if (filename !== null) fire(node_path.default.join(directory, filename.toString()));
|
|
621
|
-
}));
|
|
622
|
-
}
|
|
623
|
-
return { close: () => {
|
|
624
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
625
|
-
for (const watcher of watchers) watcher.close();
|
|
626
|
-
} };
|
|
627
|
-
};
|
|
628
|
-
//#endregion
|
|
629
|
-
//#region src/plugins/deploy/dev/runner.ts
|
|
630
|
-
/**
|
|
631
|
-
* @file deploy plugin — dev watch/recompile orchestrator.
|
|
632
|
-
*
|
|
633
|
-
* One long-lived session: cold-build the Moku site, optionally apply local D1 migrations, spawn
|
|
634
|
-
* `wrangler dev --live-reload` ONCE, then watch the site sources and rebuild on change (wrangler's
|
|
635
|
-
* asset server live-reloads the browser). Build failures keep the session serving the last good
|
|
636
|
-
* build. Tears down cleanly on SIGINT. Side-effecting work is injected via DevDeps so the
|
|
637
|
-
* orchestration is unit-testable without real processes, watchers, or signals.
|
|
638
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
639
|
-
*/
|
|
640
|
-
/**
|
|
641
|
-
* Spawn the long-lived `wrangler dev` child (inherits the parent env; non-blocking).
|
|
642
|
-
*
|
|
643
|
-
* @param args - The `wrangler dev …` arguments.
|
|
644
|
-
* @returns A handle exposing kill().
|
|
645
|
-
* @example
|
|
646
|
-
* ```ts
|
|
647
|
-
* const child = spawnWranglerDev(["dev", "--port", "8787"]);
|
|
648
|
-
* ```
|
|
649
|
-
*/
|
|
650
|
-
const spawnWranglerDev = (args) => {
|
|
651
|
-
const child = (0, node_child_process.spawn)("wrangler", args, { stdio: "inherit" });
|
|
652
|
-
return { kill: () => child.kill() };
|
|
653
|
-
};
|
|
654
|
-
/**
|
|
655
|
-
* Resolve when the user first interrupts the dev session (SIGINT).
|
|
656
|
-
*
|
|
657
|
-
* @returns A promise that settles on the first SIGINT.
|
|
658
|
-
* @example
|
|
659
|
-
* ```ts
|
|
660
|
-
* await waitForSigint();
|
|
661
|
-
* ```
|
|
662
|
-
*/
|
|
663
|
-
const waitForSigint = () => {
|
|
664
|
-
return new Promise((resolve) => {
|
|
665
|
-
process.once("SIGINT", () => {
|
|
666
|
-
resolve();
|
|
667
|
-
});
|
|
668
|
-
});
|
|
669
|
-
};
|
|
670
|
-
/**
|
|
671
|
-
* Wall-clock timestamp in ms (extracted so realDevDeps holds only named references).
|
|
672
|
-
*
|
|
673
|
-
* @returns The current time in milliseconds.
|
|
674
|
-
* @example
|
|
675
|
-
* ```ts
|
|
676
|
-
* const t = nowMs();
|
|
677
|
-
* ```
|
|
678
|
-
*/
|
|
679
|
-
const nowMs = () => Date.now();
|
|
680
|
-
/**
|
|
681
|
-
* Build the real (side-effecting) dev deps used by api.dev(). Subprocesses inherit the parent env.
|
|
682
|
-
*
|
|
683
|
-
* @returns The production DevDeps (real spawn / fs.watch / SIGINT / Date.now).
|
|
684
|
-
* @example
|
|
685
|
-
* ```ts
|
|
686
|
-
* await runDev(ctx, opts, realDevDeps());
|
|
687
|
-
* ```
|
|
688
|
-
*/
|
|
689
|
-
const realDevDeps = () => ({
|
|
690
|
-
build: buildSite,
|
|
691
|
-
runWrangler,
|
|
692
|
-
spawnDev: spawnWranglerDev,
|
|
693
|
-
watch: watchPaths,
|
|
694
|
-
untilSignal: waitForSigint,
|
|
695
|
-
now: nowMs
|
|
696
|
-
});
|
|
697
|
-
/**
|
|
698
|
-
* The d1 binding to migrate locally, when a d1 plugin is present in the app.
|
|
699
|
-
*
|
|
700
|
-
* @param ctx - The deploy plugin context.
|
|
701
|
-
* @returns The d1 binding name, or undefined when no d1 plugin is present.
|
|
702
|
-
* @example
|
|
703
|
-
* ```ts
|
|
704
|
-
* const binding = d1Binding(ctx); // "DB" | undefined
|
|
705
|
-
* ```
|
|
706
|
-
*/
|
|
707
|
-
const d1Binding = (ctx) => ctx.has("d1") ? ctx.require(require_storage.d1Plugin).deployManifest().binding : void 0;
|
|
708
|
-
/**
|
|
709
|
-
* Rebuild the site once and announce the result. A failed build keeps the session alive (it just
|
|
710
|
-
* emits dev:error and serves the last good build).
|
|
711
|
-
*
|
|
712
|
-
* @param ctx - The deploy plugin context.
|
|
713
|
-
* @param deps - The injected dev deps.
|
|
714
|
-
* @param changedPath - The path that triggered the rebuild.
|
|
715
|
-
* @param webBuild - Optional call-time web build hook threaded into the rebuild.
|
|
716
|
-
* @returns Resolves once the rebuild attempt completes.
|
|
717
|
-
* @example
|
|
718
|
-
* ```ts
|
|
719
|
-
* await rebuild(ctx, deps, "src/app.tsx", () => web.cli.build());
|
|
720
|
-
* ```
|
|
721
|
-
*/
|
|
722
|
-
const rebuild = async (ctx, deps, changedPath, webBuild) => {
|
|
723
|
-
ctx.emit("dev:phase", {
|
|
724
|
-
phase: "rebuild",
|
|
725
|
-
detail: changedPath
|
|
726
|
-
});
|
|
727
|
-
const started = deps.now();
|
|
728
|
-
try {
|
|
729
|
-
const { files } = await deps.build(ctx, webBuild);
|
|
730
|
-
ctx.emit("dev:rebuilt", {
|
|
731
|
-
files,
|
|
732
|
-
ms: deps.now() - started
|
|
733
|
-
});
|
|
734
|
-
} catch (error) {
|
|
735
|
-
ctx.emit("dev:error", { message: error instanceof Error ? error.message : String(error) });
|
|
736
|
-
}
|
|
737
|
-
};
|
|
738
|
-
/**
|
|
739
|
-
* Run a long-lived dev session: cold build → (local d1 migrate) → spawn `wrangler dev` →
|
|
740
|
-
* watch + rebuild on change → teardown on signal.
|
|
741
|
-
*
|
|
742
|
-
* @param ctx - The deploy plugin context (config + emit + require/has).
|
|
743
|
-
* @param opts - Optional options.
|
|
744
|
-
* @param opts.port - Local dev port (default 8787).
|
|
745
|
-
* @param opts.webBuild - Web build hook (re)run on cold build + each change (e.g. `() => web.cli.build()`).
|
|
746
|
-
* @param deps - Injected side effects (real ones from realDevDeps in production).
|
|
747
|
-
* @returns Resolves when the session ends (SIGINT).
|
|
748
|
-
* @example
|
|
749
|
-
* ```ts
|
|
750
|
-
* await runDev(ctx, { port: 8787, webBuild: () => web.cli.build() }, realDevDeps());
|
|
751
|
-
* ```
|
|
752
|
-
*/
|
|
753
|
-
const runDev = async (ctx, opts, deps) => {
|
|
754
|
-
const port = opts?.port ?? 8787;
|
|
755
|
-
const webBuild = opts?.webBuild;
|
|
756
|
-
ctx.emit("dev:phase", {
|
|
757
|
-
phase: "build",
|
|
758
|
-
detail: "site"
|
|
759
|
-
});
|
|
760
|
-
await deps.build(ctx, webBuild);
|
|
761
|
-
const binding = d1Binding(ctx);
|
|
762
|
-
if (ctx.config.migrateLocal && binding !== void 0) {
|
|
763
|
-
ctx.emit("dev:phase", {
|
|
764
|
-
phase: "migrate",
|
|
765
|
-
detail: "d1 (local)"
|
|
766
|
-
});
|
|
767
|
-
await deps.runWrangler([
|
|
768
|
-
"d1",
|
|
769
|
-
"migrations",
|
|
770
|
-
"apply",
|
|
771
|
-
binding,
|
|
772
|
-
"--local"
|
|
773
|
-
]);
|
|
774
|
-
}
|
|
775
|
-
ctx.emit("dev:phase", {
|
|
776
|
-
phase: "serve",
|
|
777
|
-
detail: `http://localhost:${String(port)}`
|
|
778
|
-
});
|
|
779
|
-
const child = deps.spawnDev([
|
|
780
|
-
"dev",
|
|
781
|
-
"--port",
|
|
782
|
-
String(port),
|
|
783
|
-
"--config",
|
|
784
|
-
ctx.config.configFile,
|
|
785
|
-
"--live-reload"
|
|
786
|
-
]);
|
|
787
|
-
const watcher = deps.watch(ctx.config.watch, ctx.config.debounceMs, (changedPath) => rebuild(ctx, deps, changedPath, webBuild));
|
|
788
|
-
await deps.untilSignal();
|
|
789
|
-
watcher.close();
|
|
790
|
-
child.kill();
|
|
791
|
-
ctx.emit("dev:phase", { phase: "stopped" });
|
|
792
|
-
};
|
|
793
|
-
//#endregion
|
|
794
|
-
//#region src/plugins/deploy/infra/plan.ts
|
|
795
|
-
/**
|
|
796
|
-
* Decide whether a single declared resource already exists in the account, recovering its id
|
|
797
|
-
* (kv/d1) when it does. Durable Objects are config-only (they ship with the script), so they are
|
|
798
|
-
* always treated as "missing" — provisioning them is a no-op that just records the binding.
|
|
799
|
-
*
|
|
800
|
-
* @param resource - The declared resource descriptor.
|
|
801
|
-
* @param existing - The indexed set of resources already in the account.
|
|
802
|
-
* @returns Whether it exists, plus the captured id for kv/d1.
|
|
803
|
-
* @example
|
|
804
|
-
* ```ts
|
|
805
|
-
* checkExisting({ kind: "kv", binding: "SESSIONS" }, existing); // { exists: true, id: "ns123" }
|
|
806
|
-
* ```
|
|
807
|
-
*/
|
|
808
|
-
const checkExisting = (resource, existing) => {
|
|
809
|
-
switch (resource.kind) {
|
|
810
|
-
case "kv": {
|
|
811
|
-
const id = existing.kv.get(resource.binding);
|
|
812
|
-
return id === void 0 ? { exists: false } : {
|
|
813
|
-
exists: true,
|
|
814
|
-
id
|
|
815
|
-
};
|
|
816
|
-
}
|
|
817
|
-
case "d1": {
|
|
818
|
-
const id = existing.d1.get(resource.binding);
|
|
819
|
-
return id === void 0 ? { exists: false } : {
|
|
820
|
-
exists: true,
|
|
821
|
-
id
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
case "r2": return { exists: existing.r2.has(resource.bucket) };
|
|
825
|
-
case "queue": return { exists: resource.producers.every((producer) => existing.queue.has(producer)) };
|
|
826
|
-
case "do": return { exists: false };
|
|
827
|
-
}
|
|
828
|
-
};
|
|
829
|
-
/**
|
|
830
|
-
* Run the read-only infra preflight: resolve the account, list existing resources, diff against
|
|
831
|
-
* the manifest, emit `provision:plan`, and return the plan. Writes nothing.
|
|
832
|
-
*
|
|
833
|
-
* @param ctx - The deploy plugin context (env + emit).
|
|
834
|
-
* @param manifest - The assembled (or caller-supplied) deploy manifest.
|
|
835
|
-
* @returns The infra plan: existing (with ids) vs missing resources.
|
|
836
|
-
* @throws {Error} When the token is absent/invalid or a Cloudflare listing fails.
|
|
837
|
-
* @example
|
|
838
|
-
* ```ts
|
|
839
|
-
* const plan = await planInfra(ctx, manifest);
|
|
840
|
-
* ```
|
|
841
|
-
*/
|
|
842
|
-
const planInfra = async (ctx, manifest) => {
|
|
843
|
-
const token = ctx.env.require("CLOUDFLARE_API_TOKEN");
|
|
844
|
-
const pinnedAccountId = ctx.env.get("CLOUDFLARE_ACCOUNT_ID");
|
|
845
|
-
const account = pinnedAccountId ? {
|
|
846
|
-
id: pinnedAccountId,
|
|
847
|
-
name: pinnedAccountId
|
|
848
|
-
} : await resolveAccount(token);
|
|
849
|
-
const kinds = /* @__PURE__ */ new Set();
|
|
850
|
-
for (const resource of manifest.resources) if (resource.kind !== "do") kinds.add(resource.kind);
|
|
851
|
-
const existing = await listExisting(token, account.id, kinds);
|
|
852
|
-
const exists = [];
|
|
853
|
-
const missing = [];
|
|
854
|
-
for (const resource of manifest.resources) {
|
|
855
|
-
const check = checkExisting(resource, existing);
|
|
856
|
-
if (check.exists) exists.push(check.id === void 0 ? { resource } : {
|
|
857
|
-
resource,
|
|
858
|
-
id: check.id
|
|
859
|
-
});
|
|
860
|
-
else missing.push(resource);
|
|
861
|
-
}
|
|
862
|
-
ctx.emit("provision:plan", {
|
|
863
|
-
exists: exists.length,
|
|
864
|
-
missing: missing.length,
|
|
865
|
-
account: account.name
|
|
866
|
-
});
|
|
867
|
-
return {
|
|
868
|
-
account: account.name,
|
|
869
|
-
accountId: account.id,
|
|
870
|
-
exists,
|
|
871
|
-
missing
|
|
872
|
-
};
|
|
873
|
-
};
|
|
874
|
-
//#endregion
|
|
875
|
-
//#region src/plugins/deploy/providers/d1.ts
|
|
876
|
-
/**
|
|
877
|
-
* @file deploy plugin — D1 provisioning adapter.
|
|
878
|
-
*
|
|
879
|
-
* Creates a Cloudflare D1 database via `wrangler d1 create <binding>`, captures the created
|
|
880
|
-
* database id from wrangler's output (so writeWranglerConfig can write a real `database_id`
|
|
881
|
-
* instead of an empty placeholder), and applies migrations when declared.
|
|
882
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
883
|
-
*/
|
|
884
|
-
/**
|
|
885
|
-
* Parse the created D1 database id from `wrangler d1 create` output.
|
|
886
|
-
* Wrangler prints the new binding as JSON (`"database_id": "..."`) or TOML
|
|
887
|
-
* (`database_id = "..."`); the leading boundary keeps the match anchored to the field name.
|
|
888
|
-
*
|
|
889
|
-
* @param output - Raw stdout from the wrangler create command.
|
|
890
|
-
* @returns The database id, or undefined when none is found.
|
|
891
|
-
* @example
|
|
892
|
-
* ```ts
|
|
893
|
-
* parseD1DatabaseId('{ "database_id": "uuid-1234" }'); // "uuid-1234"
|
|
894
|
-
* ```
|
|
895
|
-
*/
|
|
896
|
-
const parseD1DatabaseId = (output) => {
|
|
897
|
-
return /(?:^|[\s,{])"?database_id"?\s*[:=]\s*"([^"]+)"/m.exec(output)?.[1];
|
|
898
|
-
};
|
|
899
|
-
/**
|
|
900
|
-
* Provision a D1 database via `wrangler d1 create`, capture its id, and apply migrations.
|
|
901
|
-
*
|
|
902
|
-
* @param manifest - The D1 resource descriptor.
|
|
903
|
-
* @param _ci - Whether running non-interactively.
|
|
904
|
-
* @returns The captured database id when wrangler reported one, else an empty outcome.
|
|
905
|
-
* @example
|
|
906
|
-
* ```ts
|
|
907
|
-
* const { id } = await provisionD1({ kind: "d1", binding: "DB", migrations: "./migrations" }, false);
|
|
908
|
-
* ```
|
|
909
|
-
*/
|
|
910
|
-
const provisionD1 = async (manifest, _ci) => {
|
|
911
|
-
const id = parseD1DatabaseId(await runWrangler([
|
|
912
|
-
"d1",
|
|
913
|
-
"create",
|
|
914
|
-
manifest.binding
|
|
915
|
-
]));
|
|
916
|
-
if (manifest.migrations) await runWrangler([
|
|
917
|
-
"d1",
|
|
918
|
-
"migrations",
|
|
919
|
-
"apply",
|
|
920
|
-
manifest.binding,
|
|
921
|
-
"--local"
|
|
922
|
-
]);
|
|
923
|
-
return id ? { id } : {};
|
|
924
|
-
};
|
|
925
|
-
//#endregion
|
|
926
|
-
//#region src/plugins/deploy/providers/do.ts
|
|
927
|
-
/**
|
|
928
|
-
* Provision Durable Object bindings. DOs are config-driven (no `wrangler do create` command
|
|
929
|
-
* exists) — the actual binding entries are written by writeWranglerConfig. This function is
|
|
930
|
-
* a resolved no-op for the dispatch step.
|
|
931
|
-
*
|
|
932
|
-
* @param _manifest - The Durable Objects resource descriptor.
|
|
933
|
-
* @param _ci - Whether running non-interactively.
|
|
934
|
-
* @returns Resolves immediately (DOs are config-only provisioning).
|
|
935
|
-
* @example
|
|
936
|
-
* ```ts
|
|
937
|
-
* await provisionDurableObject({ kind: "do", bindings: { counter: "COUNTER" } }, false);
|
|
938
|
-
* ```
|
|
939
|
-
*/
|
|
940
|
-
const provisionDurableObject = async (_manifest, _ci) => {};
|
|
941
|
-
//#endregion
|
|
942
|
-
//#region src/plugins/deploy/providers/kv.ts
|
|
943
|
-
/**
|
|
944
|
-
* @file deploy plugin — KV provisioning adapter.
|
|
945
|
-
*
|
|
946
|
-
* Creates a Cloudflare KV namespace via `wrangler kv namespace create <binding>` and captures
|
|
947
|
-
* the created namespace id from wrangler's output, so writeWranglerConfig can write a real `id`
|
|
948
|
-
* (not an empty placeholder) into the generated wrangler config — otherwise the binding resolves
|
|
949
|
-
* to nothing at runtime. Node-only; never imported by the runtime Worker bundle.
|
|
950
|
-
*/
|
|
951
|
-
/**
|
|
952
|
-
* Parse the created KV namespace id from `wrangler kv namespace create` output.
|
|
953
|
-
* Wrangler prints the new binding as JSON (`"id": "..."`) or TOML (`id = "..."`); the leading
|
|
954
|
-
* boundary (start / whitespace / `{` / `,`) keeps the match off a longer identifier such as
|
|
955
|
-
* `kv_namespace_id`.
|
|
956
|
-
*
|
|
957
|
-
* @param output - Raw stdout from the wrangler create command.
|
|
958
|
-
* @returns The namespace id, or undefined when none is found.
|
|
959
|
-
* @example
|
|
960
|
-
* ```ts
|
|
961
|
-
* parseKvNamespaceId('{ "id": "abc123" }'); // "abc123"
|
|
962
|
-
* ```
|
|
963
|
-
*/
|
|
964
|
-
const parseKvNamespaceId = (output) => {
|
|
965
|
-
return /(?:^|[\s,{])"?id"?\s*[:=]\s*"([^"]+)"/m.exec(output)?.[1];
|
|
966
|
-
};
|
|
967
|
-
/**
|
|
968
|
-
* Provision a KV namespace via `wrangler kv namespace create` and capture its id.
|
|
969
|
-
*
|
|
970
|
-
* @param manifest - The KV resource descriptor.
|
|
971
|
-
* @param _ci - Whether running non-interactively (passed through; wrangler respects env vars).
|
|
972
|
-
* @returns The captured namespace id when wrangler reported one, else an empty outcome.
|
|
973
|
-
* @example
|
|
974
|
-
* ```ts
|
|
975
|
-
* const { id } = await provisionKv({ kind: "kv", binding: "CACHE" }, false);
|
|
976
|
-
* ```
|
|
977
|
-
*/
|
|
978
|
-
const provisionKv = async (manifest, _ci) => {
|
|
979
|
-
const id = parseKvNamespaceId(await runWrangler([
|
|
980
|
-
"kv",
|
|
981
|
-
"namespace",
|
|
982
|
-
"create",
|
|
983
|
-
manifest.binding
|
|
984
|
-
]));
|
|
985
|
-
return id ? { id } : {};
|
|
986
|
-
};
|
|
987
|
-
//#endregion
|
|
988
|
-
//#region src/plugins/deploy/providers/queues.ts
|
|
989
|
-
/**
|
|
990
|
-
* @file deploy plugin — Queues provisioning adapter.
|
|
991
|
-
*
|
|
992
|
-
* Creates Cloudflare Queues via `wrangler queues create <name>` for each producer.
|
|
993
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
994
|
-
*/
|
|
995
|
-
/**
|
|
996
|
-
* Provision queues via `wrangler queues create` for each declared producer.
|
|
997
|
-
*
|
|
998
|
-
* @param manifest - The queue resource descriptor.
|
|
999
|
-
* @param _ci - Whether running non-interactively.
|
|
1000
|
-
* @returns Resolves once all queues are created.
|
|
1001
|
-
* @example
|
|
1002
|
-
* ```ts
|
|
1003
|
-
* await provisionQueue({ kind: "queue", producers: ["orders"] }, false);
|
|
1004
|
-
* ```
|
|
1005
|
-
*/
|
|
1006
|
-
const provisionQueue = async (manifest, _ci) => {
|
|
1007
|
-
for (const producer of manifest.producers) await runWrangler([
|
|
1008
|
-
"queues",
|
|
1009
|
-
"create",
|
|
1010
|
-
producer
|
|
1011
|
-
]);
|
|
1012
|
-
};
|
|
1013
|
-
//#endregion
|
|
1014
|
-
//#region src/plugins/deploy/providers/r2.ts
|
|
1015
|
-
/**
|
|
1016
|
-
* @file deploy plugin — R2 provisioning + asset upload adapter.
|
|
1017
|
-
*
|
|
1018
|
-
* Provides two exports:
|
|
1019
|
-
* - `provisionR2`: creates an R2 bucket via `wrangler r2 bucket create`.
|
|
1020
|
-
* - `uploadDirToR2`: walks a directory recursively and uploads each file via
|
|
1021
|
-
* `wrangler r2 object put`, returning the uploaded file count.
|
|
1022
|
-
*
|
|
1023
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
1024
|
-
*/
|
|
1025
|
-
/**
|
|
1026
|
-
* Provision an R2 bucket via `wrangler r2 bucket create`.
|
|
1027
|
-
*
|
|
1028
|
-
* @param manifest - The R2 resource descriptor.
|
|
1029
|
-
* @param _ci - Whether running non-interactively.
|
|
1030
|
-
* @returns Resolves once the bucket is created.
|
|
1031
|
-
* @example
|
|
1032
|
-
* ```ts
|
|
1033
|
-
* await provisionR2({ kind: "r2", bucket: "ASSETS" }, false);
|
|
1034
|
-
* ```
|
|
1035
|
-
*/
|
|
1036
|
-
const provisionR2 = async (manifest, _ci) => {
|
|
1037
|
-
await runWrangler([
|
|
1038
|
-
"r2",
|
|
1039
|
-
"bucket",
|
|
1040
|
-
"create",
|
|
1041
|
-
manifest.bucket
|
|
1042
|
-
]);
|
|
1043
|
-
};
|
|
1044
|
-
/**
|
|
1045
|
-
* Walk a directory recursively and return all file paths (absolute).
|
|
1046
|
-
*
|
|
1047
|
-
* @param directory - Directory path to walk.
|
|
1048
|
-
* @returns All file paths found under the directory.
|
|
1049
|
-
* @example
|
|
1050
|
-
* ```ts
|
|
1051
|
-
* const files = await walkDir("./public");
|
|
1052
|
-
* ```
|
|
1053
|
-
*/
|
|
1054
|
-
const walkDir = async (directory) => {
|
|
1055
|
-
const entries = await (0, node_fs_promises.readdir)(directory);
|
|
1056
|
-
const results = [];
|
|
1057
|
-
for (const entry of entries) {
|
|
1058
|
-
const fullPath = node_path.default.join(directory, entry);
|
|
1059
|
-
if ((await (0, node_fs_promises.stat)(fullPath)).isDirectory()) {
|
|
1060
|
-
const nested = await walkDir(fullPath);
|
|
1061
|
-
results.push(...nested);
|
|
1062
|
-
} else results.push(fullPath);
|
|
1063
|
-
}
|
|
1064
|
-
return results;
|
|
1065
|
-
};
|
|
1066
|
-
/**
|
|
1067
|
-
* Upload a directory to an R2 bucket and return the uploaded file count.
|
|
1068
|
-
* Each file is uploaded via `wrangler r2 object put <bucket>/<key> --file <path>`.
|
|
1069
|
-
*
|
|
1070
|
-
* @param bucket - The R2 bucket binding name.
|
|
1071
|
-
* @param directory - The directory to upload.
|
|
1072
|
-
* @returns The number of files uploaded.
|
|
1073
|
-
* @example
|
|
1074
|
-
* ```ts
|
|
1075
|
-
* const count = await uploadDirToR2("ASSETS", "./public");
|
|
1076
|
-
* ```
|
|
1077
|
-
*/
|
|
1078
|
-
const uploadDirToR2 = async (bucket, directory) => {
|
|
1079
|
-
const files = await walkDir(directory);
|
|
1080
|
-
for (const filePath of files) await runWrangler([
|
|
1081
|
-
"r2",
|
|
1082
|
-
"object",
|
|
1083
|
-
"put",
|
|
1084
|
-
`${bucket}/${node_path.default.relative(directory, filePath)}`,
|
|
1085
|
-
"--file",
|
|
1086
|
-
filePath
|
|
1087
|
-
]);
|
|
1088
|
-
return files.length;
|
|
1089
|
-
};
|
|
1090
|
-
//#endregion
|
|
1091
|
-
//#region src/plugins/deploy/providers/index.ts
|
|
1092
|
-
/**
|
|
1093
|
-
* Dispatch a resource descriptor to the matching provider's provisioning routine.
|
|
1094
|
-
*
|
|
1095
|
-
* @param resource - The resource descriptor to provision.
|
|
1096
|
-
* @param ci - Whether running non-interactively.
|
|
1097
|
-
* @returns The provisioning outcome — `{ id }` for kv/d1, `{}` for r2/queue/do.
|
|
1098
|
-
* @example
|
|
1099
|
-
* ```ts
|
|
1100
|
-
* const { id } = await provisionResource({ kind: "kv", binding: "CACHE" }, false);
|
|
1101
|
-
* await provisionResource({ kind: "r2", bucket: "ASSETS" }, false); // {}
|
|
1102
|
-
* ```
|
|
1103
|
-
*/
|
|
1104
|
-
const provisionResource = async (resource, ci) => {
|
|
1105
|
-
switch (resource.kind) {
|
|
1106
|
-
case "kv": return provisionKv(resource, ci);
|
|
1107
|
-
case "d1": return provisionD1(resource, ci);
|
|
1108
|
-
case "r2":
|
|
1109
|
-
await provisionR2(resource, ci);
|
|
1110
|
-
return {};
|
|
1111
|
-
case "queue":
|
|
1112
|
-
await provisionQueue(resource, ci);
|
|
1113
|
-
return {};
|
|
1114
|
-
case "do":
|
|
1115
|
-
await provisionDurableObject(resource, ci);
|
|
1116
|
-
return {};
|
|
1117
|
-
}
|
|
1118
|
-
};
|
|
1119
|
-
//#endregion
|
|
1120
|
-
//#region src/plugins/deploy/tty.ts
|
|
1121
|
-
/**
|
|
1122
|
-
* @file deploy plugin — TTY detection (isolated so the guided flow is testable).
|
|
1123
|
-
*
|
|
1124
|
-
* The guided deploy only prompts on an interactive terminal; in a pipe or CI it must never block
|
|
1125
|
-
* on stdin. Kept in its own module so tests can mock it without stubbing `process.stdout`.
|
|
1126
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
1127
|
-
*/
|
|
1128
|
-
/**
|
|
1129
|
-
* Whether stdout is an interactive TTY (so prompts are safe to show).
|
|
1130
|
-
*
|
|
1131
|
-
* @returns True when stdout is a terminal.
|
|
1132
|
-
* @example
|
|
1133
|
-
* ```ts
|
|
1134
|
-
* if (stdoutIsTty()) await prompts.confirm("Deploy?");
|
|
1135
|
-
* ```
|
|
1136
|
-
*/
|
|
1137
|
-
const stdoutIsTty = () => process.stdout.isTTY === true;
|
|
1138
|
-
//#endregion
|
|
1139
|
-
//#region src/plugins/deploy/wrangler-config.ts
|
|
1140
|
-
/**
|
|
1141
|
-
* @file deploy plugin — wrangler config generation + scaffold.
|
|
1142
|
-
*
|
|
1143
|
-
* Provides two exports:
|
|
1144
|
-
* - `writeWranglerConfig`: generates/updates a wrangler.jsonc file from an ExternalManifest.
|
|
1145
|
-
* Non-destructive: preserves existing top-level keys not managed by deploy.
|
|
1146
|
-
* - `scaffoldWranglerAndCi`: creates a minimal starter wrangler config when the file does not
|
|
1147
|
-
* exist yet; idempotent (leaves existing files untouched).
|
|
1148
|
-
*
|
|
1149
|
-
* Node-only; never imported by the runtime Worker bundle.
|
|
1150
|
-
*/
|
|
1151
|
-
/**
|
|
1152
|
-
* Strip JSONC line- and block-comments, then JSON.parse the result.
|
|
1153
|
-
*
|
|
1154
|
-
* @param source - Raw JSONC file contents.
|
|
1155
|
-
* @returns The parsed object.
|
|
1156
|
-
* @example
|
|
1157
|
-
* ```ts
|
|
1158
|
-
* const cfg = parseJsonc('{ "name": "w" } // trailing comment');
|
|
1159
|
-
* ```
|
|
1160
|
-
*/
|
|
1161
|
-
const parseJsonc = (source) => {
|
|
1162
|
-
const stripped = source.replaceAll(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/gu, "");
|
|
1163
|
-
return JSON.parse(stripped);
|
|
1164
|
-
};
|
|
1165
|
-
/**
|
|
1166
|
-
* Build the wrangler `kv_namespaces` array from the manifest's kv resources.
|
|
1167
|
-
*
|
|
1168
|
-
* @param resources - All resource descriptors from the manifest.
|
|
1169
|
-
* @param ids - Captured Cloudflare ids keyed by binding; the entry's `id` is filled from here.
|
|
1170
|
-
* @returns One wrangler KV namespace entry per kv resource (real `id` when known, else "").
|
|
1171
|
-
* @example
|
|
1172
|
-
* ```ts
|
|
1173
|
-
* const kv = buildKvNamespaces([{ kind: "kv", binding: "CACHE" }], { CACHE: "ns123" });
|
|
1174
|
-
* ```
|
|
1175
|
-
*/
|
|
1176
|
-
const buildKvNamespaces = (resources, ids) => resources.filter((resource) => resource.kind === "kv").map((resource) => ({
|
|
1177
|
-
binding: resource.binding,
|
|
1178
|
-
id: ids[resource.binding] ?? ""
|
|
1179
|
-
}));
|
|
1180
|
-
/**
|
|
1181
|
-
* Build the wrangler `r2_buckets` array from the manifest's r2 resources.
|
|
1182
|
-
*
|
|
1183
|
-
* @param resources - All resource descriptors from the manifest.
|
|
1184
|
-
* @returns One wrangler R2 bucket entry per r2 resource.
|
|
1185
|
-
* @example
|
|
1186
|
-
* ```ts
|
|
1187
|
-
* const r2 = buildR2Buckets([{ kind: "r2", bucket: "ASSETS" }]);
|
|
1188
|
-
* ```
|
|
1189
|
-
*/
|
|
1190
|
-
const buildR2Buckets = (resources) => resources.filter((resource) => resource.kind === "r2").map((resource) => ({
|
|
1191
|
-
binding: resource.bucket,
|
|
1192
|
-
bucket_name: resource.bucket.toLowerCase()
|
|
1193
|
-
}));
|
|
1194
|
-
/**
|
|
1195
|
-
* Build the wrangler `d1_databases` array from the manifest's d1 resources.
|
|
1196
|
-
*
|
|
1197
|
-
* @param resources - All resource descriptors from the manifest.
|
|
1198
|
-
* @param ids - Captured Cloudflare ids keyed by binding; the entry's `database_id` is filled from here.
|
|
1199
|
-
* @returns One wrangler D1 database entry per d1 resource (migrations_dir set when present).
|
|
1200
|
-
* @example
|
|
1201
|
-
* ```ts
|
|
1202
|
-
* const d1 = buildD1Databases([{ kind: "d1", binding: "DB" }], { DB: "uuid-1234" });
|
|
1203
|
-
* ```
|
|
1204
|
-
*/
|
|
1205
|
-
const buildD1Databases = (resources, ids) => resources.filter((resource) => resource.kind === "d1").map((resource) => {
|
|
1206
|
-
const entry = {
|
|
1207
|
-
binding: resource.binding,
|
|
1208
|
-
database_name: resource.binding.toLowerCase(),
|
|
1209
|
-
database_id: ids[resource.binding] ?? ""
|
|
1210
|
-
};
|
|
1211
|
-
if (resource.migrations) entry.migrations_dir = resource.migrations;
|
|
1212
|
-
return entry;
|
|
1213
|
-
});
|
|
1214
|
-
/**
|
|
1215
|
-
* Build the wrangler `queues` producers section from the manifest's queue resources.
|
|
1216
|
-
*
|
|
1217
|
-
* @param resources - All resource descriptors from the manifest.
|
|
1218
|
-
* @returns The queues section, or undefined when there are no queue resources.
|
|
1219
|
-
* @example
|
|
1220
|
-
* ```ts
|
|
1221
|
-
* const q = buildQueues([{ kind: "queue", producers: ["jobs"] }]);
|
|
1222
|
-
* ```
|
|
1223
|
-
*/
|
|
1224
|
-
const buildQueues = (resources) => {
|
|
1225
|
-
const queueResources = resources.filter((resource) => resource.kind === "queue");
|
|
1226
|
-
if (queueResources.length === 0) return void 0;
|
|
1227
|
-
return { producers: queueResources.flatMap((resource) => resource.producers.map((producer) => ({
|
|
1228
|
-
queue: producer,
|
|
1229
|
-
binding: producer.toUpperCase()
|
|
1230
|
-
}))) };
|
|
1231
|
-
};
|
|
1232
|
-
/**
|
|
1233
|
-
* Build the wrangler `durable_objects` bindings section from the manifest's do resources.
|
|
1234
|
-
*
|
|
1235
|
-
* @param resources - All resource descriptors from the manifest.
|
|
1236
|
-
* @returns The durable_objects section, or undefined when there are no do resources.
|
|
1237
|
-
* @example
|
|
1238
|
-
* ```ts
|
|
1239
|
-
* const dobj = buildDurableObjects([{ kind: "do", bindings: { Counter: "COUNTER" } }]);
|
|
1240
|
-
* ```
|
|
1241
|
-
*/
|
|
1242
|
-
const buildDurableObjects = (resources) => {
|
|
1243
|
-
const doResources = resources.filter((resource) => resource.kind === "do");
|
|
1244
|
-
if (doResources.length === 0) return void 0;
|
|
1245
|
-
return { bindings: doResources.flatMap((resource) => Object.entries(resource.bindings).map(([className, bindingName]) => ({
|
|
1246
|
-
name: bindingName,
|
|
1247
|
-
class_name: className
|
|
1248
|
-
}))) };
|
|
1249
|
-
};
|
|
1250
|
-
/**
|
|
1251
|
-
* Generate/update the wrangler config file from a manifest (non-destructive merge).
|
|
1252
|
-
* If the file exists, its top-level keys are preserved and only deploy-managed keys
|
|
1253
|
-
* (name, compatibility_date, kv_namespaces, r2_buckets, d1_databases, queues,
|
|
1254
|
-
* durable_objects) are updated.
|
|
1255
|
-
*
|
|
1256
|
-
* @param configFile - Path to the wrangler config file.
|
|
1257
|
-
* @param manifest - The assembled deploy manifest.
|
|
1258
|
-
* @param ids - Captured Cloudflare ids keyed by binding (kv namespace id, d1 database id). Defaults
|
|
1259
|
-
* to an empty map, in which case `id`/`database_id` are written as "" (e.g. the universal path).
|
|
1260
|
-
* @returns Resolves once the file is written.
|
|
1261
|
-
* @example
|
|
1262
|
-
* ```ts
|
|
1263
|
-
* await writeWranglerConfig("wrangler.jsonc", manifest, { CACHE: "ns123", DB: "uuid-1234" });
|
|
1264
|
-
* ```
|
|
1265
|
-
*/
|
|
1266
|
-
const writeWranglerConfig = async (configFile, manifest, ids = {}) => {
|
|
1267
|
-
let existing = {};
|
|
1268
|
-
if ((0, node_fs.existsSync)(configFile)) try {
|
|
1269
|
-
existing = parseJsonc((0, node_fs.readFileSync)(configFile, "utf8"));
|
|
1270
|
-
} catch {
|
|
1271
|
-
existing = {};
|
|
1272
|
-
}
|
|
1273
|
-
const kvNamespaces = buildKvNamespaces(manifest.resources, ids);
|
|
1274
|
-
const r2Buckets = buildR2Buckets(manifest.resources);
|
|
1275
|
-
const d1Databases = buildD1Databases(manifest.resources, ids);
|
|
1276
|
-
const queues = buildQueues(manifest.resources);
|
|
1277
|
-
const durableObjects = buildDurableObjects(manifest.resources);
|
|
1278
|
-
const updated = {
|
|
1279
|
-
...existing,
|
|
1280
|
-
name: manifest.name,
|
|
1281
|
-
compatibility_date: manifest.compatibilityDate
|
|
1282
|
-
};
|
|
1283
|
-
if (kvNamespaces.length > 0) updated.kv_namespaces = kvNamespaces;
|
|
1284
|
-
if (r2Buckets.length > 0) updated.r2_buckets = r2Buckets;
|
|
1285
|
-
if (d1Databases.length > 0) updated.d1_databases = d1Databases;
|
|
1286
|
-
if (queues !== void 0) updated.queues = queues;
|
|
1287
|
-
if (durableObjects !== void 0) updated.durable_objects = durableObjects;
|
|
1288
|
-
await (0, node_fs_promises.writeFile)(configFile, JSON.stringify(updated, void 0, 2));
|
|
1289
|
-
};
|
|
1290
|
-
/**
|
|
1291
|
-
* Scaffold a starting wrangler config and, when ci is set, CI workflow files.
|
|
1292
|
-
* Idempotent: an existing config file is left completely untouched.
|
|
1293
|
-
*
|
|
1294
|
-
* @param configFile - Path to the wrangler config file.
|
|
1295
|
-
* @param _ci - Whether to also scaffold CI workflow files.
|
|
1296
|
-
* @returns Resolves once scaffolding is written.
|
|
1297
|
-
* @example
|
|
1298
|
-
* ```ts
|
|
1299
|
-
* await scaffoldWranglerAndCi("wrangler.jsonc", true);
|
|
1300
|
-
* ```
|
|
1301
|
-
*/
|
|
1302
|
-
const scaffoldWranglerAndCi = async (configFile, _ci) => {
|
|
1303
|
-
if ((0, node_fs.existsSync)(configFile)) return;
|
|
1304
|
-
const starter = {
|
|
1305
|
-
name: "my-worker",
|
|
1306
|
-
main: "src/worker.ts",
|
|
1307
|
-
compatibility_date: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)
|
|
1308
|
-
};
|
|
1309
|
-
await (0, node_fs_promises.writeFile)(configFile, JSON.stringify(starter, void 0, 2));
|
|
1310
|
-
};
|
|
1311
|
-
//#endregion
|
|
1312
|
-
//#region src/plugins/deploy/api.ts
|
|
1313
|
-
/**
|
|
1314
|
-
* @file deploy plugin — API factory (run, dev, init, checkInfra, provisionInfra).
|
|
1315
|
-
*
|
|
1316
|
-
* Pure ctx-taking factory. Assembles the deploy manifest from each resource plugin's own
|
|
1317
|
-
* deployManifest() api (never sibling pluginConfigs — design F6), runs an infra preflight
|
|
1318
|
-
* (check-before-create + capture real ids), generates/updates the wrangler config, uploads the
|
|
1319
|
-
* R2 upload dir, and runs wrangler deploy. Emits only global events: deploy:phase,
|
|
1320
|
-
* deploy:complete, provision:resource, provision:plan, provision:skip.
|
|
1321
|
-
*
|
|
1322
|
-
* Node-only: uses node:child_process (via runner.ts), node:fs (via wrangler-config.ts), and the
|
|
1323
|
-
* Cloudflare REST API (via infra/). Never called in the deployed Worker runtime.
|
|
1324
|
-
*/
|
|
1325
|
-
/**
|
|
1326
|
-
* Derive a human-readable name string from a resource descriptor (used in provision events).
|
|
1327
|
-
*
|
|
1328
|
-
* @param resource - The resource descriptor.
|
|
1329
|
-
* @returns A name suitable for the provision:resource / provision:skip event payload.
|
|
1330
|
-
* @example
|
|
1331
|
-
* ```ts
|
|
1332
|
-
* resourceName({ kind: "kv", binding: "CACHE" }); // "CACHE"
|
|
1333
|
-
* ```
|
|
1334
|
-
*/
|
|
1335
|
-
const resourceName = (resource) => {
|
|
1336
|
-
switch (resource.kind) {
|
|
1337
|
-
case "r2": return resource.bucket;
|
|
1338
|
-
case "do": return Object.values(resource.bindings).join(",");
|
|
1339
|
-
case "queue": return resource.producers.join(",");
|
|
1340
|
-
default: return resource.binding;
|
|
1341
|
-
}
|
|
1342
|
-
};
|
|
1343
|
-
/**
|
|
1344
|
-
* Assemble the deploy manifest from each present resource plugin's OWN deployManifest() api,
|
|
1345
|
-
* gated by ctx.has(name) so absent plugins are skipped — never sibling pluginConfigs (F6).
|
|
1346
|
-
*
|
|
1347
|
-
* @param ctx - The deploy plugin context.
|
|
1348
|
-
* @returns The assembled manifest (name, compatibilityDate, resources).
|
|
1349
|
-
* @example
|
|
1350
|
-
* ```ts
|
|
1351
|
-
* const manifest = assembleManifest(ctx);
|
|
1352
|
-
* ```
|
|
1353
|
-
*/
|
|
1354
|
-
const assembleManifest = (ctx) => ({
|
|
1355
|
-
name: ctx.global.name,
|
|
1356
|
-
compatibilityDate: ctx.global.compatibilityDate,
|
|
1357
|
-
resources: [
|
|
1358
|
-
ctx.has("storage") ? ctx.require(require_storage.storagePlugin).deployManifest() : void 0,
|
|
1359
|
-
ctx.has("kv") ? ctx.require(require_storage.kvPlugin).deployManifest() : void 0,
|
|
1360
|
-
ctx.has("d1") ? ctx.require(require_storage.d1Plugin).deployManifest() : void 0,
|
|
1361
|
-
ctx.has("queues") ? ctx.require(require_storage.queuesPlugin).deployManifest() : void 0,
|
|
1362
|
-
ctx.has("durableObjects") ? ctx.require(require_storage.durableObjectsPlugin).deployManifest() : void 0
|
|
1363
|
-
].filter((resource) => resource !== void 0)
|
|
1364
|
-
});
|
|
1365
|
-
/**
|
|
1366
|
-
* Act on an infra plan: skip the resources that already exist (reusing their ids), create only
|
|
1367
|
-
* the missing ones (capturing each new id), and announce each via provision:skip / :resource.
|
|
1368
|
-
*
|
|
1369
|
-
* @param ctx - The deploy plugin context.
|
|
1370
|
-
* @param plan - The infra plan from planInfra (existing vs missing).
|
|
1371
|
-
* @returns The provisioning result: created, skipped, and the merged binding → id map.
|
|
1372
|
-
* @example
|
|
1373
|
-
* ```ts
|
|
1374
|
-
* const { ids } = await applyPlan(ctx, plan);
|
|
1375
|
-
* ```
|
|
1376
|
-
*/
|
|
1377
|
-
const applyPlan = async (ctx, plan) => {
|
|
1378
|
-
const ids = {};
|
|
1379
|
-
for (const ref of plan.exists) {
|
|
1380
|
-
if (ref.id !== void 0 && (ref.resource.kind === "kv" || ref.resource.kind === "d1")) ids[ref.resource.binding] = ref.id;
|
|
1381
|
-
ctx.emit("provision:skip", {
|
|
1382
|
-
kind: ref.resource.kind,
|
|
1383
|
-
name: resourceName(ref.resource)
|
|
1384
|
-
});
|
|
1385
|
-
}
|
|
1386
|
-
const created = [];
|
|
1387
|
-
for (const resource of plan.missing) {
|
|
1388
|
-
const { id } = await provisionResource(resource, ctx.config.ci);
|
|
1389
|
-
if (id !== void 0 && (resource.kind === "kv" || resource.kind === "d1")) ids[resource.binding] = id;
|
|
1390
|
-
created.push(id === void 0 ? { resource } : {
|
|
1391
|
-
resource,
|
|
1392
|
-
id
|
|
1393
|
-
});
|
|
1394
|
-
ctx.emit("provision:resource", {
|
|
1395
|
-
kind: resource.kind,
|
|
1396
|
-
name: resourceName(resource)
|
|
1397
|
-
});
|
|
1398
|
-
}
|
|
1399
|
-
return {
|
|
1400
|
-
created,
|
|
1401
|
-
skipped: plan.exists,
|
|
1402
|
-
ids
|
|
1403
|
-
};
|
|
1404
|
-
};
|
|
1405
|
-
/**
|
|
1406
|
-
* Create the deploy api. Assembles the manifest from each resource plugin's own deployManifest(),
|
|
1407
|
-
* runs an infra preflight (check-before-create + id capture), generates config, uploads, and runs
|
|
1408
|
-
* `wrangler deploy`, emitting global deploy events along the way.
|
|
1409
|
-
*
|
|
1410
|
-
* @param ctx - Plugin context (own config + require + has + emit + global + env).
|
|
1411
|
-
* @returns The app.deploy api: run / dev / init / checkInfra / provisionInfra.
|
|
1412
|
-
* @example
|
|
1413
|
-
* ```ts
|
|
1414
|
-
* const api = createDeployApi(ctx);
|
|
1415
|
-
* await api.run();
|
|
1416
|
-
* ```
|
|
1417
|
-
*/
|
|
1418
|
-
const createDeployApi = (ctx) => ({
|
|
1419
|
-
/**
|
|
1420
|
-
* Run the full deploy pipeline: detect → preflight (check-before-create) → provision (only the
|
|
1421
|
-
* missing) → wrangler-config (with real ids) → upload → deploy. When opts.manifest is supplied
|
|
1422
|
-
* it is used verbatim (universal path).
|
|
1423
|
-
*
|
|
1424
|
-
* @param opts - Optional run options.
|
|
1425
|
-
* @param opts.guided - Enable interactive confirmation steps (only on a TTY, non-CI).
|
|
1426
|
-
* @param opts.yes - Auto-confirm all prompts (non-interactive / CI).
|
|
1427
|
-
* @param opts.webBuild - Build the web site first (e.g. `() => webApp.cli.build()`), before deploy.
|
|
1428
|
-
* @param opts.manifest - Caller-supplied manifest (bypasses deployManifest() assembly).
|
|
1429
|
-
* @returns Resolves once the deploy completes.
|
|
1430
|
-
* @example
|
|
1431
|
-
* ```ts
|
|
1432
|
-
* await api.run({ guided: true, webBuild: () => web.cli.build() });
|
|
1433
|
-
* await api.run({ manifest: { name: "w", compatibilityDate: "2026-06-17", resources: [] } });
|
|
1434
|
-
* ```
|
|
1435
|
-
*/
|
|
1436
|
-
async run(opts) {
|
|
1437
|
-
const confirm = (opts?.guided ?? false) && !ctx.config.ci && !(opts?.yes ?? false) && stdoutIsTty() ? (0, _moku_labs_common_cli.createBrandPrompts)().confirm : async (_question) => true;
|
|
1438
|
-
ctx.emit("deploy:phase", { phase: "auth" });
|
|
1439
|
-
await verifyAuth(ctx);
|
|
1440
|
-
const webBuild = opts?.webBuild ?? ctx.config.webBuild;
|
|
1441
|
-
if (webBuild !== void 0) {
|
|
1442
|
-
ctx.emit("deploy:phase", {
|
|
1443
|
-
phase: "build",
|
|
1444
|
-
detail: "web"
|
|
1445
|
-
});
|
|
1446
|
-
await webBuild();
|
|
1447
|
-
}
|
|
1448
|
-
ctx.emit("deploy:phase", { phase: "detect" });
|
|
1449
|
-
const manifest = opts?.manifest ?? assembleManifest(ctx);
|
|
1450
|
-
ctx.emit("deploy:phase", { phase: "provision" });
|
|
1451
|
-
const plan = await planInfra(ctx, manifest);
|
|
1452
|
-
if (plan.missing.length > 0 && !await confirm(`Create ${plan.missing.length} missing resource(s) in "${plan.account}"?`)) {
|
|
1453
|
-
ctx.emit("deploy:phase", { phase: "aborted" });
|
|
1454
|
-
return;
|
|
1455
|
-
}
|
|
1456
|
-
const { ids } = await applyPlan(ctx, plan);
|
|
1457
|
-
ctx.emit("deploy:phase", { phase: "wrangler-config" });
|
|
1458
|
-
await writeWranglerConfig(ctx.config.configFile, manifest, ids);
|
|
1459
|
-
const r2Resource = manifest.resources.find((resource) => resource.kind === "r2");
|
|
1460
|
-
if (r2Resource?.upload) {
|
|
1461
|
-
const count = await uploadDirToR2(r2Resource.bucket, r2Resource.upload);
|
|
1462
|
-
ctx.emit("deploy:phase", {
|
|
1463
|
-
phase: "upload",
|
|
1464
|
-
detail: `${String(count)} files`
|
|
1465
|
-
});
|
|
1466
|
-
}
|
|
1467
|
-
if (!await confirm(`Deploy "${manifest.name}" to ${ctx.global.stage}?`)) {
|
|
1468
|
-
ctx.emit("deploy:phase", { phase: "aborted" });
|
|
1469
|
-
return;
|
|
1470
|
-
}
|
|
1471
|
-
ctx.emit("deploy:phase", { phase: "deploy" });
|
|
1472
|
-
const url = await runWrangler([
|
|
1473
|
-
"deploy",
|
|
1474
|
-
"--config",
|
|
1475
|
-
ctx.config.configFile
|
|
1476
|
-
]);
|
|
1477
|
-
ctx.emit("deploy:complete", { url });
|
|
1478
|
-
},
|
|
1479
|
-
/**
|
|
1480
|
-
* Start a long-lived local dev session: cold-build the Moku site, spawn `wrangler dev
|
|
1481
|
-
* --live-reload`, and watch the site sources — rebuilding on change (wrangler live-reloads the
|
|
1482
|
-
* browser). Resolves on SIGINT.
|
|
1483
|
-
*
|
|
1484
|
-
* @param opts - Optional options.
|
|
1485
|
-
* @param opts.port - Local dev port (default 8787).
|
|
1486
|
-
* @param opts.webBuild - Rebuild the web site on change (e.g. `() => webApp.cli.build()`).
|
|
1487
|
-
* @returns Resolves when the dev session ends.
|
|
1488
|
-
* @example
|
|
1489
|
-
* ```ts
|
|
1490
|
-
* await api.dev({ port: 8787, webBuild: () => web.cli.build() });
|
|
1491
|
-
* ```
|
|
1492
|
-
*/
|
|
1493
|
-
dev: (opts) => runDev(ctx, opts, realDevDeps()),
|
|
1494
|
-
/**
|
|
1495
|
-
* Scaffold a starting wrangler config (and CI files when ci is set).
|
|
1496
|
-
* Idempotent: an existing config file is left untouched.
|
|
1497
|
-
*
|
|
1498
|
-
* @param opts - Optional options.
|
|
1499
|
-
* @param opts.ci - Also scaffold CI workflow files.
|
|
1500
|
-
* @returns Resolves once scaffolding is written.
|
|
1501
|
-
* @example
|
|
1502
|
-
* ```ts
|
|
1503
|
-
* await api.init({ ci: true });
|
|
1504
|
-
* ```
|
|
1505
|
-
*/
|
|
1506
|
-
init: async (opts) => {
|
|
1507
|
-
await scaffoldWranglerAndCi(ctx.config.configFile, opts?.ci ?? ctx.config.ci);
|
|
1508
|
-
},
|
|
1509
|
-
/**
|
|
1510
|
-
* Read-only infra preflight: assemble the manifest, resolve the account, list what exists in
|
|
1511
|
-
* Cloudflare, diff, emit provision:plan, and return the plan. Writes nothing.
|
|
1512
|
-
*
|
|
1513
|
-
* @returns The infra plan (existing vs missing resources, with captured ids).
|
|
1514
|
-
* @example
|
|
1515
|
-
* ```ts
|
|
1516
|
-
* const plan = await api.checkInfra();
|
|
1517
|
-
* ```
|
|
1518
|
-
*/
|
|
1519
|
-
checkInfra: () => planInfra(ctx, assembleManifest(ctx)),
|
|
1520
|
-
/**
|
|
1521
|
-
* Create only the resources missing from the plan (skipping existing), capturing each id.
|
|
1522
|
-
*
|
|
1523
|
-
* @param plan - A plan produced by checkInfra().
|
|
1524
|
-
* @returns The provisioning result: created, skipped, and the merged id map.
|
|
1525
|
-
* @example
|
|
1526
|
-
* ```ts
|
|
1527
|
-
* const { created } = await api.provisionInfra(await api.checkInfra());
|
|
1528
|
-
* ```
|
|
1529
|
-
*/
|
|
1530
|
-
provisionInfra: (plan) => applyPlan(ctx, plan),
|
|
1531
|
-
/**
|
|
1532
|
-
* Verify the `.env` Cloudflare API token (must be active) and resolve its account; emits
|
|
1533
|
-
* auth:verified. Throws a branded error pointing at `auth setup` when absent/invalid/inactive.
|
|
1534
|
-
*
|
|
1535
|
-
* @returns The verified auth status (account + id).
|
|
1536
|
-
* @example
|
|
1537
|
-
* ```ts
|
|
1538
|
-
* const { account } = await api.verifyAuth();
|
|
1539
|
-
* ```
|
|
1540
|
-
*/
|
|
1541
|
-
verifyAuth: () => verifyAuth(ctx),
|
|
1542
|
-
/**
|
|
1543
|
-
* Derive the minimum Cloudflare API token this app needs from its manifest (pure, no network).
|
|
1544
|
-
*
|
|
1545
|
-
* @returns The token requirement (full set + groups to add to the stock template).
|
|
1546
|
-
* @example
|
|
1547
|
-
* ```ts
|
|
1548
|
-
* const { toAdd } = api.requiredToken();
|
|
1549
|
-
* ```
|
|
1550
|
-
*/
|
|
1551
|
-
requiredToken: () => requiredToken(assembleManifest(ctx)),
|
|
1552
|
-
/**
|
|
1553
|
-
* Render the `auth setup` guidance from the derived token requirement (pure, no network).
|
|
1554
|
-
*
|
|
1555
|
-
* @returns The rendered instruction text.
|
|
1556
|
-
* @example
|
|
1557
|
-
* ```ts
|
|
1558
|
-
* const text = api.tokenInstructions();
|
|
1559
|
-
* ```
|
|
1560
|
-
*/
|
|
1561
|
-
tokenInstructions: () => tokenInstructions(assembleManifest(ctx)),
|
|
1562
|
-
/**
|
|
1563
|
-
* Run an arbitrary wrangler command, streaming its output (the branded CLI escape hatch).
|
|
1564
|
-
*
|
|
1565
|
-
* @param args - The wrangler arguments.
|
|
1566
|
-
* @returns Resolves once wrangler exits.
|
|
1567
|
-
* @example
|
|
1568
|
-
* ```ts
|
|
1569
|
-
* await api.wrangler(["kv", "namespace", "list"]);
|
|
1570
|
-
* ```
|
|
1571
|
-
*/
|
|
1572
|
-
wrangler: (args) => runWranglerInherit(args)
|
|
1573
|
-
});
|
|
1574
|
-
/**
|
|
1575
|
-
* Complex tier (node-only) — build-time deploy orchestrator over the five resource plugins.
|
|
1576
|
-
*
|
|
1577
|
-
* Assembles each resource plugin's deployManifest() via ctx.require, provisions resources,
|
|
1578
|
-
* generates/updates wrangler config, uploads the R2 upload dir, and runs wrangler deploy.
|
|
1579
|
-
* Also supports a universal path: run({ manifest }) uses a caller-supplied manifest verbatim.
|
|
1580
|
-
*
|
|
1581
|
-
* Emits only the global events `deploy:phase`, `deploy:complete`, and `provision:resource`
|
|
1582
|
-
* (declared in WorkerEvents — no per-plugin events block).
|
|
1583
|
-
*
|
|
1584
|
-
* @see README.md
|
|
1585
|
-
*/
|
|
1586
|
-
const deployPlugin = require_storage.createPlugin("deploy", {
|
|
1587
|
-
config: {
|
|
1588
|
-
configFile: "wrangler.jsonc",
|
|
1589
|
-
ci: false,
|
|
1590
|
-
watch: ["src/**/*.{ts,tsx,css}", "public/**/*"],
|
|
1591
|
-
buildCommand: "",
|
|
1592
|
-
migrateLocal: true,
|
|
1593
|
-
debounceMs: 120
|
|
1594
|
-
},
|
|
1595
|
-
depends: [
|
|
1596
|
-
require_storage.storagePlugin,
|
|
1597
|
-
require_storage.kvPlugin,
|
|
1598
|
-
require_storage.d1Plugin,
|
|
1599
|
-
require_storage.queuesPlugin,
|
|
1600
|
-
require_storage.durableObjectsPlugin
|
|
1601
|
-
],
|
|
1602
|
-
api: (ctx) => createDeployApi(ctx)
|
|
1603
|
-
});
|
|
1604
|
-
//#endregion
|
|
1605
|
-
//#region src/plugins/cli/api.ts
|
|
1606
|
-
/**
|
|
1607
|
-
* @file cli plugin — API factory (dev, deploy, auth, doctor).
|
|
1608
|
-
*/
|
|
1609
|
-
/**
|
|
1610
|
-
* Builds app.cli.* — thin passthroughs to the deploy plugin via ctx.require(deployPlugin).
|
|
1611
|
-
* Both verbs forward their opts verbatim; `dev` defaults port to ctx.config.port when no
|
|
1612
|
-
* opts are supplied.
|
|
1613
|
-
*
|
|
1614
|
-
* @param ctx - CLI plugin context (own config + typed require to deployPlugin).
|
|
1615
|
-
* @returns The cli API object with `dev` and `deploy` methods.
|
|
1616
|
-
* @example
|
|
1617
|
-
* ```ts
|
|
1618
|
-
* const api = createCliApi(ctx);
|
|
1619
|
-
* await api.dev(); // → deploy.dev({ port: 8787 })
|
|
1620
|
-
* await api.deploy({ yes: true }); // → deploy.run({ yes: true })
|
|
1621
|
-
* ```
|
|
1622
|
-
*/
|
|
1623
|
-
const createCliApi = (ctx) => ({
|
|
1624
|
-
/**
|
|
1625
|
-
* Run the Worker locally; defaults port to ctx.config.port (8787) when no opts supplied. A
|
|
1626
|
-
* `webBuild` hook (e.g. `() => webApp.cli.build()`) wires the web build into the dev loop so the
|
|
1627
|
-
* site recompiles on change — this is how an app-side script composes web + worker.
|
|
1628
|
-
*
|
|
1629
|
-
* @param opts - Optional local dev options.
|
|
1630
|
-
* @param opts.port - Local dev port to bind. Defaults to ctx.config.port (8787).
|
|
1631
|
-
* @param opts.webBuild - Rebuild the web site on change (e.g. `() => webApp.cli.build()`).
|
|
1632
|
-
* @returns Resolves when the dev session ends.
|
|
1633
|
-
* @example
|
|
1634
|
-
* ```ts
|
|
1635
|
-
* await api.dev(); // port 8787, worker only
|
|
1636
|
-
* await api.dev({ webBuild: () => web.cli.build() }); // wire the web build in
|
|
1637
|
-
* ```
|
|
1638
|
-
*/
|
|
1639
|
-
dev(opts) {
|
|
1640
|
-
const port = opts?.port ?? ctx.config.port;
|
|
1641
|
-
return ctx.require(deployPlugin).dev(opts?.webBuild ? {
|
|
1642
|
-
port,
|
|
1643
|
-
webBuild: opts.webBuild
|
|
1644
|
-
} : { port });
|
|
1645
|
-
},
|
|
1646
|
-
/**
|
|
1647
|
-
* One-command guided Cloudflare deploy; forwards flags verbatim to deploy.run.
|
|
1648
|
-
* Passes `undefined` when called with no opts (not a default empty object). A `webBuild` hook
|
|
1649
|
-
* builds the web site first (before `wrangler deploy`) — how an app-side script ships web + worker.
|
|
1650
|
-
*
|
|
1651
|
-
* @param opts - Optional deploy options.
|
|
1652
|
-
* @param opts.guided - Walk through each step interactively.
|
|
1653
|
-
* @param opts.yes - Skip confirmation prompts (non-interactive / CI).
|
|
1654
|
-
* @param opts.webBuild - Build the web site first (e.g. `() => webApp.cli.build()`), before deploy.
|
|
1655
|
-
* @returns Resolves once the deploy completes.
|
|
1656
|
-
* @example
|
|
1657
|
-
* ```ts
|
|
1658
|
-
* await api.deploy({ guided: true, webBuild: () => web.cli.build() });
|
|
1659
|
-
* await api.deploy({ yes: true }); // CI
|
|
1660
|
-
* await api.deploy(); // opts === undefined
|
|
1661
|
-
* ```
|
|
1662
|
-
*/
|
|
1663
|
-
deploy(opts) {
|
|
1664
|
-
return ctx.require(deployPlugin).run(opts);
|
|
1665
|
-
},
|
|
1666
|
-
/**
|
|
1667
|
-
* Verify the `.env` token (no sub) or print the config-derived token guidance (`"setup"`),
|
|
1668
|
-
* rendered in Moku style. `setup` works without a token; verify reports the resolved account.
|
|
1669
|
-
*
|
|
1670
|
-
* @param sub - Pass "setup" to print guidance; omit to verify the current token.
|
|
1671
|
-
* @returns Resolves once the check or guidance render completes.
|
|
1672
|
-
* @example
|
|
1673
|
-
* ```ts
|
|
1674
|
-
* await api.auth("setup"); // print what token to create
|
|
1675
|
-
* await api.auth(); // verify the current token
|
|
1676
|
-
* ```
|
|
1677
|
-
*/
|
|
1678
|
-
async auth(sub) {
|
|
1679
|
-
const deploy = ctx.require(deployPlugin);
|
|
1680
|
-
const ui = (0, _moku_labs_common_cli.createBrandConsole)();
|
|
1681
|
-
if (sub === "setup") {
|
|
1682
|
-
for (const line of deploy.tokenInstructions().split("\n")) ui.line(line);
|
|
1683
|
-
return;
|
|
1684
|
-
}
|
|
1685
|
-
try {
|
|
1686
|
-
const status = await deploy.verifyAuth();
|
|
1687
|
-
ui.check(true, "token valid", `account "${status.account}" (${status.accountId})`);
|
|
1688
|
-
} catch (error) {
|
|
1689
|
-
ui.error(error instanceof Error ? error.message : String(error));
|
|
1690
|
-
}
|
|
1691
|
-
},
|
|
1692
|
-
/**
|
|
1693
|
-
* One-shot preflight report: token + account (verifyAuth) then infra drift (checkInfra),
|
|
1694
|
-
* each as a branded check line. Stops after the token check when auth fails.
|
|
1695
|
-
*
|
|
1696
|
-
* @returns Resolves once the report is printed.
|
|
1697
|
-
* @example
|
|
1698
|
-
* ```ts
|
|
1699
|
-
* await api.doctor();
|
|
1700
|
-
* ```
|
|
1701
|
-
*/
|
|
1702
|
-
async doctor() {
|
|
1703
|
-
const deploy = ctx.require(deployPlugin);
|
|
1704
|
-
const ui = (0, _moku_labs_common_cli.createBrandConsole)();
|
|
1705
|
-
ui.heading("doctor");
|
|
1706
|
-
let tokenOk = false;
|
|
1707
|
-
try {
|
|
1708
|
-
const status = await deploy.verifyAuth();
|
|
1709
|
-
tokenOk = true;
|
|
1710
|
-
ui.check(true, "token", `valid · account "${status.account}" (${status.accountId})`);
|
|
1711
|
-
} catch (error) {
|
|
1712
|
-
ui.check(false, "token", error instanceof Error ? error.message : String(error));
|
|
1713
|
-
}
|
|
1714
|
-
if (!tokenOk) {
|
|
1715
|
-
ui.line("Run `auth setup` for the exact token to create.");
|
|
1716
|
-
return;
|
|
1717
|
-
}
|
|
1718
|
-
try {
|
|
1719
|
-
const plan = await deploy.checkInfra();
|
|
1720
|
-
ui.check(true, "infra", `${plan.exists.length} exist, ${plan.missing.length} to create in "${plan.account}"`);
|
|
1721
|
-
} catch (error) {
|
|
1722
|
-
ui.check(false, "infra", error instanceof Error ? error.message : String(error));
|
|
1723
|
-
}
|
|
1724
|
-
},
|
|
1725
|
-
/**
|
|
1726
|
-
* Print the resolved Cloudflare account for the current `.env` token.
|
|
1727
|
-
*
|
|
1728
|
-
* @returns Resolves once the account summary is printed.
|
|
1729
|
-
* @example
|
|
1730
|
-
* ```ts
|
|
1731
|
-
* await api.whoami();
|
|
1732
|
-
* ```
|
|
1733
|
-
*/
|
|
1734
|
-
async whoami() {
|
|
1735
|
-
const ui = (0, _moku_labs_common_cli.createBrandConsole)();
|
|
1736
|
-
try {
|
|
1737
|
-
const status = await ctx.require(deployPlugin).verifyAuth();
|
|
1738
|
-
ui.check(true, "account", `${status.account} (${status.accountId})`);
|
|
1739
|
-
} catch (error) {
|
|
1740
|
-
ui.error(error instanceof Error ? error.message : String(error));
|
|
1741
|
-
}
|
|
1742
|
-
},
|
|
1743
|
-
/**
|
|
1744
|
-
* Run an arbitrary wrangler command through the branded CLI (escape hatch). Streams its output.
|
|
1745
|
-
*
|
|
1746
|
-
* @param args - The wrangler arguments.
|
|
1747
|
-
* @returns Resolves once wrangler exits.
|
|
1748
|
-
* @example
|
|
1749
|
-
* ```ts
|
|
1750
|
-
* await api.wrangler(["kv", "namespace", "list"]);
|
|
1751
|
-
* ```
|
|
1752
|
-
*/
|
|
1753
|
-
async wrangler(args) {
|
|
1754
|
-
(0, _moku_labs_common_cli.createBrandConsole)().heading(`wrangler ${args.join(" ")}`);
|
|
1755
|
-
await ctx.require(deployPlugin).wrangler(args);
|
|
1756
|
-
}
|
|
1757
|
-
});
|
|
1758
|
-
//#endregion
|
|
1759
|
-
//#region src/plugins/cli/handlers.ts
|
|
1760
|
-
/**
|
|
1761
|
-
* Builds the hook handlers that turn global deploy events into a live progress TUI.
|
|
1762
|
-
* Each logs a clean, prefix-free message via `ctx.log`; the branded log sink (installed
|
|
1763
|
-
* by the cli plugin's onInit from `@moku-labs/common/cli`) adds the `›` marker, brand
|
|
1764
|
-
* color, and stderr routing. Pure observers — print and return; never mutate state,
|
|
1765
|
-
* never block the deploy pipeline (fire-and-forget, spec/07 §3,§4).
|
|
1766
|
-
*
|
|
1767
|
-
* @param ctx - CLI plugin context with injected log core API.
|
|
1768
|
-
* @returns Hook map for the three global deploy events.
|
|
1769
|
-
* @example
|
|
1770
|
-
* ```ts
|
|
1771
|
-
* const hooks = createCliHooks(ctx);
|
|
1772
|
-
* hooks["deploy:phase"]({ phase: "detect" }); // logs "detect" → renders " › detect"
|
|
1773
|
-
* hooks["provision:resource"]({ kind: "kv", name: "KV" }); // logs "kv KV" → " › kv KV"
|
|
1774
|
-
* hooks["deploy:complete"]({ url: "https://x.workers.dev" }); // "deployed → https://x.workers.dev"
|
|
1775
|
-
* ```
|
|
1776
|
-
*/
|
|
1777
|
-
const createCliHooks = (ctx) => ({
|
|
1778
|
-
/**
|
|
1779
|
-
* Log one clean line per pipeline phase: "phase" or "phase · detail".
|
|
1780
|
-
*
|
|
1781
|
-
* @param p - The deploy:phase event payload.
|
|
1782
|
-
* @example
|
|
1783
|
-
* ```ts
|
|
1784
|
-
* handler({ phase: "detect" }); // "detect"
|
|
1785
|
-
* handler({ phase: "upload", detail: "3 files" }); // "upload · 3 files"
|
|
1786
|
-
* ```
|
|
1787
|
-
*/
|
|
1788
|
-
"deploy:phase"(p) {
|
|
1789
|
-
ctx.log.info(p.detail ? `${p.phase} · ${p.detail}` : p.phase);
|
|
1790
|
-
},
|
|
1791
|
-
/**
|
|
1792
|
-
* Log the infra preflight summary: "infra · N exist, M to create · account".
|
|
1793
|
-
*
|
|
1794
|
-
* @param p - The provision:plan event payload.
|
|
1795
|
-
* @example
|
|
1796
|
-
* ```ts
|
|
1797
|
-
* handler({ exists: 2, missing: 1, account: "Play Co" }); // "infra · 2 exist, 1 to create · Play Co"
|
|
1798
|
-
* ```
|
|
1799
|
-
*/
|
|
1800
|
-
"provision:plan"(p) {
|
|
1801
|
-
ctx.log.info(`infra · ${p.exists} exist, ${p.missing} to create · ${p.account}`);
|
|
1802
|
-
},
|
|
1803
|
-
/**
|
|
1804
|
-
* Log one clean line per provisioned resource: "kind name".
|
|
1805
|
-
*
|
|
1806
|
-
* @param p - The provision:resource event payload.
|
|
1807
|
-
* @example
|
|
1808
|
-
* ```ts
|
|
1809
|
-
* handler({ kind: "kv", name: "KV" }); // "kv KV"
|
|
1810
|
-
* ```
|
|
1811
|
-
*/
|
|
1812
|
-
"provision:resource"(p) {
|
|
1813
|
-
ctx.log.info(`${p.kind} ${p.name}`);
|
|
1814
|
-
},
|
|
1815
|
-
/**
|
|
1816
|
-
* Log one clean line per already-existing resource (skipped): "kind name (exists)".
|
|
1817
|
-
*
|
|
1818
|
-
* @param p - The provision:skip event payload.
|
|
1819
|
-
* @example
|
|
1820
|
-
* ```ts
|
|
1821
|
-
* handler({ kind: "kv", name: "KV" }); // "kv KV (exists)"
|
|
1822
|
-
* ```
|
|
1823
|
-
*/
|
|
1824
|
-
"provision:skip"(p) {
|
|
1825
|
-
ctx.log.info(`${p.kind} ${p.name} (exists)`);
|
|
1826
|
-
},
|
|
1827
|
-
/**
|
|
1828
|
-
* Log one dev-session phase: "phase" or "phase · detail".
|
|
1829
|
-
*
|
|
1830
|
-
* @param p - The dev:phase event payload.
|
|
1831
|
-
* @example
|
|
1832
|
-
* ```ts
|
|
1833
|
-
* handler({ phase: "serve", detail: "http://localhost:8787" }); // "serve · http://localhost:8787"
|
|
1834
|
-
* ```
|
|
1835
|
-
*/
|
|
1836
|
-
"dev:phase"(p) {
|
|
1837
|
-
ctx.log.info(p.detail ? `${p.phase} · ${p.detail}` : p.phase);
|
|
1838
|
-
},
|
|
1839
|
-
/**
|
|
1840
|
-
* Log the site rebuild result: "site <n> files · <ms>ms" (omits the count when unknown).
|
|
1841
|
-
*
|
|
1842
|
-
* @param p - The dev:rebuilt event payload.
|
|
1843
|
-
* @example
|
|
1844
|
-
* ```ts
|
|
1845
|
-
* handler({ files: 12, ms: 240 }); // "site 12 files · 240ms"
|
|
1846
|
-
* handler({ files: 0, ms: 240 }); // "site · 240ms"
|
|
1847
|
-
* ```
|
|
1848
|
-
*/
|
|
1849
|
-
"dev:rebuilt"(p) {
|
|
1850
|
-
ctx.log.info(p.files > 0 ? `site ${String(p.files)} files · ${String(p.ms)}ms` : `site · ${String(p.ms)}ms`);
|
|
1851
|
-
},
|
|
1852
|
-
/**
|
|
1853
|
-
* Log a non-fatal dev build failure via warn (the session keeps serving the last good build).
|
|
1854
|
-
*
|
|
1855
|
-
* @param p - The dev:error event payload.
|
|
1856
|
-
* @example
|
|
1857
|
-
* ```ts
|
|
1858
|
-
* handler({ message: "build failed" }); // warn "build failed"
|
|
1859
|
-
* ```
|
|
1860
|
-
*/
|
|
1861
|
-
"dev:error"(p) {
|
|
1862
|
-
ctx.log.warn(p.message);
|
|
1863
|
-
},
|
|
1864
|
-
/**
|
|
1865
|
-
* Log the terminal success line with the deployed URL.
|
|
1866
|
-
*
|
|
1867
|
-
* @param p - The deploy:complete event payload.
|
|
1868
|
-
* @example
|
|
1869
|
-
* ```ts
|
|
1870
|
-
* handler({ url: "https://my-worker.workers.dev" }); // "deployed → https://my-worker.workers.dev"
|
|
1871
|
-
* ```
|
|
1872
|
-
*/
|
|
1873
|
-
"deploy:complete"(p) {
|
|
1874
|
-
ctx.log.info(`deployed → ${p.url}`);
|
|
1875
|
-
}
|
|
1876
|
-
});
|
|
1877
|
-
/**
|
|
1878
|
-
* Standard tier (node-only) — developer-facing CLI surface.
|
|
1879
|
-
*
|
|
1880
|
-
* Mounts `app.cli.dev()` and `app.cli.deploy()` as thin passthroughs to deployPlugin.
|
|
1881
|
-
* Hooks subscribe to the global deploy:phase / provision:resource / deploy:complete events
|
|
1882
|
-
* and print a live progress TUI via the injected ctx.log core API.
|
|
1883
|
-
*
|
|
1884
|
-
* Inline lambdas on `api`/`hooks` preserve event-name inference so the hook map keys
|
|
1885
|
-
* are constrained to `WorkerEvents` keys (spec/15 §5).
|
|
1886
|
-
*
|
|
1887
|
-
* @see README.md
|
|
1888
|
-
*/
|
|
1889
|
-
const cliPlugin = require_storage.createPlugin("cli", {
|
|
1890
|
-
depends: [deployPlugin],
|
|
1891
|
-
config: { port: 8787 },
|
|
1892
|
-
onInit: (ctx) => {
|
|
1893
|
-
ctx.log.clearSinks();
|
|
1894
|
-
ctx.log.addSink((0, _moku_labs_common_cli.brandedSink)("info"));
|
|
1895
|
-
},
|
|
1896
|
-
api: (ctx) => createCliApi(ctx),
|
|
1897
|
-
hooks: (ctx) => createCliHooks(ctx)
|
|
1898
|
-
});
|
|
1899
|
-
//#endregion
|
|
1900
|
-
exports.cliPlugin = cliPlugin;
|
|
1901
|
-
exports.deployPlugin = deployPlugin;
|
|
2
|
+
const require_cli = require("./cli-Dc0q0hIy.cjs");
|
|
3
|
+
exports.cliPlugin = require_cli.cliPlugin;
|
|
4
|
+
exports.deployPlugin = require_cli.deployPlugin;
|