@moku-labs/worker 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1631,6 +1631,76 @@ const runWranglerInherit = (args) => {
1631
1631
  });
1632
1632
  };
1633
1633
  //#endregion
1634
+ //#region src/plugins/deploy/seed.ts
1635
+ /**
1636
+ * @file deploy plugin — shared D1 seed helpers (resolve the target db, run a configured seed).
1637
+ *
1638
+ * Pure orchestration over an INJECTED wrangler runner, so the post-deploy REMOTE seed (api.ts) and
1639
+ * the dev-session LOCAL seed (dev/runner.ts) stay in lockstep — same file, same KV-reset semantics,
1640
+ * differing only in the `--remote` / `--local` scope. Migrations are NOT applied here: each caller
1641
+ * applies the schema first (the deploy's migration step / dev's local-migrate step), then seeds.
1642
+ * Node-only; never imported by the runtime Worker bundle.
1643
+ */
1644
+ /**
1645
+ * Resolve the single configured d1 database (or the one bound to `binding` when several exist) from
1646
+ * the d1 plugin's manifest. The shared resolver behind `seed()`, the post-deploy seed, and the dev
1647
+ * seed; throws a branded error when the choice is ambiguous (none/several, no binding) or unknown.
1648
+ *
1649
+ * @param ctx - The deploy plugin context.
1650
+ * @param binding - The d1 binding to target when more than one is configured; the sole one otherwise.
1651
+ * @returns The resolved d1 resource descriptor (its binding + optional migrations dir).
1652
+ * @throws {Error} When no single database resolves (none/several without a binding, or unknown binding).
1653
+ * @example
1654
+ * ```ts
1655
+ * const db = resolveD1(ctx, "DB");
1656
+ * ```
1657
+ */
1658
+ const resolveD1 = (ctx, binding) => {
1659
+ const databases = ctx.require(d1Plugin).deployManifest();
1660
+ const matched = binding === void 0 ? databases : databases.filter((db) => db.binding === binding);
1661
+ const target = matched.length === 1 ? matched[0] : void 0;
1662
+ if (target === void 0) throw new Error(binding === void 0 ? `[moku-worker] seed: ${String(databases.length)} d1 databases configured — pass { binding } to choose one.` : `[moku-worker] seed: no d1 database is bound to "${binding}".`);
1663
+ return target;
1664
+ };
1665
+ /**
1666
+ * Run a configured seed against one scope: execute the seed SQL against the d1 database, then delete
1667
+ * each configured cached KV key so the next read rebuilds it from the freshly-seeded rows. The
1668
+ * schema is assumed to exist (the caller applies migrations first), so this never migrates. The
1669
+ * wrangler runner is injected so the same orchestration serves the streamed deploy path and the
1670
+ * injectable dev path.
1671
+ *
1672
+ * @param ctx - The deploy plugin context.
1673
+ * @param run - The wrangler runner to execute each command through.
1674
+ * @param seed - The resolved seed config (SQL file, optional binding, KV keys to reset).
1675
+ * @param scope - The wrangler scope: `--remote` (deploy) or `--local` (dev).
1676
+ * @returns Resolves once the seed file has executed and every cached KV key is cleared.
1677
+ * @throws {Error} When no d1 database is configured, or the seed's binding cannot be resolved.
1678
+ * @example
1679
+ * ```ts
1680
+ * await runConfiguredSeed(ctx, runWranglerInherit, ctx.config.seed, "--remote");
1681
+ * ```
1682
+ */
1683
+ const runConfiguredSeed = async (ctx, run, seed, scope) => {
1684
+ if (!ctx.has("d1")) throw new Error("[moku-worker] seed: no d1 database is configured.");
1685
+ await run([
1686
+ "d1",
1687
+ "execute",
1688
+ resolveD1(ctx, seed.binding).binding,
1689
+ scope,
1690
+ "--file",
1691
+ seed.file
1692
+ ]);
1693
+ for (const entry of seed.resetKv ?? []) await run([
1694
+ "kv",
1695
+ "key",
1696
+ "delete",
1697
+ entry.key,
1698
+ "--binding",
1699
+ entry.binding,
1700
+ scope
1701
+ ]);
1702
+ };
1703
+ //#endregion
1634
1704
  //#region src/plugins/deploy/dev/build.ts
