@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.mjs CHANGED
@@ -1,9 +1,227 @@
1
1
  import { i as durableObjectsPlugin, n as queuesPlugin, o as d1Plugin, r as kvPlugin, t as storagePlugin, u as createPlugin } from "./storage-COo-F38H.mjs";
2
- import { brandedSink } from "@moku-labs/common/cli";
2
+ import { brandedSink, createBrandConsole, createBrandPrompts } from "@moku-labs/common/cli";
3
3
  import { spawn } from "node:child_process";
4
- import { readdir, stat, writeFile } from "node:fs/promises";
4
+ import { existsSync, readFileSync, watch } from "node:fs";
5
5
  import path from "node:path";
6
- import { existsSync, readFileSync } from "node:fs";
6
+ import { readdir, stat, writeFile } from "node:fs/promises";
7
+ //#region src/plugins/deploy/auth/permissions.ts
8
+ /** Permission groups every deploy needs, regardless of resources. */
9
+ const ALWAYS = [{
10
+ group: "Account · Workers Scripts",
11
+ scope: "Edit",
12
+ reason: "deploy",
13
+ inBaseTemplate: true
14
+ }, {
15
+ group: "Account · Account Settings",
16
+ scope: "Read",
17
+ reason: "account",
18
+ inBaseTemplate: true
19
+ }];
20
+ /**
21
+ * Per-resource-kind permission group. `do` needs nothing extra (Durable Objects ship with the
22
+ * Worker script, covered by Workers Scripts · Edit). `d1`/`queue` are NOT in the stock template.
23
+ */
24
+ const BY_KIND = {
25
+ kv: {
26
+ group: "Account · Workers KV Storage",
27
+ scope: "Edit",
28
+ reason: "kv",
29
+ inBaseTemplate: true
30
+ },
31
+ r2: {
32
+ group: "Account · Workers R2 Storage",
33
+ scope: "Edit",
34
+ reason: "r2",
35
+ inBaseTemplate: true
36
+ },
37
+ d1: {
38
+ group: "Account · D1",
39
+ scope: "Edit",
40
+ reason: "d1",
41
+ inBaseTemplate: false
42
+ },
43
+ queue: {
44
+ group: "Account · Queues",
45
+ scope: "Edit",
46
+ reason: "queue",
47
+ inBaseTemplate: false
48
+ },
49
+ do: void 0
50
+ };
51
+ /**
52
+ * Derive the Cloudflare API token requirement from an app manifest: the full permission set plus
53
+ * the subset that must be ADDED to the stock "Edit Cloudflare Workers" template.
54
+ *
55
+ * @param manifest - The assembled deploy manifest.
56
+ * @returns The token requirement (base template, full required set, and groups to add).
57
+ * @example
58
+ * ```ts
59
+ * const { toAdd } = requiredToken({ name: "w", compatibilityDate: "…", resources: [{ kind: "d1", binding: "DB" }] });
60
+ * // toAdd → [{ group: "Account · D1", scope: "Edit", … }]
61
+ * ```
62
+ */
63
+ const requiredToken = (manifest) => {
64
+ const required = [...ALWAYS];
65
+ const seen = new Set(required.map((permission) => permission.group));
66
+ for (const resource of manifest.resources) {
67
+ const permission = BY_KIND[resource.kind];
68
+ if (permission !== void 0 && !seen.has(permission.group)) {
69
+ required.push(permission);
70
+ seen.add(permission.group);
71
+ }
72
+ }
73
+ return {
74
+ base: "Edit Cloudflare Workers",
75
+ required,
76
+ toAdd: required.filter((permission) => !permission.inBaseTemplate)
77
+ };
78
+ };
79
+ /** Permission every CI/automation redeploy needs: ship the Worker script. */
80
+ const CI_ALWAYS = [{
81
+ group: "Account · Workers Scripts",
82
+ scope: "Edit",
83
+ reason: "deploy",
84
+ inBaseTemplate: true
85
+ }];
86
+ /**
87
+ * Per-resource-kind permission for the CI/automation token. After a first LOCAL deploy has
88
+ * provisioned everything, CI only needs to LIST existing infra (the idempotent preflight) and
89
+ * ship — so data resources drop to `Read`; R2 stays `Edit` because asset upload writes objects.
90
+ */
91
+ const CI_BY_KIND = {
92
+ kv: {
93
+ group: "Account · Workers KV Storage",
94
+ scope: "Read",
95
+ reason: "kv (preflight)",
96
+ inBaseTemplate: true
97
+ },
98
+ r2: {
99
+ group: "Account · Workers R2 Storage",
100
+ scope: "Edit",
101
+ reason: "r2 (asset upload)",
102
+ inBaseTemplate: true
103
+ },
104
+ d1: {
105
+ group: "Account · D1",
106
+ scope: "Read",
107
+ reason: "d1 (preflight)",
108
+ inBaseTemplate: false
109
+ },
110
+ queue: {
111
+ group: "Account · Queues",
112
+ scope: "Read",
113
+ reason: "queue (preflight)",
114
+ inBaseTemplate: false
115
+ },
116
+ do: void 0
117
+ };
118
+ /**
119
+ * Derive the REDUCED Cloudflare API token for CI/automation redeploys, from the same manifest.
120
+ * Assumes a prior LOCAL deploy already provisioned the infra, so CI never creates: data resources
121
+ * need only `Read` (the idempotent preflight lists them), R2 keeps `Edit` for asset upload, and no
122
+ * `Account Settings · Read` is needed because CI pins `CLOUDFLARE_ACCOUNT_ID`. Pure: no network.
123
+ *
124
+ * @param manifest - The assembled deploy manifest.
125
+ * @returns The minimum permission groups for a CI redeploy token (deduped, manifest-scoped).
126
+ * @example
127
+ * ```ts
128
+ * const groups = ciToken({ name: "w", compatibilityDate: "…", resources: [{ kind: "d1", binding: "DB" }] });
129
+ * // → [Workers Scripts·Edit, D1·Read]
130
+ * ```
131
+ */
132
+ const ciToken = (manifest) => {
133
+ const groups = [...CI_ALWAYS];
134
+ const seen = new Set(groups.map((permission) => permission.group));
135
+ for (const resource of manifest.resources) {
136
+ const permission = CI_BY_KIND[resource.kind];
137
+ if (permission !== void 0 && !seen.has(permission.group)) {
138
+ groups.push(permission);
139
+ seen.add(permission.group);
140
+ }
141
+ }
142
+ return groups;
143
+ };
144
+ //#endregion
145
+ //#region src/plugins/deploy/auth/setup.ts
146
+ /** Cloudflare's dashboard path for creating API tokens. */
147
+ const TOKENS_URL = "https://dash.cloudflare.com/profile/api-tokens";
148
+ /**
149
+ * Render the FULL local-first token section (the deploy that provisions everything): the permission
150
+ * table flagging template-missing rows, the template + "add these" steps, and the `.env.local` lines.
151
+ *
152
+ * @param requirement - The full token requirement (from requiredToken()).
153
+ * @returns The local-first section lines.
154
+ * @example
155
+ * ```ts
156
+ * const lines = localSection(requiredToken(manifest));
157
+ * ```
158
+ */
159
+ const localSection = (requirement) => {
160
+ const permissionRows = requirement.required.map((permission) => {
161
+ const flag = permission.inBaseTemplate ? "" : " <- add to template";
162
+ return ` - ${permission.group} : ${permission.scope} (${permission.reason})${flag}`;
163
+ });
164
+ 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.`];
165
+ return [
166
+ "LOCAL — first deploy (provisions infra). A Cloudflare API token with these permissions:",
167
+ "",
168
+ ...permissionRows,
169
+ "",
170
+ "Fastest path:",
171
+ ` 1. ${TOKENS_URL} -> Create Token`,
172
+ ` 2. Start from the "${requirement.base}" template.`,
173
+ ...step3,
174
+ " 4. Account Resources -> Include -> your account.",
175
+ " 5. Create the token, copy it, then add it to .env.local:",
176
+ " CLOUDFLARE_API_TOKEN=<paste your token>",
177
+ " CLOUDFLARE_ACCOUNT_ID=<your account id>",
178
+ " 6. Verify it with `auth` (app.deploy.verifyAuth())."
179
+ ];
180
+ };
181
+ /**
182
+ * Render the REDUCED CI/automation token section (redeploy-only): the scoped permission table plus
183
+ * the CI-secret + account-pin steps.
184
+ *
185
+ * @param groups - The CI permission groups (from ciToken()).
186
+ * @returns The CI section lines.
187
+ * @example
188
+ * ```ts
189
+ * const lines = ciSection(ciToken(manifest));
190
+ * ```
191
+ */
192
+ const ciSection = (groups) => {
193
+ return [
194
+ "CI — automation redeploy (infra already provisioned by a local deploy). A SCOPED token with:",
195
+ "",
196
+ ...groups.map((permission) => ` - ${permission.group} : ${permission.scope} (${permission.reason})`),
197
+ "",
198
+ ` 1. ${TOKENS_URL} -> Create Token -> Create Custom Token.`,
199
+ " 2. Add exactly the permissions above (Read, not Edit, on data resources — CI never creates).",
200
+ " 3. Account Resources -> Include -> your account.",
201
+ " 4. Store it as the CLOUDFLARE_API_TOKEN secret in CI, and PIN the account so no account",
202
+ " lookup (and no Account Settings -> Read) is needed:",
203
+ " CLOUDFLARE_ACCOUNT_ID=<your account id>",
204
+ " CI reuses the same idempotent pipeline — it lists existing infra and ships. To let CI also",
205
+ " CREATE missing infra (self-heal), give it the LOCAL token above instead."
206
+ ];
207
+ };
208
+ /**
209
+ * Render the `auth setup` instructions from the app manifest: the FULL local-first token (provisions
210
+ * everything) followed by the REDUCED CI/automation token (redeploy-only).
211
+ *
212
+ * @param manifest - The assembled deploy manifest.
213
+ * @returns A multi-line instruction string covering both tokens.
214
+ * @example
215
+ * ```ts
216
+ * const text = tokenInstructions(manifest);
217
+ * ```
218
+ */
219
+ const tokenInstructions = (manifest) => [
220
+ ...localSection(requiredToken(manifest)),
221
+ "",
222
+ ...ciSection(ciToken(manifest))
223
+ ].join("\n");
224
+ //#endregion
7
225
  //#region src/plugins/deploy/infra/cloudflare.ts
8
226
  /**
9
227
  * @file deploy plugin — Cloudflare REST discovery client (infra preflight).
@@ -59,26 +277,44 @@ const resolveAccount = async (token) => {
59
277
  };
60
278
  };
61
279
  /**
62
- * List every kv / d1 / r2 / queue resource that already exists in the account (one request per
63
- * kind, in parallel), indexed for the preflight diff.
280
+ * Verify a Cloudflare API token via `GET /user/tokens/verify`. Returns its status (`"active"` for
281
+ * a usable token); throws (via cfGet) when the token is rejected outright (401/invalid).
282
+ *
283
+ * @param token - The Cloudflare API token to verify.
284
+ * @returns The token status string reported by Cloudflare.
285
+ * @throws {Error} When the verify request fails (invalid/expired token).
286
+ * @example
287
+ * ```ts
288
+ * const { status } = await verifyToken(token); // status === "active"
289
+ * ```
290
+ */
291
+ const verifyToken = async (token) => {
292
+ return { status: (await cfGet(token, "/user/tokens/verify")).status };
293
+ };
294
+ /**
295
+ * List the resources that already exist in the account, querying ONLY the kinds the app declares
296
+ * (one request per declared kind, in parallel), indexed for the preflight diff. Scoping to the
297
+ * declared kinds keeps the API token minimal — an app with only KV never lists (and so never needs
298
+ * read permission on) D1, R2, or Queues.
64
299
  *
65
300
  * @param token - The Cloudflare API token.
66
301
  * @param accountId - The Cloudflare account id to scope the listings to.
67
- * @returns The existing resources, indexed by kind.
302
+ * @param kinds - The resource kinds present in the manifest (the only kinds queried).
303
+ * @returns The existing resources, indexed by kind (un-queried kinds resolve empty).
68
304
  * @throws {Error} When any listing request fails.
69
305
  * @example
70
306
  * ```ts
71
- * const existing = await listExisting(token, accountId);
307
+ * const existing = await listExisting(token, accountId, new Set(["kv", "d1"]));
72
308
  * if (existing.kv.has("SESSIONS")) { ... }
73
309
  * ```
74
310
  */
75
- const listExisting = async (token, accountId) => {
311
+ const listExisting = async (token, accountId, kinds) => {
76
312
  const base = `/accounts/${accountId}`;
77
313
  const [kv, d1, r2, queues] = await Promise.all([
78
- cfGet(token, `${base}/storage/kv/namespaces`),
79
- cfGet(token, `${base}/d1/database`),
80
- cfGet(token, `${base}/r2/buckets`),
81
- cfGet(token, `${base}/queues`)
314
+ kinds.has("kv") ? cfGet(token, `${base}/storage/kv/namespaces`) : Promise.resolve([]),
315
+ kinds.has("d1") ? cfGet(token, `${base}/d1/database`) : Promise.resolve([]),
316
+ kinds.has("r2") ? cfGet(token, `${base}/r2/buckets`) : Promise.resolve({}),
317
+ kinds.has("queue") ? cfGet(token, `${base}/queues`) : Promise.resolve([])
82
318
  ]);
83
319
  return {
84
320
  kv: new Map(kv.map((namespace) => [namespace.title, namespace.id])),
@@ -88,82 +324,53 @@ const listExisting = async (token, accountId) => {
88
324
  };
89
325
  };
90
326
  //#endregion
91
- //#region src/plugins/deploy/infra/plan.ts
327
+ //#region src/plugins/deploy/auth/verify.ts
92
328
  /**
93
- * Decide whether a single declared resource already exists in the account, recovering its id
94
- * (kv/d1) when it does. Durable Objects are config-only (they ship with the script), so they are
95
- * always treated as "missing" — provisioning them is a no-op that just records the binding.
329
+ * @file deploy plugin `.env` token verification + account resolution.
96
330
  *
97
- * @param resource - The declared resource descriptor.
98
- * @param existing - The indexed set of resources already in the account.
99
- * @returns Whether it exists, plus the captured id for kv/d1.
100
- * @example
101
- * ```ts
102
- * checkExisting({ kind: "kv", binding: "SESSIONS" }, existing); // { exists: true, id: "ns123" }
103
- * ```
331
+ * Reads CLOUDFLARE_API_TOKEN via ctx.env, verifies it is active against the Cloudflare API, and
332
+ * resolves the account. Emits auth:verified. Throws a branded, actionable error (pointing at
333
+ * `auth setup`) when the token is absent, invalid, or inactive never an interactive login.
334
+ * Node-only; never imported by the runtime Worker bundle.
104
335
  */
105
- const checkExisting = (resource, existing) => {
106
- switch (resource.kind) {
107
- case "kv": {
108
- const id = existing.kv.get(resource.binding);
109
- return id === void 0 ? { exists: false } : {
110
- exists: true,
111
- id
112
- };
113
- }
114
- case "d1": {
115
- const id = existing.d1.get(resource.binding);
116
- return id === void 0 ? { exists: false } : {
117
- exists: true,
118
- id
119
- };
120
- }
121
- case "r2": return { exists: existing.r2.has(resource.bucket) };
122
- case "queue": return { exists: resource.producers.every((producer) => existing.queue.has(producer)) };
123
- case "do": return { exists: false };
124
- }
125
- };
336
+ /** Branded hint appended to every auth failure so the user knows the next step. */
337
+ const SETUP_HINT = "Run `auth setup` for the exact token to create.";
126
338
  /**
127
- * Run the read-only infra preflight: resolve the account, list existing resources, diff against
128
- * the manifest, emit `provision:plan`, and return the plan. Writes nothing.
339
+ * Verify the `.env` Cloudflare API token and resolve its account.
129
340
  *
130
341
  * @param ctx - The deploy plugin context (env + emit).
131
- * @param manifest - The assembled (or caller-supplied) deploy manifest.
132
- * @returns The infra plan: existing (with ids) vs missing resources.
133
- * @throws {Error} When the token is absent/invalid or a Cloudflare listing fails.
342
+ * @returns The verified auth status (account + id).
343
+ * @throws {Error} When the token is absent, invalid/expired, or not active.
134
344
  * @example
135
345
  * ```ts
136
- * const plan = await planInfra(ctx, manifest);
346
+ * const { account, accountId } = await verifyAuth(ctx);
137
347
  * ```
138
348
  */
139
- const planInfra = async (ctx, manifest) => {
140
- const token = ctx.env.require("CLOUDFLARE_API_TOKEN");
349
+ const verifyAuth = async (ctx) => {
350
+ const token = ctx.env.get("CLOUDFLARE_API_TOKEN");
351
+ if (token === void 0 || token === "") throw new Error(`[moku-worker] CLOUDFLARE_API_TOKEN is not set. ${SETUP_HINT}`);
352
+ let status;
353
+ try {
354
+ ({status} = await verifyToken(token));
355
+ } catch (error) {
356
+ throw new Error(`[moku-worker] Cloudflare API token is invalid or expired. ${SETUP_HINT}`, { cause: error });
357
+ }
358
+ if (status !== "active") throw new Error(`[moku-worker] Cloudflare API token is "${status}", not active. ${SETUP_HINT}`);
141
359
  const pinnedAccountId = ctx.env.get("CLOUDFLARE_ACCOUNT_ID");
142
- const account = pinnedAccountId ? {
360
+ const account = pinnedAccountId === void 0 || pinnedAccountId === "" ? await resolveAccount(token) : {
143
361
  id: pinnedAccountId,
144
362
  name: pinnedAccountId
145
- } : await resolveAccount(token);
146
- const existing = await listExisting(token, account.id);
147
- const exists = [];
148
- const missing = [];
149
- for (const resource of manifest.resources) {
150
- const check = checkExisting(resource, existing);
151
- if (check.exists) exists.push(check.id === void 0 ? { resource } : {
152
- resource,
153
- id: check.id
154
- });
155
- else missing.push(resource);
156
- }
157
- ctx.emit("provision:plan", {
158
- exists: exists.length,
159
- missing: missing.length,
160
- account: account.name
363
+ };
364
+ ctx.emit("auth:verified", {
365
+ account: account.name,
366
+ accountId: account.id,
367
+ scopes: []
161
368
  });
162
369
  return {
370
+ ok: true,
163
371
  account: account.name,
164
372
  accountId: account.id,
165
- exists,
166
- missing
373
+ scopes: []
167
374
  };
168
375
  };
169
376
  //#endregion
@@ -235,6 +442,411 @@ const runWrangler = (args) => new Promise((resolve, reject) => {
235
442
  resolve(args[0] === "deploy" ? extractDeployedUrl(stdout) : stdout);
236
443
  });
237
444
  });
445
+ /**
446
+ * Spawn `wrangler` with the given args, inheriting stdio so its output streams live to the user's
447
+ * terminal (used by the generic passthrough and long-lived commands like `tail`).
448
+ *
449
+ * @param args - Wrangler CLI arguments (e.g. ["kv", "namespace", "list"]).
450
+ * @returns Resolves once wrangler exits successfully.
451
+ * @throws {Error} When wrangler cannot be spawned or exits non-zero.
452
+ * @example
453
+ * ```ts
454
+ * await runWranglerInherit(["kv", "namespace", "list"]);
455
+ * ```
456
+ */
457
+ const runWranglerInherit = (args) => {
458
+ return new Promise((resolve, reject) => {
459
+ const child = spawn("wrangler", args, { stdio: "inherit" });
460
+ child.on("error", (error) => {
461
+ reject(/* @__PURE__ */ new Error(`[moku-worker] Failed to spawn wrangler.\n ${error.message}`));
462
+ });
463
+ child.on("close", (code) => {
464
+ if (code === 0) {
465
+ resolve();
466
+ return;
467
+ }
468
+ reject(/* @__PURE__ */ new Error(`[moku-worker] wrangler exited with code ${String(code)}.`));
469
+ });
470
+ });
471
+ };
472
+ //#endregion
473
+ //#region src/plugins/deploy/dev/build.ts
474
+ /**
475
+ * @file deploy plugin — dev site-rebuild resolution.
476
+ *
477
+ * Resolves HOW to rebuild the Moku web site on change: the in-process `webBuild` hook (preferred,
478
+ * fast, typed — passed call-time from the consumer's script or set as a config default) → a
479
+ * `buildCommand` shell string → an auto-detected `scripts/build.ts`. When nothing is configured,
480
+ * dev serves the worker only and says so. Subprocesses inherit the parent env by default.
481
+ * Node-only; never imported by the runtime Worker bundle.
482
+ */
483
+ /** Convention build script auto-detected when no webBuild/buildCommand is configured. */
484
+ const AUTO_DETECT = "scripts/build.ts";
485
+ /**
486
+ * Run a shell build command, resolving on a zero exit and rejecting otherwise.
487
+ *
488
+ * @param command - The shell command to run (the consumer's own configured build).
489
+ * @returns Resolves once the command exits successfully.
490
+ * @throws {Error} When the command fails to start or exits non-zero.
491
+ * @example
492
+ * ```ts
493
+ * await runShellBuild("bun run scripts/build.ts");
494
+ * ```
495
+ */
496
+ const runShellBuild = (command) => {
497
+ return new Promise((resolve, reject) => {
498
+ const child = spawn(command, {
499
+ shell: true,
500
+ stdio: "inherit"
501
+ });
502
+ child.on("error", (error) => {
503
+ reject(/* @__PURE__ */ new Error(`[moku-worker] site build failed to start.\n ${error.message}`));
504
+ });
505
+ child.on("close", (code) => {
506
+ if (code === 0) {
507
+ resolve();
508
+ return;
509
+ }
510
+ reject(/* @__PURE__ */ new Error(`[moku-worker] site build exited with code ${String(code)}.`));
511
+ });
512
+ });
513
+ };
514
+ /**
515
+ * Rebuild the Moku web site using the resolved strategy: the call-time `webBuild` hook (the
516
+ * script-driven path), else the `webBuild` config default, else the `buildCommand` shell string,
517
+ * else an auto-detected `scripts/build.ts`. A hook's result is normalized to a `{ files }` count
518
+ * (0 when the hook reports none, and for the shell path where it is unknown).
519
+ *
520
+ * @param ctx - The deploy plugin context (config + emit).
521
+ * @param webBuild - Optional call-time web build hook (takes precedence over `ctx.config.webBuild`).
522
+ * @returns The rebuilt file count (0 for the shell path / a countless hook).
523
+ * @throws {Error} When the resolved shell build fails.
524
+ * @example
525
+ * ```ts
526
+ * const { files } = await buildSite(ctx, () => web.cli.build());
527
+ * ```
528
+ */
529
+ const buildSite = async (ctx, webBuild) => {
530
+ const hook = webBuild ?? ctx.config.webBuild;
531
+ if (hook !== void 0) return { files: (await hook())?.files ?? 0 };
532
+ const command = ctx.config.buildCommand || (existsSync(AUTO_DETECT) ? `bun run ${AUTO_DETECT}` : "");
533
+ if (command === "") {
534
+ ctx.emit("dev:error", { message: "No site build configured (pass webBuild or set buildCommand); serving worker only." });
535
+ return { files: 0 };
536
+ }
537
+ await runShellBuild(command);
538
+ return { files: 0 };
539
+ };
540
+ //#endregion
541
+ //#region src/plugins/deploy/dev/watch.ts
542
+ /**
543
+ * @file deploy plugin — debounced filesystem watcher for dev.
544
+ *
545
+ * Watches the top-level directories implied by the config globs (recursive) and fires a debounced
546
+ * change callback with the last changed path. Uses node:fs.watch — no extra dependency.
547
+ * Node-only; never imported by the runtime Worker bundle.
548
+ */
549
+ /**
550
+ * Derive the set of top-level directories to watch from glob patterns.
551
+ *
552
+ * @param globs - Watch globs (e.g. ["src/**\/*.ts", "public/**\/*"]).
553
+ * @returns The distinct top-level directories (e.g. ["src", "public"]).
554
+ * @example
555
+ * ```ts
556
+ * watchDirectories(["src/**\/*.ts", "public/**\/*"]); // ["src", "public"]
557
+ * ```
558
+ */
559
+ const watchDirectories = (globs) => {
560
+ const directories = /* @__PURE__ */ new Set();
561
+ for (const glob of globs) {
562
+ const globStart = glob.search(/[*?[{]/u);
563
+ const top = (globStart === -1 ? path.dirname(glob) : glob.slice(0, globStart)).split(/[/\\]/u).find((segment) => segment !== "") ?? ".";
564
+ directories.add(top);
565
+ }
566
+ return [...directories];
567
+ };
568
+ /**
569
+ * Watch the directories implied by `globs` and fire `onChange` (debounced by `debounceMs`) with
570
+ * the last changed path. Missing directories are skipped silently.
571
+ *
572
+ * @param globs - Watch globs.
573
+ * @param debounceMs - Coalesce rapid changes into one callback within this window.
574
+ * @param onChange - Called with the last changed path after the debounce settles.
575
+ * @returns A handle whose close() stops all watchers and cancels any pending callback.
576
+ * @example
577
+ * ```ts
578
+ * const handle = watchPaths(["src/**\/*.ts"], 120, p => rebuild(p));
579
+ * handle.close();
580
+ * ```
581
+ */
582
+ const watchPaths = (globs, debounceMs, onChange) => {
583
+ let timer;
584
+ let lastPath = "";
585
+ const fire = (changedPath) => {
586
+ lastPath = changedPath;
587
+ if (timer !== void 0) clearTimeout(timer);
588
+ timer = setTimeout(() => {
589
+ onChange(lastPath);
590
+ }, debounceMs);
591
+ };
592
+ const watchers = [];
593
+ for (const directory of watchDirectories(globs)) {
594
+ if (!existsSync(directory)) continue;
595
+ watchers.push(watch(directory, { recursive: true }, (_event, filename) => {
596
+ if (filename !== null) fire(path.join(directory, filename.toString()));
597
+ }));
598
+ }
599
+ return { close: () => {
600
+ if (timer !== void 0) clearTimeout(timer);
601
+ for (const watcher of watchers) watcher.close();
602
+ } };
603
+ };
604
+ //#endregion
605
+ //#region src/plugins/deploy/dev/runner.ts
606
+ /**
607
+ * @file deploy plugin — dev watch/recompile orchestrator.
608
+ *
609
+ * One long-lived session: cold-build the Moku site, optionally apply local D1 migrations, spawn
610
+ * `wrangler dev --live-reload` ONCE, then watch the site sources and rebuild on change (wrangler's
611
+ * asset server live-reloads the browser). Build failures keep the session serving the last good
612
+ * build. Tears down cleanly on SIGINT. Side-effecting work is injected via DevDeps so the
613
+ * orchestration is unit-testable without real processes, watchers, or signals.
614
+ * Node-only; never imported by the runtime Worker bundle.
615
+ */
616
+ /**
617
+ * Spawn the long-lived `wrangler dev` child (inherits the parent env; non-blocking).
618
+ *
619
+ * @param args - The `wrangler dev …` arguments.
620
+ * @returns A handle exposing kill().
621
+ * @example
622
+ * ```ts
623
+ * const child = spawnWranglerDev(["dev", "--port", "8787"]);
624
+ * ```
625
+ */
626
+ const spawnWranglerDev = (args) => {
627
+ const child = spawn("wrangler", args, { stdio: "inherit" });
628
+ return { kill: () => child.kill() };
629
+ };
630
+ /**
631
+ * Resolve when the user first interrupts the dev session (SIGINT).
632
+ *
633
+ * @returns A promise that settles on the first SIGINT.
634
+ * @example
635
+ * ```ts
636
+ * await waitForSigint();
637
+ * ```
638
+ */
639
+ const waitForSigint = () => {
640
+ return new Promise((resolve) => {
641
+ process.once("SIGINT", () => {
642
+ resolve();
643
+ });
644
+ });
645
+ };
646
+ /**
647
+ * Wall-clock timestamp in ms (extracted so realDevDeps holds only named references).
648
+ *
649
+ * @returns The current time in milliseconds.
650
+ * @example
651
+ * ```ts
652
+ * const t = nowMs();
653
+ * ```
654
+ */
655
+ const nowMs = () => Date.now();
656
+ /**
657
+ * Build the real (side-effecting) dev deps used by api.dev(). Subprocesses inherit the parent env.
658
+ *
659
+ * @returns The production DevDeps (real spawn / fs.watch / SIGINT / Date.now).
660
+ * @example
661
+ * ```ts
662
+ * await runDev(ctx, opts, realDevDeps());
663
+ * ```
664
+ */
665
+ const realDevDeps = () => ({
666
+ build: buildSite,
667
+ runWrangler,
668
+ spawnDev: spawnWranglerDev,
669
+ watch: watchPaths,
670
+ untilSignal: waitForSigint,
671
+ now: nowMs
672
+ });
673
+ /**
674
+ * The d1 binding to migrate locally, when a d1 plugin is present in the app.
675
+ *
676
+ * @param ctx - The deploy plugin context.
677
+ * @returns The d1 binding name, or undefined when no d1 plugin is present.
678
+ * @example
679
+ * ```ts
680
+ * const binding = d1Binding(ctx); // "DB" | undefined
681
+ * ```
682
+ */
683
+ const d1Binding = (ctx) => ctx.has("d1") ? ctx.require(d1Plugin).deployManifest().binding : void 0;
684
+ /**
685
+ * Rebuild the site once and announce the result. A failed build keeps the session alive (it just
686
+ * emits dev:error and serves the last good build).
687
+ *
688
+ * @param ctx - The deploy plugin context.
689
+ * @param deps - The injected dev deps.
690
+ * @param changedPath - The path that triggered the rebuild.
691
+ * @param webBuild - Optional call-time web build hook threaded into the rebuild.
692
+ * @returns Resolves once the rebuild attempt completes.
693
+ * @example
694
+ * ```ts
695
+ * await rebuild(ctx, deps, "src/app.tsx", () => web.cli.build());
696
+ * ```
697
+ */
698
+ const rebuild = async (ctx, deps, changedPath, webBuild) => {
699
+ ctx.emit("dev:phase", {
700
+ phase: "rebuild",
701
+ detail: changedPath
702
+ });
703
+ const started = deps.now();
704
+ try {
705
+ const { files } = await deps.build(ctx, webBuild);
706
+ ctx.emit("dev:rebuilt", {
707
+ files,
708
+ ms: deps.now() - started
709
+ });
710
+ } catch (error) {
711
+ ctx.emit("dev:error", { message: error instanceof Error ? error.message : String(error) });
712
+ }
713
+ };
714
+ /**
715
+ * Run a long-lived dev session: cold build → (local d1 migrate) → spawn `wrangler dev` →
716
+ * watch + rebuild on change → teardown on signal.
717
+ *
718
+ * @param ctx - The deploy plugin context (config + emit + require/has).
719
+ * @param opts - Optional options.
720
+ * @param opts.port - Local dev port (default 8787).
721
+ * @param opts.webBuild - Web build hook (re)run on cold build + each change (e.g. `() => web.cli.build()`).
722
+ * @param deps - Injected side effects (real ones from realDevDeps in production).
723
+ * @returns Resolves when the session ends (SIGINT).
724
+ * @example
725
+ * ```ts
726
+ * await runDev(ctx, { port: 8787, webBuild: () => web.cli.build() }, realDevDeps());
727
+ * ```
728
+ */
729
+ const runDev = async (ctx, opts, deps) => {
730
+ const port = opts?.port ?? 8787;
731
+ const webBuild = opts?.webBuild;
732
+ ctx.emit("dev:phase", {
733
+ phase: "build",
734
+ detail: "site"
735
+ });
736
+ await deps.build(ctx, webBuild);
737
+ const binding = d1Binding(ctx);
738
+ if (ctx.config.migrateLocal && binding !== void 0) {
739
+ ctx.emit("dev:phase", {
740
+ phase: "migrate",
741
+ detail: "d1 (local)"
742
+ });
743
+ await deps.runWrangler([
744
+ "d1",
745
+ "migrations",
746
+ "apply",
747
+ binding,
748
+ "--local"
749
+ ]);
750
+ }
751
+ ctx.emit("dev:phase", {
752
+ phase: "serve",
753
+ detail: `http://localhost:${String(port)}`
754
+ });
755
+ const child = deps.spawnDev([
756
+ "dev",
757
+ "--port",
758
+ String(port),
759
+ "--config",
760
+ ctx.config.configFile,
761
+ "--live-reload"
762
+ ]);
763
+ const watcher = deps.watch(ctx.config.watch, ctx.config.debounceMs, (changedPath) => rebuild(ctx, deps, changedPath, webBuild));
764
+ await deps.untilSignal();
765
+ watcher.close();
766
+ child.kill();
767
+ ctx.emit("dev:phase", { phase: "stopped" });
768
+ };
769
+ //#endregion
770
+ //#region src/plugins/deploy/infra/plan.ts
771
+ /**
772
+ * Decide whether a single declared resource already exists in the account, recovering its id
773
+ * (kv/d1) when it does. Durable Objects are config-only (they ship with the script), so they are
774
+ * always treated as "missing" — provisioning them is a no-op that just records the binding.
775
+ *
776
+ * @param resource - The declared resource descriptor.
777
+ * @param existing - The indexed set of resources already in the account.
778
+ * @returns Whether it exists, plus the captured id for kv/d1.
779
+ * @example
780
+ * ```ts
781
+ * checkExisting({ kind: "kv", binding: "SESSIONS" }, existing); // { exists: true, id: "ns123" }
782
+ * ```
783
+ */
784
+ const checkExisting = (resource, existing) => {
785
+ switch (resource.kind) {
786
+ case "kv": {
787
+ const id = existing.kv.get(resource.binding);
788
+ return id === void 0 ? { exists: false } : {
789
+ exists: true,
790
+ id
791
+ };
792
+ }
793
+ case "d1": {
794
+ const id = existing.d1.get(resource.binding);
795
+ return id === void 0 ? { exists: false } : {
796
+ exists: true,
797
+ id
798
+ };
799
+ }
800
+ case "r2": return { exists: existing.r2.has(resource.bucket) };
801
+ case "queue": return { exists: resource.producers.every((producer) => existing.queue.has(producer)) };
802
+ case "do": return { exists: false };
803
+ }
804
+ };
805
+ /**
806
+ * Run the read-only infra preflight: resolve the account, list existing resources, diff against
807
+ * the manifest, emit `provision:plan`, and return the plan. Writes nothing.
808
+ *
809
+ * @param ctx - The deploy plugin context (env + emit).
810
+ * @param manifest - The assembled (or caller-supplied) deploy manifest.
811
+ * @returns The infra plan: existing (with ids) vs missing resources.
812
+ * @throws {Error} When the token is absent/invalid or a Cloudflare listing fails.
813
+ * @example
814
+ * ```ts
815
+ * const plan = await planInfra(ctx, manifest);
816
+ * ```
817
+ */
818
+ const planInfra = async (ctx, manifest) => {
819
+ const token = ctx.env.require("CLOUDFLARE_API_TOKEN");
820
+ const pinnedAccountId = ctx.env.get("CLOUDFLARE_ACCOUNT_ID");
821
+ const account = pinnedAccountId ? {
822
+ id: pinnedAccountId,
823
+ name: pinnedAccountId
824
+ } : await resolveAccount(token);
825
+ const kinds = /* @__PURE__ */ new Set();
826
+ for (const resource of manifest.resources) if (resource.kind !== "do") kinds.add(resource.kind);
827
+ const existing = await listExisting(token, account.id, kinds);
828
+ const exists = [];
829
+ const missing = [];
830
+ for (const resource of manifest.resources) {
831
+ const check = checkExisting(resource, existing);
832
+ if (check.exists) exists.push(check.id === void 0 ? { resource } : {
833
+ resource,
834
+ id: check.id
835
+ });
836
+ else missing.push(resource);
837
+ }
838
+ ctx.emit("provision:plan", {
839
+ exists: exists.length,
840
+ missing: missing.length,
841
+ account: account.name
842
+ });
843
+ return {
844
+ account: account.name,
845
+ accountId: account.id,
846
+ exists,
847
+ missing
848
+ };
849
+ };
238
850
  //#endregion
