@moku-labs/worker 0.4.0 → 0.5.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/dist/cli.cjs +926 -93
- package/dist/cli.d.cts +169 -69
- package/dist/cli.d.mts +169 -69
- package/dist/cli.mjs +927 -94
- package/dist/{config-50jmPyMv.d.cts → config-BYPJvEbl.d.cts} +16 -0
- package/dist/{config-50jmPyMv.d.mts → config-BYPJvEbl.d.mts} +16 -0
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/package.json +9 -1
package/dist/cli.cjs
CHANGED
|
@@ -24,10 +24,228 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
const require_storage = require("./storage-CgXl-dUA.cjs");
|
|
25
25
|
let _moku_labs_common_cli = require("@moku-labs/common/cli");
|
|
26
26
|
let node_child_process = require("node:child_process");
|
|
27
|
-
let
|
|
27
|
+
let node_fs = require("node:fs");
|
|
28
28
|
let node_path = require("node:path");
|
|
29
29
|
node_path = __toESM(node_path, 1);
|
|
30
|
-
let
|
|
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
|
|
31
249
|
//#region src/plugins/deploy/infra/cloudflare.ts
|
|
32
250
|
/**
|
|
33
251
|
* @file deploy plugin — Cloudflare REST discovery client (infra preflight).
|
|
@@ -83,26 +301,44 @@ const resolveAccount = async (token) => {
|
|
|
83
301
|
};
|
|
84
302
|
};
|
|
85
303
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
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.
|
|
88
323
|
*
|
|
89
324
|
* @param token - The Cloudflare API token.
|
|
90
325
|
* @param accountId - The Cloudflare account id to scope the listings to.
|
|
91
|
-
* @
|
|
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).
|
|
92
328
|
* @throws {Error} When any listing request fails.
|
|
93
329
|
* @example
|
|
94
330
|
* ```ts
|
|
95
|
-
* const existing = await listExisting(token, accountId);
|
|
331
|
+
* const existing = await listExisting(token, accountId, new Set(["kv", "d1"]));
|
|
96
332
|
* if (existing.kv.has("SESSIONS")) { ... }
|
|
97
333
|
* ```
|
|
98
334
|
*/
|
|
99
|
-
const listExisting = async (token, accountId) => {
|
|
335
|
+
const listExisting = async (token, accountId, kinds) => {
|
|
100
336
|
const base = `/accounts/${accountId}`;
|
|
101
337
|
const [kv, d1, r2, queues] = await Promise.all([
|
|
102
|
-
cfGet(token, `${base}/storage/kv/namespaces`),
|
|
103
|
-
cfGet(token, `${base}/d1/database`),
|
|
104
|
-
cfGet(token, `${base}/r2/buckets`),
|
|
105
|
-
cfGet(token, `${base}/queues`)
|
|
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([])
|
|
106
342
|
]);
|
|
107
343
|
return {
|
|
108
344
|
kv: new Map(kv.map((namespace) => [namespace.title, namespace.id])),
|
|
@@ -112,82 +348,53 @@ const listExisting = async (token, accountId) => {
|
|
|
112
348
|
};
|
|
113
349
|
};
|
|
114
350
|
//#endregion
|
|
115
|
-
//#region src/plugins/deploy/
|
|
351
|
+
//#region src/plugins/deploy/auth/verify.ts
|
|
116
352
|
/**
|
|
117
|
-
*
|
|
118
|
-
* (kv/d1) when it does. Durable Objects are config-only (they ship with the script), so they are
|
|
119
|
-
* always treated as "missing" — provisioning them is a no-op that just records the binding.
|
|
353
|
+
* @file deploy plugin — `.env` token verification + account resolution.
|
|
120
354
|
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* ```ts
|
|
126
|
-
* checkExisting({ kind: "kv", binding: "SESSIONS" }, existing); // { exists: true, id: "ns123" }
|
|
127
|
-
* ```
|
|
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.
|
|
128
359
|
*/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
case "kv": {
|
|
132
|
-
const id = existing.kv.get(resource.binding);
|
|
133
|
-
return id === void 0 ? { exists: false } : {
|
|
134
|
-
exists: true,
|
|
135
|
-
id
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
case "d1": {
|
|
139
|
-
const id = existing.d1.get(resource.binding);
|
|
140
|
-
return id === void 0 ? { exists: false } : {
|
|
141
|
-
exists: true,
|
|
142
|
-
id
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
case "r2": return { exists: existing.r2.has(resource.bucket) };
|
|
146
|
-
case "queue": return { exists: resource.producers.every((producer) => existing.queue.has(producer)) };
|
|
147
|
-
case "do": return { exists: false };
|
|
148
|
-
}
|
|
149
|
-
};
|
|
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.";
|
|
150
362
|
/**
|
|
151
|
-
*
|
|
152
|
-
* the manifest, emit `provision:plan`, and return the plan. Writes nothing.
|
|
363
|
+
* Verify the `.env` Cloudflare API token and resolve its account.
|
|
153
364
|
*
|
|
154
365
|
* @param ctx - The deploy plugin context (env + emit).
|
|
155
|
-
* @
|
|
156
|
-
* @
|
|
157
|
-
* @throws {Error} When the token is absent/invalid or a Cloudflare listing fails.
|
|
366
|
+
* @returns The verified auth status (account + id).
|
|
367
|
+
* @throws {Error} When the token is absent, invalid/expired, or not active.
|
|
158
368
|
* @example
|
|
159
369
|
* ```ts
|
|
160
|
-
* const
|
|
370
|
+
* const { account, accountId } = await verifyAuth(ctx);
|
|
161
371
|
* ```
|
|
162
372
|
*/
|
|
163
|
-
const
|
|
164
|
-
const token = ctx.env.
|
|
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}`);
|
|
165
383
|
const pinnedAccountId = ctx.env.get("CLOUDFLARE_ACCOUNT_ID");
|
|
166
|
-
const account = pinnedAccountId ? {
|
|
384
|
+
const account = pinnedAccountId === void 0 || pinnedAccountId === "" ? await resolveAccount(token) : {
|
|
167
385
|
id: pinnedAccountId,
|
|
168
386
|
name: pinnedAccountId
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const check = checkExisting(resource, existing);
|
|
175
|
-
if (check.exists) exists.push(check.id === void 0 ? { resource } : {
|
|
176
|
-
resource,
|
|
177
|
-
id: check.id
|
|
178
|
-
});
|
|
179
|
-
else missing.push(resource);
|
|
180
|
-
}
|
|
181
|
-
ctx.emit("provision:plan", {
|
|
182
|
-
exists: exists.length,
|
|
183
|
-
missing: missing.length,
|
|
184
|
-
account: account.name
|
|
387
|
+
};
|
|
388
|
+
ctx.emit("auth:verified", {
|
|
389
|
+
account: account.name,
|
|
390
|
+
accountId: account.id,
|
|
391
|
+
scopes: []
|
|
185
392
|
});
|
|
186
393
|
return {
|
|
394
|
+
ok: true,
|
|
187
395
|
account: account.name,
|
|
188
396
|
accountId: account.id,
|
|
189
|
-
|
|
190
|
-
missing
|
|
397
|
+
scopes: []
|
|
191
398
|
};
|
|
192
399
|
};
|
|
193
400
|
//#endregion
|
|
@@ -259,6 +466,411 @@ const runWrangler = (args) => new Promise((resolve, reject) => {
|
|
|
259
466
|
resolve(args[0] === "deploy" ? extractDeployedUrl(stdout) : stdout);
|
|
260
467
|
});
|
|
261
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
|
+
};
|
|
262
874
|
//#endregion
|
|
263
875
|
//#region src/plugins/deploy/providers/d1.ts
|
|
264
876
|
/**
|
|
@@ -505,6 +1117,25 @@ const provisionResource = async (resource, ci) => {
|
|
|
505
1117
|
}
|
|
506
1118
|
};
|
|
507
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
|
|
508
1139
|
//#region src/plugins/deploy/wrangler-config.ts
|
|
509
1140
|
/**
|
|
510
1141
|
* @file deploy plugin — wrangler config generation + scaffold.
|
|
@@ -791,21 +1422,38 @@ const createDeployApi = (ctx) => ({
|
|
|
791
1422
|
* it is used verbatim (universal path).
|
|
792
1423
|
*
|
|
793
1424
|
* @param opts - Optional run options.
|
|
794
|
-
* @param opts.guided - Enable interactive confirmation steps (
|
|
795
|
-
* @param opts.yes - Auto-confirm all prompts (
|
|
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.
|
|
796
1428
|
* @param opts.manifest - Caller-supplied manifest (bypasses deployManifest() assembly).
|
|
797
1429
|
* @returns Resolves once the deploy completes.
|
|
798
1430
|
* @example
|
|
799
1431
|
* ```ts
|
|
800
|
-
* await api.run({ guided: true });
|
|
1432
|
+
* await api.run({ guided: true, webBuild: () => web.cli.build() });
|
|
801
1433
|
* await api.run({ manifest: { name: "w", compatibilityDate: "2026-06-17", resources: [] } });
|
|
802
1434
|
* ```
|
|
803
1435
|
*/
|
|
804
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
|
+
}
|
|
805
1448
|
ctx.emit("deploy:phase", { phase: "detect" });
|
|
806
1449
|
const manifest = opts?.manifest ?? assembleManifest(ctx);
|
|
807
1450
|
ctx.emit("deploy:phase", { phase: "provision" });
|
|
808
|
-
const
|
|
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);
|
|
809
1457
|
ctx.emit("deploy:phase", { phase: "wrangler-config" });
|
|
810
1458
|
await writeWranglerConfig(ctx.config.configFile, manifest, ids);
|
|
811
1459
|
const r2Resource = manifest.resources.find((resource) => resource.kind === "r2");
|
|
@@ -816,6 +1464,10 @@ const createDeployApi = (ctx) => ({
|
|
|
816
1464
|
detail: `${String(count)} files`
|
|
817
1465
|
});
|
|
818
1466
|
}
|
|
1467
|
+
if (!await confirm(`Deploy "${manifest.name}" to ${ctx.global.stage}?`)) {
|
|
1468
|
+
ctx.emit("deploy:phase", { phase: "aborted" });
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
819
1471
|
ctx.emit("deploy:phase", { phase: "deploy" });
|
|
820
1472
|
const url = await runWrangler([
|
|
821
1473
|
"deploy",
|
|
@@ -825,25 +1477,20 @@ const createDeployApi = (ctx) => ({
|
|
|
825
1477
|
ctx.emit("deploy:complete", { url });
|
|
826
1478
|
},
|
|
827
1479
|
/**
|
|
828
|
-
* Start a local
|
|
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.
|
|
829
1483
|
*
|
|
830
1484
|
* @param opts - Optional options.
|
|
831
1485
|
* @param opts.port - Local dev port (default 8787).
|
|
1486
|
+
* @param opts.webBuild - Rebuild the web site on change (e.g. `() => webApp.cli.build()`).
|
|
832
1487
|
* @returns Resolves when the dev session ends.
|
|
833
1488
|
* @example
|
|
834
1489
|
* ```ts
|
|
835
|
-
* await api.dev({ port: 8787 });
|
|
1490
|
+
* await api.dev({ port: 8787, webBuild: () => web.cli.build() });
|
|
836
1491
|
* ```
|
|
837
1492
|
*/
|
|
838
|
-
dev:
|
|
839
|
-
await runWrangler([
|
|
840
|
-
"dev",
|
|
841
|
-
"--port",
|
|
842
|
-
String(opts?.port ?? 8787),
|
|
843
|
-
"--config",
|
|
844
|
-
ctx.config.configFile
|
|
845
|
-
]);
|
|
846
|
-
},
|
|
1493
|
+
dev: (opts) => runDev(ctx, opts, realDevDeps()),
|
|
847
1494
|
/**
|
|
848
1495
|
* Scaffold a starting wrangler config (and CI files when ci is set).
|
|
849
1496
|
* Idempotent: an existing config file is left untouched.
|
|
@@ -880,7 +1527,49 @@ const createDeployApi = (ctx) => ({
|
|
|
880
1527
|
* const { created } = await api.provisionInfra(await api.checkInfra());
|
|
881
1528
|
* ```
|
|
882
1529
|
*/
|
|
883
|
-
provisionInfra: (plan) => applyPlan(ctx, plan)
|
|
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)
|
|
884
1573
|
});
|
|
885
1574
|
/**
|
|
886
1575
|
* Complex tier (node-only) — build-time deploy orchestrator over the five resource plugins.
|
|
@@ -897,7 +1586,11 @@ const createDeployApi = (ctx) => ({
|
|
|
897
1586
|
const deployPlugin = require_storage.createPlugin("deploy", {
|
|
898
1587
|
config: {
|
|
899
1588
|
configFile: "wrangler.jsonc",
|
|
900
|
-
ci: false
|
|
1589
|
+
ci: false,
|
|
1590
|
+
watch: ["src/**/*.{ts,tsx,css}", "public/**/*"],
|
|
1591
|
+
buildCommand: "",
|
|
1592
|
+
migrateLocal: true,
|
|
1593
|
+
debounceMs: 120
|
|
901
1594
|
},
|
|
902
1595
|
depends: [
|
|
903
1596
|
require_storage.storagePlugin,
|
|
@@ -911,6 +1604,9 @@ const deployPlugin = require_storage.createPlugin("deploy", {
|
|
|
911
1604
|
//#endregion
|
|
912
1605
|
//#region src/plugins/cli/api.ts
|
|
913
1606
|
/**
|
|
1607
|
+
* @file cli plugin — API factory (dev, deploy, auth, doctor).
|
|
1608
|
+
*/
|
|
1609
|
+
/**
|
|
914
1610
|
* Builds app.cli.* — thin passthroughs to the deploy plugin via ctx.require(deployPlugin).
|
|
915
1611
|
* Both verbs forward their opts verbatim; `dev` defaults port to ctx.config.port when no
|
|
916
1612
|
* opts are supplied.
|
|
@@ -926,37 +1622,137 @@ const deployPlugin = require_storage.createPlugin("deploy", {
|
|
|
926
1622
|
*/
|
|
927
1623
|
const createCliApi = (ctx) => ({
|
|
928
1624
|
/**
|
|
929
|
-
* Run the Worker locally; defaults port to ctx.config.port (8787) when no opts supplied.
|
|
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.
|
|
930
1628
|
*
|
|
931
1629
|
* @param opts - Optional local dev options.
|
|
932
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()`).
|
|
933
1632
|
* @returns Resolves when the dev session ends.
|
|
934
1633
|
* @example
|
|
935
1634
|
* ```ts
|
|
936
|
-
* await api.dev();
|
|
937
|
-
* await api.dev({
|
|
1635
|
+
* await api.dev(); // port 8787, worker only
|
|
1636
|
+
* await api.dev({ webBuild: () => web.cli.build() }); // wire the web build in
|
|
938
1637
|
* ```
|
|
939
1638
|
*/
|
|
940
1639
|
dev(opts) {
|
|
941
|
-
|
|
1640
|
+
const port = opts?.port ?? ctx.config.port;
|
|
1641
|
+
return ctx.require(deployPlugin).dev(opts?.webBuild ? {
|
|
1642
|
+
port,
|
|
1643
|
+
webBuild: opts.webBuild
|
|
1644
|
+
} : { port });
|
|
942
1645
|
},
|
|
943
1646
|
/**
|
|
944
1647
|
* One-command guided Cloudflare deploy; forwards flags verbatim to deploy.run.
|
|
945
|
-
* Passes `undefined` when called with no opts (not a default empty object).
|
|
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.
|
|
946
1650
|
*
|
|
947
1651
|
* @param opts - Optional deploy options.
|
|
948
1652
|
* @param opts.guided - Walk through each step interactively.
|
|
949
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.
|
|
950
1655
|
* @returns Resolves once the deploy completes.
|
|
951
1656
|
* @example
|
|
952
1657
|
* ```ts
|
|
953
|
-
* await api.deploy({ guided: true });
|
|
1658
|
+
* await api.deploy({ guided: true, webBuild: () => web.cli.build() });
|
|
954
1659
|
* await api.deploy({ yes: true }); // CI
|
|
955
1660
|
* await api.deploy(); // opts === undefined
|
|
956
1661
|
* ```
|
|
957
1662
|
*/
|
|
958
1663
|
deploy(opts) {
|
|
959
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);
|
|
960
1756
|
}
|
|
961
1757
|
});
|
|
962
1758
|
//#endregion
|
|
@@ -1029,6 +1825,43 @@ const createCliHooks = (ctx) => ({
|
|
|
1029
1825
|
ctx.log.info(`${p.kind} ${p.name} (exists)`);
|
|
1030
1826
|
},
|
|
1031
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
|
+
/**
|
|
1032
1865
|
* Log the terminal success line with the deployed URL.
|
|
1033
1866
|
*
|
|
1034
1867
|
* @param p - The deploy:complete event payload.
|