1635
1705
  /**
1636
1706
  * @file deploy plugin — dev site-rebuild resolution.
@@ -1953,8 +2023,32 @@ const rebuild = async (ctx, deps, changedPaths, hooks) => {
1953
2023
  }
1954
2024
  };
1955
2025
  /**
1956
- * Run a long-lived dev session: cold build (local d1 migrate) spawn `wrangler dev`
1957
- * watch + rebuild on change teardown on signal.
2026
+ * Load the configured seed into the LOCAL D1 for a `dev --seed` session: execute the SQL file, then
2027
+ * clear the configured cached KV keys so the app rebuilds them from the freshly-seeded rows. The
2028
+ * schema already exists (the migrate step above runs first), so this never migrates — the local
2029
+ * analogue of the deploy's remote seed, over the same `pluginConfigs.deploy.seed` config.
2030
+ *
2031
+ * @param ctx - The deploy plugin context.
2032
+ * @param deps - The injected dev deps (for the wrangler runner).
2033
+ * @returns Resolves once the seed file has executed and every cached KV key is cleared.
2034
+ * @throws {Error} When `--seed` is set but no seed is configured under `pluginConfigs.deploy.seed`.
2035
+ * @example
2036
+ * ```ts
2037
+ * await seedLocal(ctx, realDevDeps());
2038
+ * ```
2039
+ */
2040
+ const seedLocal = async (ctx, deps) => {
2041
+ const config = ctx.config.seed;
2042
+ if (config === void 0) throw new Error("[moku-worker] dev({ seed: true }) but no seed is configured — set pluginConfigs.deploy.seed.");
2043
+ ctx.emit("dev:phase", {
2044
+ phase: "seed",
2045
+ detail: config.file
2046
+ });
2047
+ await runConfiguredSeed(ctx, deps.runWrangler, config, "--local");
2048
+ };
2049
+ /**
2050
+ * Run a long-lived dev session: cold build → (local d1 migrate) → (local seed) → spawn `wrangler
2051
+ * dev` → watch + rebuild on change → teardown on signal.
1958
2052
  *
1959
2053
  * @param ctx - The deploy plugin context (config + emit + require/has).
1960
2054
  * @param opts - Optional options.
@@ -1962,23 +2056,25 @@ const rebuild = async (ctx, deps, changedPaths, hooks) => {
1962
2056
  * @param opts.webBuild - Cold-build hook (also the per-change rebuild when `onChange` is omitted).
1963
2057
  * @param opts.onChange - Incremental per-change rebuild hook (e.g. `c => web.cli.update(c)`); when
1964
2058
  * set, each debounced change rebuilds only the changed paths instead of a full `webBuild()`.
2059
+ * @param opts.seed - Load the configured seed into the LOCAL D1 (+ reset its KV keys) before serving.
1965
2060
  * @param deps - Injected side effects (real ones from realDevDeps in production).
1966
2061
  * @returns Resolves when the session ends (SIGINT).
1967
2062
  * @example
1968
2063
  * ```ts
1969
- * await runDev(ctx, { port: 8787, webBuild: () => web.cli.build(), onChange: c => web.cli.update(c) }, realDevDeps());
2064
+ * await runDev(ctx, { port: 8787, seed: true, webBuild: () => web.cli.build() }, realDevDeps());
1970
2065
  * ```
1971
2066
  */