239
851
  //#region src/plugins/deploy/providers/d1.ts
240
852
  /**
@@ -481,6 +1093,25 @@ const provisionResource = async (resource, ci) => {
481
1093
  }
482
1094
  };
483
1095
  //#endregion
1096
+ //#region src/plugins/deploy/tty.ts
1097
+ /**
1098
+ * @file deploy plugin — TTY detection (isolated so the guided flow is testable).
1099
+ *
1100
+ * The guided deploy only prompts on an interactive terminal; in a pipe or CI it must never block
1101
+ * on stdin. Kept in its own module so tests can mock it without stubbing `process.stdout`.
1102
+ * Node-only; never imported by the runtime Worker bundle.
1103
+ */
1104
+ /**
1105
+ * Whether stdout is an interactive TTY (so prompts are safe to show).
1106
+ *
1107
+ * @returns True when stdout is a terminal.
1108
+ * @example
1109
+ * ```ts
1110
+ * if (stdoutIsTty()) await prompts.confirm("Deploy?");
1111
+ * ```
1112
+ */
1113
+ const stdoutIsTty = () => process.stdout.isTTY === true;
1114
+ //#endregion
484
1115
  //#region src/plugins/deploy/wrangler-config.ts
485
1116
  /**
486
1117
  * @file deploy plugin — wrangler config generation + scaffold.
@@ -767,21 +1398,38 @@ const createDeployApi = (ctx) => ({
767
1398
  * it is used verbatim (universal path).
768
1399
  *
769
1400
  * @param opts - Optional run options.
770
- * @param opts.guided - Enable interactive confirmation steps (wired in a later phase).
771
- * @param opts.yes - Auto-confirm all prompts (wired in a later phase).
1401
+ * @param opts.guided - Enable interactive confirmation steps (only on a TTY, non-CI).
1402
+ * @param opts.yes - Auto-confirm all prompts (non-interactive / CI).
1403
+ * @param opts.webBuild - Build the web site first (e.g. `() => webApp.cli.build()`), before deploy.
772
1404
  * @param opts.manifest - Caller-supplied manifest (bypasses deployManifest() assembly).
773
1405
  * @returns Resolves once the deploy completes.
774
1406
  * @example
775
1407
  * ```ts
776
- * await api.run({ guided: true });
1408
+ * await api.run({ guided: true, webBuild: () => web.cli.build() });
777
1409
  * await api.run({ manifest: { name: "w", compatibilityDate: "2026-06-17", resources: [] } });
778
1410
  * ```
779
1411
  */