1972
2067
  const runDev = async (ctx, opts, deps) => {
1973
2068
  const port = opts?.port ?? 8787;
1974
2069
  const webBuild = opts?.webBuild;
1975
2070
  const onChange = opts?.onChange;
2071
+ const seed = opts?.seed === true;
1976
2072
  ctx.emit("dev:phase", {
1977
2073
  phase: "build",
1978
2074
  detail: "site"
1979
2075
  });
1980
2076
  await deps.build(ctx, webBuild);
1981
- const migrationBindings = ctx.config.migrateLocal ? d1MigrationBindings(ctx) : [];
2077
+ const migrationBindings = ctx.config.migrateLocal || seed ? d1MigrationBindings(ctx) : [];
1982
2078
  if (migrationBindings.length > 0) {
1983
2079
  ctx.emit("dev:phase", {
1984
2080
  phase: "migrate",
@@ -1992,6 +2088,7 @@ const runDev = async (ctx, opts, deps) => {
1992
2088
  "--local"
1993
2089
  ]);
1994
2090
  }
2091
+ if (seed) await seedLocal(ctx, deps);
1995
2092
  ctx.emit("dev:phase", {
1996
2093
  phase: "serve",
1997
2094
  detail: `http://localhost:${String(port)}`
@@ -2974,17 +3071,33 @@ const HINTS = {
2974
3071
  deploy: "wrangler deploy failed — review the output above, then retry."
2975
3072
  };
2976
3073
  /**
2977
- * Emit the terminal `aborted` phase the single exit every guided gate/retry funnels through when
2978
- * the user stops the deploy. Factored out so each abort path renders one consistent line.
3074
+ * Emit the terminal `aborted` phase AND build the matching {@link DeployReport} the single exit
3075
+ * every guided gate/retry funnels through when the user stops the deploy (or auth was never set up).
3076
+ * Centralizing it keeps every abort path emitting one consistent line and returning the same shaped
3077
+ * report: `status: "aborted"`, both post-steps `"skipped"`, no errors — so a calling script sees a
3078
+ * clean stop, never a half-filled success, and the remote-DB migration/seed are guaranteed unrun.
2979
3079
  *
2980
3080
  * @param ctx - The deploy plugin context.
2981
- * @returns Nothing.
3081
+ * @param stage - The resolved deploy stage (echoed into the report).
3082
+ * @param startedAt - The run's start timestamp, for the elapsed field.
3083
+ * @returns The aborted deploy report.
2982
3084
  * @example
2983
3085
  * ```ts
2984
- * if (declined) return emitAborted(ctx);
3086
+ * if (declined) return aborted(ctx, stage, startedAt);
2985
3087
  * ```
2986
3088
  */
2987
- const emitAborted = (ctx) => ctx.emit("deploy:phase", { phase: "aborted" });
3089
+ const aborted = (ctx, stage, startedAt) => {
3090
+ ctx.emit("deploy:phase", { phase: "aborted" });
3091
+ return {
3092
+ ok: false,
3093
+ status: "aborted",
3094
+ stage,
3095
+ migration: "skipped",
3096
+ seed: "skipped",
3097
+ elapsedMs: Date.now() - startedAt,
3098
+ errors: []
3099
+ };
3100
+ };
2988
3101
  /**
2989
3102
  * The full guided token setup shown after an auth failure on a TTY. Offers to walk the user through
2990
3103
  * it, and when accepted: prints WHERE to create the Cloudflare token (dashboard URL, which template,
@@ -3204,6 +3317,104 @@ const guidedDeployStep = async (ctx, manifest, stage, deps) => {
3204
3317
  return url;
3205
3318
  };
3206
3319
  /**
3320
+ * Apply pending D1 migrations to the REMOTE database for every configured d1 instance that ships a
3321
+ * migrations dir — the generic, deploy-owned analogue of `wrangler d1 migrations apply <binding>
3322
+ * --remote`. The wrangler config was written earlier in the pipeline, so each binding resolves. The
3323
+ * caller runs this only AFTER a successful deploy, so a deploy that never happened never migrates a
3324
+ * remote DB. Streams wrangler's output; throws on the first non-zero exit (the caller folds it into
3325
+ * the report).
3326
+ *
3327
+ * @param ctx - The deploy plugin context.
3328
+ * @returns Resolves once every configured database's remote migrations have been applied.
3329
+ * @example
3330
+ * ```ts
3331
+ * await applyRemoteMigrations(ctx);
3332
+ * ```
3333
+ */
3334
+ const applyRemoteMigrations = async (ctx) => {
3335
+ if (!ctx.has("d1")) return;
3336
+ for (const database of ctx.require(d1Plugin).deployManifest()) if (database.migrations !== void 0) await runWranglerInherit([
3337
+ "d1",
3338
+ "migrations",
3339
+ "apply",
3340
+ database.binding,
3341
+ "--remote"
3342
+ ]);
3343
+ };
3344
+ /**
3345
+ * Render a post-deploy step's failure as a branded line and capture its message into `errors` —
3346
+ * folding the failure into the report instead of throwing, so a deploy that already went live still
3347
+ * yields a complete, honest report when a later remote step (migration/seed) fails.
3348
+ *
3349
+ * @param ui - The branded console to render the error through.
3350
+ * @param errors - The accumulator the captured message is pushed into.
3351
+ * @param error - The thrown error (or value) to brand and capture.
3352
+ * @returns The captured (branded) message.
3353
+ * @example
3354
+ * ```ts
3355
+ * captureFailure(ui, errors, new Error("[moku-worker] seed failed"));
3356
+ * ```
3357
+ */
3358
+ const captureFailure = (ui, errors, error) => {
3359
+ const message = error instanceof Error ? error.message : String(error);
3360
+ ui.error(message);
3361
+ errors.push(message);
3362
+ return message;
3363
+ };
3364
+ /**
3365
+ * Run the post-deploy remote steps — REACHED ONLY ON A SUCCESSFUL DEPLOY (every gate in `run` returns
3366
+ * early before here), so a deploy that never happened never touches a remote DB. Applies remote D1
3367
+ * migrations (when requested), then loads the configured seed (when requested) — but skips the seed
3368
+ * if the migration it depends on failed. Each step's failure is RENDERED inline and CAPTURED in
3369
+ * `errors` (never thrown), so one failed step still yields a complete, honest report.
3370
+ *
3371
+ * @param ctx - The deploy plugin context.
3372
+ * @param want - Which post-steps the caller requested.
3373
+ * @param want.migration - Whether to apply pending remote D1 migrations.
3374
+ * @param want.seed - Whether to load the configured remote seed (and reset its KV keys).
3375
+ * @returns The migration + seed outcomes and any captured branded errors.
3376
+ * @example
3377
+ * ```ts
3378
+ * const post = await runPostDeploy(ctx, { migration: true, seed: true });
3379
+ * ```
3380
+ */
3381
+ const runPostDeploy = async (ctx, want) => {
3382
+ const ui = (0, _moku_labs_common_cli.createBrandConsole)();
3383
+ const errors = [];
3384
+ let migration = "skipped";
3385
+ if (want.migration) try {
3386
+ await applyRemoteMigrations(ctx);
3387
+ migration = "applied";
3388
+ ui.check(true, "migrated", "remote D1");
3389
+ } catch (error) {
3390
+ migration = "failed";
3391
+ captureFailure(ui, errors, error);
3392
+ }
3393
+ let seed = "skipped";
3394
+ if (want.seed && migration === "failed") {
3395
+ seed = "failed";
3396
+ captureFailure(ui, errors, /* @__PURE__ */ new Error("[moku-worker] seed skipped — the remote migration it depends on failed."));
3397
+ } else if (want.seed) {
3398
+ const config = ctx.config.seed;
3399
+ if (config === void 0) {
3400
+ seed = "failed";
3401
+ captureFailure(ui, errors, /* @__PURE__ */ new Error("[moku-worker] deploy({ seed: true }) but no seed is configured — set pluginConfigs.deploy.seed."));
3402
+ } else try {
3403
+ await runConfiguredSeed(ctx, runWranglerInherit, config, "--remote");
3404
+ seed = "applied";
3405
+ ui.check(true, "seeded", config.file);
3406
+ } catch (error) {
3407
+ seed = "failed";
3408
+ captureFailure(ui, errors, error);
3409
+ }
3410
+ }
3411
+ return {
3412
+ migration,
3413
+ seed,
3414
+ errors
3415
+ };
3416
+ };
3417
+ /**
3207
3418
  * Create the deploy api. Assembles the manifest from each resource plugin's own deployManifest(),
3208
3419
  * runs an infra preflight (check-before-create + id capture), generates config, uploads, and runs
3209
3420
  * `wrangler deploy`, emitting global deploy events along the way.
@@ -3219,13 +3430,19 @@ const guidedDeployStep = async (ctx, manifest, stage, deps) => {
3219
3430
  const createDeployApi = (ctx) => ({
3220
3431
  /**
3221
3432
  * Run the full deploy pipeline: detect → preflight (check-before-create) → provision (only the
3222
- * missing) → wrangler-config (with real ids) → upload → deploy. When opts.manifest is supplied
3223
- * it is used verbatim (universal path).
3433
+ * missing) → wrangler-config (with real ids) → upload → deploy, then ONLY on a successful
3434
+ * deploy the requested post-deploy remote steps (migration, seed). When opts.manifest is
3435
+ * supplied it is used verbatim (universal path).
3224
3436
  *
3225
3437
  * On a TTY the run is GUIDED end to end: each gate is confirmed, and every failure is recovered
3226
- * interactively rather than thrown — a missing/invalid token offers `auth setup`, and the build,
3227
- * infra, upload, and `wrangler deploy` steps offer a retry. In CI/pipes it fails fast (no prompt,
3228
- * the first error propagates to the branded CLI handler).
3438
+ * interactively rather than thrown — a missing/invalid token offers a `.env.local` scaffold, and
3439
+ * the build, infra, upload, and `wrangler deploy` steps offer a retry. In CI/pipes it fails fast
3440
+ * (no prompt, the first error propagates to the branded CLI handler).
3441
+ *
3442
+ * Resolves to a {@link DeployReport}. Every abort path (a declined gate, or auth never set up)
3443
+ * returns `status: "aborted"` BEFORE the post-deploy steps, so `migration`/`seed` run only when
3444
+ * the worker actually went live — a first `deploy --seed` with no token aborts cleanly instead of
3445
+ * falling through to a raw `wrangler … --remote` auth error.
3229
3446
  *
3230
3447
  * @param opts - Optional run options.
3231
3448
  * @param opts.ci - CI/automated mode: never prompts, auto-confirms every gate, fails fast. When
@@ -3234,11 +3451,15 @@ const createDeployApi = (ctx) => ({
3234
3451
  * @param opts.stage - Target stage; suffixes resource names (`production` = bare). Falls back to the app stage.
3235
3452
  * @param opts.webBuild - Build the web site first (e.g. `() => webApp.cli.build()`), before deploy.
3236
3453
  * @param opts.manifest - Caller-supplied manifest (bypasses deployManifest() assembly).
3237
- * @returns Resolves once the deploy completes.
3454
+ * @param opts.migration - After a successful deploy, apply pending remote D1 migrations for every
3455
+ * configured d1 instance that ships migrations. Skipped (not attempted) on an aborted deploy.
3456
+ * @param opts.seed - After a successful deploy (+ migration), load the seed configured under
3457
+ * `pluginConfigs.deploy.seed` into the remote D1 and reset its cached KV keys. Skipped on abort.
3458
+ * @returns The deploy report (status, url, resource tally, migration/seed outcome, errors).
3238
3459
  * @example
3239
3460
  * ```ts
3240
- * await api.run({ webBuild: () => web.cli.build() }); // guided on a TTY
3241
- * await api.run({ ci: true, manifest: { name: "w", compatibilityDate: "2026-06-17", resources: [] } });
3461
+ * const report = await api.run({ webBuild: () => web.cli.build(), migration: true, seed: true });
3462
+ * if (!report.ok) process.exitCode = 1; // aborted or a post-step failed
3242
3463
  * ```
3243
3464
  */
3244
3465
  async run(opts) {
@@ -3251,26 +3472,44 @@ const createDeployApi = (ctx) => ({
3251
3472
  };
3252
3473
  const startedAt = Date.now();
3253
3474
  ctx.emit("deploy:phase", { phase: "auth" });
3254
- if (!await guidedAuth(ctx, deps)) return emitAborted(ctx);
3255
- if (!await guidedWebBuild(ctx, opts?.webBuild ?? ctx.config.webBuild, deps)) return emitAborted(ctx);
3475
+ if (!await guidedAuth(ctx, deps)) return aborted(ctx, stage, startedAt);
3476
+ if (!await guidedWebBuild(ctx, opts?.webBuild ?? ctx.config.webBuild, deps)) return aborted(ctx, stage, startedAt);
3256
3477
  ctx.emit("deploy:phase", { phase: "detect" });
3257
3478
  const manifest = opts?.manifest ?? assembleManifest(ctx, stage);
3258
3479
  ctx.emit("deploy:phase", { phase: "provision" });
3259
3480
  const provisioned = await guidedProvision(ctx, manifest, ci, deps);
3260
- if (provisioned === ABORTED) return emitAborted(ctx);
3481
+ if (provisioned === ABORTED) return aborted(ctx, stage, startedAt);
3261
3482
  ctx.emit("deploy:phase", { phase: "wrangler-config" });
3262
3483
  await writeWranglerConfig(ctx.config.configFile, manifest, provisioned.ids, wranglerExtra(ctx.config));
3263
- if (!await guidedUpload(ctx, manifest, deps)) return emitAborted(ctx);
3484
+ if (!await guidedUpload(ctx, manifest, deps)) return aborted(ctx, stage, startedAt);
3264
3485
  const url = await guidedDeployStep(ctx, manifest, stage, deps);
3265
- if (url === void 0) return emitAborted(ctx);
3486
+ if (url === void 0) return aborted(ctx, stage, startedAt);
3487
+ const resources = {
3488
+ created: provisioned.created.length,
3489
+ exists: provisioned.skipped.length,
3490
+ failed: provisioned.failed.length
3491
+ };
3266
3492
  renderDeploySummary((0, _moku_labs_common_cli.createBrandConsole)(), {
3267
3493
  url,
3268
3494
  stage,
3269
- created: provisioned.created.length,
3270
- exists: provisioned.skipped.length,
3271
- failed: provisioned.failed.length,
3495
+ ...resources,
3272
3496
  elapsedMs: Date.now() - startedAt
3273
3497
  });
3498
+ const post = await runPostDeploy(ctx, {
3499
+ migration: opts?.migration === true,
3500
+ seed: opts?.seed === true
3501
+ });
3502
+ return {
3503
+ ok: post.errors.length === 0,
3504
+ status: post.errors.length === 0 ? "deployed" : "failed",
3505
+ stage,
3506
+ url,
3507
+ resources,
3508
+ migration: post.migration,
3509
+ seed: post.seed,
3510
+ elapsedMs: Date.now() - startedAt,
3511
+ errors: post.errors
3512
+ };
3274
3513
  },
3275
3514
  /**
3276
3515
  * Start a long-lived local dev session: cold-build the Moku site, spawn `wrangler dev
@@ -3283,10 +3522,12 @@ const createDeployApi = (ctx) => ({
3283
3522
  * @param opts.webBuild - Cold-build the web site (e.g. `() => webApp.cli.build()`); also the
3284
3523
  * per-change rebuild when `onChange` is omitted.
3285
3524
  * @param opts.onChange - Incremental per-change rebuild (e.g. `changes => webApp.cli.update(changes)`).
3525
+ * @param opts.seed - Load the configured seed (`pluginConfigs.deploy.seed`) into the LOCAL D1 and
3526
+ * reset its cached KV keys before serving — the local analogue of `deploy({ seed: true })`.
3286
3527
  * @returns Resolves when the dev session ends.
3287
3528
  * @example
3288
3529
  * ```ts
3289
- * await api.dev({ port: 8787, webBuild: () => web.cli.build(), onChange: c => web.cli.update(c) });
3530
+ * await api.dev({ port: 8787, seed: true, webBuild: () => web.cli.build(), onChange: c => web.cli.update(c) });
3290
3531
  * ```
3291
3532
  */
3292
3533
  async dev(opts) {
@@ -3316,11 +3557,7 @@ const createDeployApi = (ctx) => ({
3316
3557
  if (!ctx.has("d1")) throw new Error("[moku-worker] seed: no d1 database is configured.");
3317
3558
  const stage = opts?.stage ?? ctx.global.stage;
3318
3559
  await writeWranglerConfig(ctx.config.configFile, assembleManifest(ctx, stage), {}, wranglerExtra(ctx.config));
3319
- const databases = ctx.require(d1Plugin).deployManifest();
3320
- const wanted = opts?.binding;
3321
- const matched = wanted === void 0 ? databases : databases.filter((database) => database.binding === wanted);
3322
- const target = matched.length === 1 ? matched[0] : void 0;
3323
- if (target === void 0) throw new Error(wanted === void 0 ? `[moku-worker] seed: ${String(databases.length)} d1 databases configured — pass { binding } to choose one.` : `[moku-worker] seed: no d1 database is bound to "${wanted}".`);
3560
+ const target = resolveD1(ctx, opts?.binding);
3324
3561
  const scope = opts?.remote === true ? "--remote" : "--local";
3325
3562
  if (scope === "--local" && target.migrations !== void 0) await runWranglerInherit([
3326
3563
  "d1",
@@ -3543,10 +3780,12 @@ const createCliApi = (ctx) => ({
3543
3780
  * per-change rebuild when `onChange` is omitted.
3544
3781
  * @param opts.onChange - Incremental per-change rebuild (e.g. `changes => webApp.cli.update(changes)`),
3545
3782
  * so each change rebuilds only the changed paths instead of a full `webBuild()`.
3783
+ * @param opts.seed - Load the configured seed (`pluginConfigs.deploy.seed`) into the LOCAL D1 and
3784
+ * reset its cached KV keys before serving — the local analogue of `deploy({ seed: true })`.
3546
3785
  * @returns Resolves when the dev session ends.
3547
3786
  * @example
3548
3787
  * ```ts
3549
- * await api.dev({ port: 7878, webBuild: () => web.cli.build(), onChange: c => web.cli.update(c) });
3788
+ * await api.dev({ port: 7878, seed: true, webBuild: () => web.cli.build(), onChange: c => web.cli.update(c) });
3550
3789
  * ```
3551
3790
  */
3552
3791
  async dev(opts) {
@@ -3561,7 +3800,8 @@ const createCliApi = (ctx) => ({
3561
3800
  ...opts?.port === void 0 ? {} : { port: opts.port },
3562
3801
  ...stage === void 0 ? {} : { stage },
3563
3802
  ...opts?.webBuild ? { webBuild: opts.webBuild } : {},
3564
- ...opts?.onChange ? { onChange: opts.onChange } : {}
3803
+ ...opts?.onChange ? { onChange: opts.onChange } : {},
3804
+ ...opts?.seed ? { seed: opts.seed } : {}
3565
3805
  });
3566
3806
  ui.check(true, "dev session stopped cleanly");
3567
3807
  } catch (error) {
@@ -3570,32 +3810,48 @@ const createCliApi = (ctx) => ({
3570
3810
  }
3571
3811
  },
3572
3812
  /**
3573
- * One-command Cloudflare deploy; forwards opts verbatim to deploy.run. Guided/interactive by
3574
- * default; `{ ci: true }` runs the automated path (CI). A `webBuild` hook builds the web site
3575
- * first (before `wrangler deploy`). A failure renders a branded `✗` line + non-zero exit code
3576
- * (matching cli.auth/doctor), never a raw stack trace.
3813
+ * One-command Cloudflare deploy; forwards opts verbatim to deploy.run, then — only on a successful
3814
+ * deploy the requested post-deploy migration/seed. Guided/interactive by default; `{ ci: true }`
3815
+ * runs the automated path (CI). A `webBuild` hook builds the web site first (before `wrangler
3816
+ * deploy`). RETURNS the structured {@link DeployReport}; on a failure it also renders a branded `✗`
3817
+ * line + sets a non-zero exit code (matching cli.auth/doctor), never a raw stack trace.
3577
3818
  *
3578
3819
  * @param opts - Optional deploy options.
3579
3820
  * @param opts.ci - Automated mode: never prompts, auto-confirms. Omit/false → guided on a TTY.
3580
3821
  * @param opts.stage - Target stage (resource-name suffix); falls back to `--stage` then the app stage.
3581
3822
  * @param opts.webBuild - Build the web site first (e.g. `() => webApp.cli.build()`), before deploy.
3582
- * @returns Resolves once the deploy completes (or after a failure is rendered).
3823
+ * @param opts.migration - Apply pending remote D1 migrations after a successful deploy (skipped on abort).
3824
+ * @param opts.seed - Load the configured remote seed (`pluginConfigs.deploy.seed`) after a
3825
+ * successful deploy (+ migration); skipped on an aborted deploy.
3826
+ * @returns The deploy report (status, url, resource tally, migration/seed outcome, errors).
3583
3827
  * @example
3584
3828
  * ```ts
3585
- * await api.deploy({ webBuild: () => web.cli.build() }); // guided, app stage
3586
- * await api.deploy({ ci: true, webBuild: () => web.cli.build() }); // CI; `--stage dev` honored
3829
+ * const report = await api.deploy({ webBuild: () => web.cli.build(), migration: true, seed: true });
3830
+ * if (report.status === "aborted") return; // creds not set up yet nothing shipped
3587
3831
  * ```
3588
3832
  */
3589
3833
  async deploy(opts) {
3590
3834
  const stage = opts?.stage ?? parseStageArg(process.argv);
3591
3835
  try {
3592
- await ctx.require(deployPlugin).run({
3836
+ const report = await ctx.require(deployPlugin).run({
3593
3837
  ...opts,
3594
3838
  ...stage === void 0 ? {} : { stage }
3595
3839
  });
3840
+ if (report.status === "failed") process.exitCode = 1;
3841
+ return report;
3596
3842
  } catch (error) {
3597
- (0, _moku_labs_common_cli.createBrandConsole)().error(error instanceof Error ? error.message : String(error));
3843
+ const message = error instanceof Error ? error.message : String(error);
3844
+ (0, _moku_labs_common_cli.createBrandConsole)().error(message);
3598
3845
  process.exitCode = 1;
3846
+ return {
3847
+ ok: false,
3848
+ status: "failed",
3849
+ stage: stage ?? "production",
3850
+ migration: "skipped",
3851
+ seed: "skipped",
3852
+ elapsedMs: 0,
3853
+ errors: [message]
3854
+ };
3599
3855
  }
3600
3856
  },
3601
3857
  /**