780
1412
  async run(opts) {
1413
+ const confirm = (opts?.guided ?? false) && !ctx.config.ci && !(opts?.yes ?? false) && stdoutIsTty() ? createBrandPrompts().confirm : async (_question) => true;
1414
+ ctx.emit("deploy:phase", { phase: "auth" });
1415
+ await verifyAuth(ctx);
1416
+ const webBuild = opts?.webBuild ?? ctx.config.webBuild;
1417
+ if (webBuild !== void 0) {
1418
+ ctx.emit("deploy:phase", {
1419
+ phase: "build",
1420
+ detail: "web"
1421
+ });
1422
+ await webBuild();
1423
+ }
781
1424
  ctx.emit("deploy:phase", { phase: "detect" });
782
1425
  const manifest = opts?.manifest ?? assembleManifest(ctx);
783
1426
  ctx.emit("deploy:phase", { phase: "provision" });
784
- const { ids } = await applyPlan(ctx, await planInfra(ctx, manifest));
1427
+ const plan = await planInfra(ctx, manifest);
1428
+ if (plan.missing.length > 0 && !await confirm(`Create ${plan.missing.length} missing resource(s) in "${plan.account}"?`)) {
1429
+ ctx.emit("deploy:phase", { phase: "aborted" });
1430
+ return;
1431
+ }
1432
+ const { ids } = await applyPlan(ctx, plan);
785
1433
  ctx.emit("deploy:phase", { phase: "wrangler-config" });
786
1434
  await writeWranglerConfig(ctx.config.configFile, manifest, ids);
787
1435
  const r2Resource = manifest.resources.find((resource) => resource.kind === "r2");
@@ -792,6 +1440,10 @@ const createDeployApi = (ctx) => ({
792
1440
  detail: `${String(count)} files`
793
1441
  });
794
1442
  }
1443
+ if (!await confirm(`Deploy "${manifest.name}" to ${ctx.global.stage}?`)) {
1444
+ ctx.emit("deploy:phase", { phase: "aborted" });
1445
+ return;
1446
+ }
795
1447
  ctx.emit("deploy:phase", { phase: "deploy" });
796
1448
  const url = await runWrangler([
797
1449
  "deploy",
@@ -801,25 +1453,20 @@ const createDeployApi = (ctx) => ({
801
1453
  ctx.emit("deploy:complete", { url });
802
1454
  },
803
1455
  /**
804
- * Start a local Cloudflare dev session via `wrangler dev`.
1456
+ * Start a long-lived local dev session: cold-build the Moku site, spawn `wrangler dev
1457
+ * --live-reload`, and watch the site sources — rebuilding on change (wrangler live-reloads the
1458
+ * browser). Resolves on SIGINT.
805
1459
  *
806
1460
  * @param opts - Optional options.
807
1461
  * @param opts.port - Local dev port (default 8787).
1462
+ * @param opts.webBuild - Rebuild the web site on change (e.g. `() => webApp.cli.build()`).
808
1463
  * @returns Resolves when the dev session ends.
809
1464
  * @example
810
1465
  * ```ts
811
- * await api.dev({ port: 8787 });
1466
+ * await api.dev({ port: 8787, webBuild: () => web.cli.build() });
812
1467
  * ```
813
1468
  */
814
- dev: async (opts) => {
815
- await runWrangler([
816
- "dev",
817
- "--port",
818
- String(opts?.port ?? 8787),
819
- "--config",
820
- ctx.config.configFile
821
- ]);
822
- },
1469
+ dev: (opts) => runDev(ctx, opts, realDevDeps()),
823
1470
  /**
824
1471
  * Scaffold a starting wrangler config (and CI files when ci is set).
825
1472
  * Idempotent: an existing config file is left untouched.
@@ -856,7 +1503,49 @@ const createDeployApi = (ctx) => ({
856
1503
  * const { created } = await api.provisionInfra(await api.checkInfra());
857
1504
  * ```
858
1505
  */
859
- provisionInfra: (plan) => applyPlan(ctx, plan)
1506
+ provisionInfra: (plan) => applyPlan(ctx, plan),
1507
+ /**
1508
+ * Verify the `.env` Cloudflare API token (must be active) and resolve its account; emits
1509
+ * auth:verified. Throws a branded error pointing at `auth setup` when absent/invalid/inactive.
1510
+ *
1511
+ * @returns The verified auth status (account + id).
1512
+ * @example
1513
+ * ```ts
1514
+ * const { account } = await api.verifyAuth();
1515
+ * ```
1516
+ */
1517
+ verifyAuth: () => verifyAuth(ctx),
1518
+ /**
1519
+ * Derive the minimum Cloudflare API token this app needs from its manifest (pure, no network).
1520
+ *
1521
+ * @returns The token requirement (full set + groups to add to the stock template).
1522
+ * @example
1523
+ * ```ts
1524
+ * const { toAdd } = api.requiredToken();
1525
+ * ```
1526
+ */
1527
+ requiredToken: () => requiredToken(assembleManifest(ctx)),
1528
+ /**
1529
+ * Render the `auth setup` guidance from the derived token requirement (pure, no network).
1530
+ *
1531
+ * @returns The rendered instruction text.
1532
+ * @example
1533
+ * ```ts
1534
+ * const text = api.tokenInstructions();
1535
+ * ```
1536
+ */
1537
+ tokenInstructions: () => tokenInstructions(assembleManifest(ctx)),
1538
+ /**
1539
+ * Run an arbitrary wrangler command, streaming its output (the branded CLI escape hatch).
1540
+ *
1541
+ * @param args - The wrangler arguments.
1542
+ * @returns Resolves once wrangler exits.
1543
+ * @example
1544
+ * ```ts
1545
+ * await api.wrangler(["kv", "namespace", "list"]);
1546
+ * ```
1547
+ */
1548
+ wrangler: (args) => runWranglerInherit(args)
860
1549
  });
861
1550
  /**
862
1551
  * Complex tier (node-only) — build-time deploy orchestrator over the five resource plugins.
@@ -873,7 +1562,11 @@ const createDeployApi = (ctx) => ({
873
1562
  const deployPlugin = createPlugin("deploy", {
874
1563
  config: {
875
1564
  configFile: "wrangler.jsonc",
876
- ci: false
1565
+ ci: false,
1566
+ watch: ["src/**/*.{ts,tsx,css}", "public/**/*"],
1567
+ buildCommand: "",
1568
+ migrateLocal: true,
1569
+ debounceMs: 120
877
1570
  },
878
1571
  depends: [
879
1572
  storagePlugin,
@@ -887,6 +1580,9 @@ const deployPlugin = createPlugin("deploy", {
887
1580
  //#endregion
888
1581
  //#region src/plugins/cli/api.ts
889
1582
  /**
1583
+ * @file cli plugin — API factory (dev, deploy, auth, doctor).
1584
+ */
1585
+ /**
890
1586
  * Builds app.cli.* — thin passthroughs to the deploy plugin via ctx.require(deployPlugin).
891
1587
  * Both verbs forward their opts verbatim; `dev` defaults port to ctx.config.port when no
892
1588
  * opts are supplied.
@@ -902,37 +1598,137 @@ const deployPlugin = createPlugin("deploy", {
902
1598
  */
903
1599
  const createCliApi = (ctx) => ({
904
1600
  /**
905
- * Run the Worker locally; defaults port to ctx.config.port (8787) when no opts supplied.
1601
+ * Run the Worker locally; defaults port to ctx.config.port (8787) when no opts supplied. A
1602
+ * `webBuild` hook (e.g. `() => webApp.cli.build()`) wires the web build into the dev loop so the
1603
+ * site recompiles on change — this is how an app-side script composes web + worker.
906
1604
  *
907
1605
  * @param opts - Optional local dev options.
908
1606
  * @param opts.port - Local dev port to bind. Defaults to ctx.config.port (8787).
1607
+ * @param opts.webBuild - Rebuild the web site on change (e.g. `() => webApp.cli.build()`).
909
1608
  * @returns Resolves when the dev session ends.
910
1609
  * @example
911
1610
  * ```ts
912
- * await api.dev(); // port 8787
913
- * await api.dev({ port: 3000 }); // port 3000
1611
+ * await api.dev(); // port 8787, worker only
1612
+ * await api.dev({ webBuild: () => web.cli.build() }); // wire the web build in
914
1613
  * ```
915
1614
  */
916
1615
  dev(opts) {
917
- return ctx.require(deployPlugin).dev(opts ?? { port: ctx.config.port });
1616
+ const port = opts?.port ?? ctx.config.port;
1617
+ return ctx.require(deployPlugin).dev(opts?.webBuild ? {
1618
+ port,
1619
+ webBuild: opts.webBuild
1620
+ } : { port });
918
1621
  },
919
1622
  /**
920
1623
  * One-command guided Cloudflare deploy; forwards flags verbatim to deploy.run.
921
- * Passes `undefined` when called with no opts (not a default empty object).
1624
+ * Passes `undefined` when called with no opts (not a default empty object). A `webBuild` hook
1625
+ * builds the web site first (before `wrangler deploy`) — how an app-side script ships web + worker.
922
1626
  *
923
1627
  * @param opts - Optional deploy options.
924
1628
  * @param opts.guided - Walk through each step interactively.
925
1629
  * @param opts.yes - Skip confirmation prompts (non-interactive / CI).
1630
+ * @param opts.webBuild - Build the web site first (e.g. `() => webApp.cli.build()`), before deploy.
926
1631
  * @returns Resolves once the deploy completes.
927
1632
  * @example
928
1633
  * ```ts
929
- * await api.deploy({ guided: true });
1634
+ * await api.deploy({ guided: true, webBuild: () => web.cli.build() });
930
1635
  * await api.deploy({ yes: true }); // CI
931
1636
  * await api.deploy(); // opts === undefined
932
1637
  * ```
933
1638
  */
934
1639
  deploy(opts) {
935
1640
  return ctx.require(deployPlugin).run(opts);
1641
+ },
1642
+ /**
1643
+ * Verify the `.env` token (no sub) or print the config-derived token guidance (`"setup"`),
1644
+ * rendered in Moku style. `setup` works without a token; verify reports the resolved account.
1645
+ *
1646
+ * @param sub - Pass "setup" to print guidance; omit to verify the current token.
1647
+ * @returns Resolves once the check or guidance render completes.
1648
+ * @example
1649
+ * ```ts
1650
+ * await api.auth("setup"); // print what token to create
1651
+ * await api.auth(); // verify the current token
1652
+ * ```
1653
+ */
1654
+ async auth(sub) {
1655
+ const deploy = ctx.require(deployPlugin);
1656
+ const ui = createBrandConsole();
1657
+ if (sub === "setup") {
1658
+ for (const line of deploy.tokenInstructions().split("\n")) ui.line(line);
1659
+ return;
1660
+ }
1661
+ try {
1662
+ const status = await deploy.verifyAuth();
1663
+ ui.check(true, "token valid", `account "${status.account}" (${status.accountId})`);
1664
+ } catch (error) {
1665
+ ui.error(error instanceof Error ? error.message : String(error));
1666
+ }
1667
+ },
1668
+ /**
1669
+ * One-shot preflight report: token + account (verifyAuth) then infra drift (checkInfra),
1670
+ * each as a branded check line. Stops after the token check when auth fails.
1671
+ *
1672
+ * @returns Resolves once the report is printed.
1673
+ * @example
1674
+ * ```ts
1675
+ * await api.doctor();
1676
+ * ```
1677
+ */
1678
+ async doctor() {
1679
+ const deploy = ctx.require(deployPlugin);
1680
+ const ui = createBrandConsole();
1681
+ ui.heading("doctor");
1682
+ let tokenOk = false;
1683
+ try {
1684
+ const status = await deploy.verifyAuth();
1685
+ tokenOk = true;
1686
+ ui.check(true, "token", `valid · account "${status.account}" (${status.accountId})`);
1687
+ } catch (error) {
1688
+ ui.check(false, "token", error instanceof Error ? error.message : String(error));
1689
+ }
1690
+ if (!tokenOk) {
1691
+ ui.line("Run `auth setup` for the exact token to create.");
1692
+ return;
1693
+ }
1694
+ try {
1695
+ const plan = await deploy.checkInfra();
1696
+ ui.check(true, "infra", `${plan.exists.length} exist, ${plan.missing.length} to create in "${plan.account}"`);
1697
+ } catch (error) {
1698
+ ui.check(false, "infra", error instanceof Error ? error.message : String(error));
1699
+ }
1700
+ },
1701
+ /**
1702
+ * Print the resolved Cloudflare account for the current `.env` token.
1703
+ *
1704
+ * @returns Resolves once the account summary is printed.
1705
+ * @example
1706
+ * ```ts
1707
+ * await api.whoami();
1708
+ * ```
1709
+ */
1710
+ async whoami() {
1711
+ const ui = createBrandConsole();
1712
+ try {
1713
+ const status = await ctx.require(deployPlugin).verifyAuth();
1714
+ ui.check(true, "account", `${status.account} (${status.accountId})`);
1715
+ } catch (error) {
1716
+ ui.error(error instanceof Error ? error.message : String(error));
1717
+ }
1718
+ },
1719
+ /**
1720
+ * Run an arbitrary wrangler command through the branded CLI (escape hatch). Streams its output.
1721
+ *
1722
+ * @param args - The wrangler arguments.
1723
+ * @returns Resolves once wrangler exits.
1724
+ * @example
1725
+ * ```ts
1726
+ * await api.wrangler(["kv", "namespace", "list"]);
1727
+ * ```
1728
+ */
1729
+ async wrangler(args) {
1730
+ createBrandConsole().heading(`wrangler ${args.join(" ")}`);
1731
+ await ctx.require(deployPlugin).wrangler(args);
936
1732
  }
937
1733
  });
938
1734
  //#endregion
@@ -1005,6 +1801,43 @@ const createCliHooks = (ctx) => ({
1005
1801
  ctx.log.info(`${p.kind} ${p.name} (exists)`);
1006
1802
  },
1007
1803
  /**
1804
+ * Log one dev-session phase: "phase" or "phase · detail".
1805
+ *
1806
+ * @param p - The dev:phase event payload.
1807
+ * @example
1808
+ * ```ts
1809
+ * handler({ phase: "serve", detail: "http://localhost:8787" }); // "serve · http://localhost:8787"
1810
+ * ```
1811
+ */
1812
+ "dev:phase"(p) {
1813
+ ctx.log.info(p.detail ? `${p.phase} · ${p.detail}` : p.phase);
1814
+ },
1815
+ /**
1816
+ * Log the site rebuild result: "site <n> files · <ms>ms" (omits the count when unknown).
1817
+ *
1818
+ * @param p - The dev:rebuilt event payload.
1819
+ * @example
1820
+ * ```ts
1821
+ * handler({ files: 12, ms: 240 }); // "site 12 files · 240ms"
1822
+ * handler({ files: 0, ms: 240 }); // "site · 240ms"
1823
+ * ```
1824
+ */
1825
+ "dev:rebuilt"(p) {
1826
+ ctx.log.info(p.files > 0 ? `site ${String(p.files)} files · ${String(p.ms)}ms` : `site · ${String(p.ms)}ms`);
1827
+ },
1828
+ /**
1829
+ * Log a non-fatal dev build failure via warn (the session keeps serving the last good build).
1830
+ *
1831
+ * @param p - The dev:error event payload.
1832
+ * @example
1833
+ * ```ts
1834
+ * handler({ message: "build failed" }); // warn "build failed"
1835
+ * ```
1836
+ */
1837
+ "dev:error"(p) {
1838
+ ctx.log.warn(p.message);
1839
+ },
1840
+ /**
1008
1841
  * Log the terminal success line with the deployed URL.
1009
1842
  *
1010
1843
  * @param p - The deploy:complete event payload.