@lunora/cli 0.0.0 → 1.0.0-alpha.2

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.
Files changed (72) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +109 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/bin.mjs +11 -0
  5. package/dist/index.d.mts +852 -0
  6. package/dist/index.d.ts +852 -0
  7. package/dist/index.mjs +19 -0
  8. package/dist/packem_chunks/handler.mjs +76 -0
  9. package/dist/packem_chunks/handler10.mjs +22 -0
  10. package/dist/packem_chunks/handler11.mjs +192 -0
  11. package/dist/packem_chunks/handler12.mjs +131 -0
  12. package/dist/packem_chunks/handler13.mjs +65 -0
  13. package/dist/packem_chunks/handler14.mjs +58 -0
  14. package/dist/packem_chunks/handler15.mjs +79 -0
  15. package/dist/packem_chunks/handler16.mjs +41 -0
  16. package/dist/packem_chunks/handler17.mjs +105 -0
  17. package/dist/packem_chunks/handler18.mjs +172 -0
  18. package/dist/packem_chunks/handler19.mjs +89 -0
  19. package/dist/packem_chunks/handler2.mjs +114 -0
  20. package/dist/packem_chunks/handler20.mjs +94 -0
  21. package/dist/packem_chunks/handler21.mjs +311 -0
  22. package/dist/packem_chunks/handler3.mjs +204 -0
  23. package/dist/packem_chunks/handler4.mjs +33 -0
  24. package/dist/packem_chunks/handler5.mjs +49 -0
  25. package/dist/packem_chunks/handler6.mjs +91 -0
  26. package/dist/packem_chunks/handler7.mjs +42 -0
  27. package/dist/packem_chunks/handler8.mjs +174 -0
  28. package/dist/packem_chunks/handler9.mjs +16 -0
  29. package/dist/packem_chunks/planDevCommand.mjs +543 -0
  30. package/dist/packem_chunks/runCodegenCommand.mjs +52 -0
  31. package/dist/packem_chunks/runDeployCommand.mjs +504 -0
  32. package/dist/packem_chunks/runInitCommand.mjs +652 -0
  33. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +397 -0
  34. package/dist/packem_chunks/runResetCommand.mjs +41 -0
  35. package/dist/packem_chunks/runRpcCommand.mjs +68 -0
  36. package/dist/packem_shared/COMMANDS-1V_KEx35.mjs +905 -0
  37. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +244 -0
  38. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +19 -0
  39. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +13 -0
  40. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +38 -0
  41. package/dist/packem_shared/command-BDXcJCCJ.mjs +14 -0
  42. package/dist/packem_shared/createLogger-CHPNjFw2.mjs +73 -0
  43. package/dist/packem_shared/defaultSpawner-DxI3mebw.mjs +43 -0
  44. package/dist/packem_shared/diffSnapshots-RR2ZE8Ya.mjs +161 -0
  45. package/dist/packem_shared/docker-hMQ97KSQ.mjs +21 -0
  46. package/dist/packem_shared/features-ocSSpZtS.mjs +24 -0
  47. package/dist/packem_shared/insertSchemaExtension-BuzF6-t2.mjs +59 -0
  48. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +41 -0
  49. package/dist/packem_shared/output-format-7gyGR3h8.mjs +17 -0
  50. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +56 -0
  51. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +94 -0
  52. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +16 -0
  53. package/dist/packem_shared/runAddCommand-BZGkRnBs.mjs +693 -0
  54. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +79 -0
  55. package/dist/packem_shared/schemaIrToSnapshot-aBTo7TM5.mjs +43 -0
  56. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +12 -0
  57. package/package.json +61 -18
  58. package/skills/README.md +29 -0
  59. package/skills/lunora/SKILL.md +83 -0
  60. package/skills/lunora-create-package/SKILL.md +129 -0
  61. package/skills/lunora-deploy/SKILL.md +150 -0
  62. package/skills/lunora-functions/SKILL.md +182 -0
  63. package/skills/lunora-migration-helper/SKILL.md +194 -0
  64. package/skills/lunora-performance-audit/SKILL.md +143 -0
  65. package/skills/lunora-quickstart/SKILL.md +240 -0
  66. package/skills/lunora-realtime/SKILL.md +177 -0
  67. package/skills/lunora-setup-auth/SKILL.md +170 -0
  68. package/skills/lunora-setup-hyperdrive/SKILL.md +154 -0
  69. package/skills/lunora-setup-hyperdrive-global/SKILL.md +171 -0
  70. package/skills/lunora-setup-mail/SKILL.md +151 -0
  71. package/skills/lunora-setup-scheduler/SKILL.md +157 -0
  72. package/skills/lunora-setup-storage/SKILL.md +154 -0
@@ -0,0 +1,504 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { runCodegen, discoverMigrations } from '@lunora/codegen';
3
+ import { readLinkedProject, writeLinkedProject, validateWranglerProject, inferLunoraBindings, reconcileWranglerBindings, DEV_VARS_FILE, parseDevVariableEntries, findWranglerFile, readWranglerJsonc, discoverContainerInfo } from '@lunora/config';
4
+ import { join } from '@visulima/path';
5
+ import { Spinner } from '@visulima/spinner';
6
+ import { Project } from 'ts-morph';
7
+ import { p as parseApiSpec } from '../packem_shared/api-spec-CtA6ilu4.mjs';
8
+ import { r as readWranglerName } from '../packem_shared/wrangler-name-cy4yhm9j.mjs';
9
+ import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
10
+ import { a as isRailpackAvailable, i as isDockerAvailable } from '../packem_shared/docker-hMQ97KSQ.mjs';
11
+ import { v as validateOutputFormat, i as isJsonFormat, p as printJson, l as loggerForFormat } from '../packem_shared/output-format-7gyGR3h8.mjs';
12
+ import { containerBuildTag } from '@lunora/container';
13
+ import { defaultSpawner } from '../packem_shared/defaultSpawner-DxI3mebw.mjs';
14
+ import { r as resolveWorkerUrl } from '../packem_shared/resolve-target-qbsJ_5sF.mjs';
15
+ import { r as runSchemaDriftGate } from '../packem_shared/schema-drift-gate-BtBt0as0.mjs';
16
+ import { runMigrateDataCommand } from './runMigrateGenerateCommand.mjs';
17
+
18
+ const WORKERS_DEV_URL = /https?:\/\/[^\s"'<>]+\.workers\.dev[^\s"'<>]*/u;
19
+ const ANY_HTTPS_URL = /https:\/\/[^\s"'<>]+/u;
20
+ const parseDeployedUrl = (output) => {
21
+ const workersDev = WORKERS_DEV_URL.exec(output);
22
+ if (workersDev) {
23
+ return workersDev[0];
24
+ }
25
+ return ANY_HTTPS_URL.exec(output)?.[0];
26
+ };
27
+ const autoLinkFromDeployOutput = ({ cwd, env, logger, now, output }) => {
28
+ if (output === void 0 || readLinkedProject(cwd) !== void 0) {
29
+ return;
30
+ }
31
+ const url = parseDeployedUrl(output);
32
+ if (url === void 0) {
33
+ return;
34
+ }
35
+ try {
36
+ const stamp = (now ?? (() => (/* @__PURE__ */ new Date()).toISOString()))();
37
+ writeLinkedProject(cwd, { env, linkedAt: stamp, workerName: readWranglerName(cwd), workerUrl: url });
38
+ logger.success(`link: recorded ${url} in .lunora/project.json (run \`lunora link\` to change)`);
39
+ } catch {
40
+ }
41
+ };
42
+
43
+ const renderDeploySummary = (inputs) => {
44
+ const { cwd, env, logger, migrated } = inputs;
45
+ try {
46
+ const link = readLinkedProject(cwd);
47
+ const workerName = link?.workerName ?? readWranglerName(cwd);
48
+ logger.success("deploy complete");
49
+ logger.info(` worker: ${workerName ?? "(see wrangler output above)"}`);
50
+ if (env !== void 0) {
51
+ logger.info(` env: ${env}`);
52
+ }
53
+ if (link?.workerUrl === void 0) {
54
+ logger.info(" url: run `lunora link --url <https://your-worker>` to record it");
55
+ } else {
56
+ logger.info(` url: ${link.workerUrl}`);
57
+ }
58
+ if (migrated) {
59
+ logger.info(" migrations: applied");
60
+ }
61
+ logger.info(" studio: lunora view --remote");
62
+ logger.info(" logs: lunora logs");
63
+ } catch {
64
+ }
65
+ };
66
+
67
+ const buildRailpackImages = async (options) => {
68
+ if (options.targets.length === 0) {
69
+ return { builtTags: [], code: 0 };
70
+ }
71
+ if (!(options.railpackAvailable ?? isRailpackAvailable)()) {
72
+ const message = "deploy blocked: a container uses `image: { build }` (Railpack), but Railpack isn't ready. Install the `railpack` CLI and start a BuildKit instance, e.g. `docker run --rm --privileged -d --name buildkit moby/buildkit` then `export BUILDKIT_HOST=docker-container://buildkit`. Alternatively switch the container's `image` to a Dockerfile path or a pre-built registry reference.";
73
+ options.logger.error(message);
74
+ return { builtTags: [], code: 1, error: message };
75
+ }
76
+ const spawner = options.spawner ?? defaultSpawner;
77
+ const builtTags = [];
78
+ for (const target of options.targets) {
79
+ const tag = containerBuildTag(target.exportName);
80
+ const build = { args: ["build", target.buildDir, "--name", tag], command: "railpack", cwd: options.cwd };
81
+ const push = { args: ["exec", "wrangler", "containers", "push", tag], command: "pnpm", cwd: options.cwd };
82
+ options.logger.info(`railpack: building "${target.exportName}" → ${tag} from ${target.buildDir}`);
83
+ const buildResult = await spawner(build);
84
+ if (buildResult.code !== 0) {
85
+ const message = `railpack build failed for container "${target.exportName}" (${target.buildDir})`;
86
+ options.logger.error(message);
87
+ return { builtTags, code: buildResult.code, error: message };
88
+ }
89
+ options.logger.info(`railpack: pushing ${tag} to the Cloudflare Registry`);
90
+ const pushResult = await spawner(push);
91
+ if (pushResult.code !== 0) {
92
+ const message = `wrangler containers push failed for "${target.exportName}" (${tag})`;
93
+ options.logger.error(message);
94
+ return { builtTags, code: pushResult.code, error: message };
95
+ }
96
+ builtTags.push(tag);
97
+ }
98
+ return { builtTags, code: 0 };
99
+ };
100
+
101
+ const D1_PLACEHOLDER_ID = "<replace-with-d1-create-id>";
102
+ const ORIGIN_VAR_NAMES = ["LUNORA_ORIGIN_URL", "LUNORA_WORKER_ORIGIN", "AUTH_URL"];
103
+ const isLocalhostUrl = (value) => {
104
+ try {
105
+ const { hostname } = new URL(value);
106
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1" || hostname === "[::1]";
107
+ } catch {
108
+ return false;
109
+ }
110
+ };
111
+ const isLocalImagePath = (image) => image.startsWith("./") || image.startsWith("../") || image.startsWith("/") || image.includes("Dockerfile");
112
+ const checkContainerDockerPreflight = (cwd, logger, dockerAvailable) => {
113
+ const wranglerPath = findWranglerFile(cwd);
114
+ if (!wranglerPath) {
115
+ return void 0;
116
+ }
117
+ const { parsed } = readWranglerJsonc(wranglerPath);
118
+ const localImages = (parsed?.containers ?? []).filter((entry) => typeof entry?.image === "string" && isLocalImagePath(entry.image));
119
+ if (localImages.length === 0 || dockerAvailable()) {
120
+ return void 0;
121
+ }
122
+ const message = `deploy blocked: wrangler.jsonc declares ${String(localImages.length)} container(s) built from a local Dockerfile, but no Docker-compatible engine is available. Start Docker (or Colima), or point the container's \`image\` at a pre-built registry reference. Note: container images must target linux/amd64.`;
123
+ logger.error(message);
124
+ return message;
125
+ };
126
+ const resolveComposedWorkerEntry = (cwd) => existsSync(join(cwd, "src", "worker.ts")) ? "src/worker.ts" : void 0;
127
+ const checkContainerSourcesExist = (cwd, logger) => {
128
+ for (const container of discoverContainerInfo(cwd, "lunora").containers) {
129
+ const { image } = container;
130
+ if (image.kind === "dockerfile" && !existsSync(join(cwd, image.dockerfilePath))) {
131
+ const message = `deploy blocked: container "${container.exportName}" references a Dockerfile at "${image.dockerfilePath}" that does not exist. Create it or fix the \`image\` path in lunora/containers.ts.`;
132
+ logger.error(message);
133
+ return message;
134
+ }
135
+ if (image.kind === "build" && !existsSync(join(cwd, image.buildDir))) {
136
+ const message = `deploy blocked: container "${container.exportName}" references a Railpack build directory "${image.buildDir}" that does not exist. Create it or fix the \`image.build\` path in lunora/containers.ts.`;
137
+ logger.error(message);
138
+ return message;
139
+ }
140
+ }
141
+ return void 0;
142
+ };
143
+ const isInteractive = (options) => {
144
+ if (isJsonFormat(options.format)) {
145
+ return false;
146
+ }
147
+ if (options.interactive !== void 0) {
148
+ return options.interactive;
149
+ }
150
+ return process.stdout.isTTY && !process.env.CI;
151
+ };
152
+ const findD1PlaceholderBinding = (cwd) => {
153
+ const wranglerPath = findWranglerFile(cwd);
154
+ if (!wranglerPath) {
155
+ return void 0;
156
+ }
157
+ const { parsed } = readWranglerJsonc(wranglerPath);
158
+ if (!parsed) {
159
+ return void 0;
160
+ }
161
+ const placeholder = (parsed.d1_databases ?? []).find((entry) => entry.database_id === D1_PLACEHOLDER_ID);
162
+ return placeholder?.binding;
163
+ };
164
+ const buildContainerImages = async (cwd, options) => {
165
+ const targets = discoverContainerInfo(cwd, "lunora").containers.filter((container) => container.image.kind === "build").map((container) => {
166
+ return { buildDir: container.image.buildDir, exportName: container.exportName };
167
+ });
168
+ if (targets.length === 0) {
169
+ return void 0;
170
+ }
171
+ const result = await buildRailpackImages({
172
+ cwd,
173
+ logger: options.logger,
174
+ railpackAvailable: options.railpackAvailable,
175
+ spawner: options.spawner,
176
+ targets
177
+ });
178
+ return result.code === 0 ? void 0 : result.error ?? "railpack build failed";
179
+ };
180
+ const provisionBindings = async (cwd, logger) => {
181
+ try {
182
+ const inferred = await inferLunoraBindings({ projectRoot: cwd });
183
+ const reconciled = reconcileWranglerBindings(cwd, inferred);
184
+ if (reconciled.changed) {
185
+ logger.success(`provisioned bindings: ${reconciled.added.join(", ")} → ${reconciled.wranglerPath ?? "wrangler.jsonc"}`);
186
+ }
187
+ for (const warning of reconciled.warnings) {
188
+ logger.warn(warning);
189
+ }
190
+ } catch (error) {
191
+ const message = error instanceof Error ? error.message : String(error);
192
+ logger.warn(`binding inference skipped: ${message}`);
193
+ }
194
+ };
195
+ const warnDevVariablesNotPushed = (cwd, logger) => {
196
+ const devVariablesPath = join(cwd, DEV_VARS_FILE);
197
+ if (!existsSync(devVariablesPath)) {
198
+ return;
199
+ }
200
+ let keyCount;
201
+ try {
202
+ keyCount = parseDevVariableEntries(readFileSync(devVariablesPath, "utf8")).length;
203
+ } catch {
204
+ return;
205
+ }
206
+ if (keyCount === 0) {
207
+ return;
208
+ }
209
+ logger.warn(
210
+ `Note: \`lunora deploy\` does not push secrets. ${DEV_VARS_FILE} has ${String(keyCount)} key(s); if you changed them, run \`lunora env push --yes\` to update the deployed secrets.`
211
+ );
212
+ };
213
+ const runPostDeployMigrations = async (options, cwd) => {
214
+ const project = new Project({ skipAddingFilesFromTsConfig: true });
215
+ const lunoraDirectory = join(cwd, "lunora");
216
+ let migrations;
217
+ try {
218
+ migrations = discoverMigrations(project, lunoraDirectory);
219
+ } catch (error) {
220
+ const message = error instanceof Error ? error.message : String(error);
221
+ options.logger.warn(`--migrate: could not discover migrations (${message}); skipping`);
222
+ return 0;
223
+ }
224
+ if (migrations.length === 0) {
225
+ options.logger.info("--migrate: no data migrations declared in lunora/");
226
+ return 0;
227
+ }
228
+ options.logger.info(`--migrate: running ${String(migrations.length)} migration(s) against deployed worker`);
229
+ for (const migration of migrations) {
230
+ options.logger.info(`--migrate: up "${migration.id}" (table "${migration.table}")`);
231
+ const migrateOptions = {
232
+ cwd,
233
+ fetchImpl: options.fetchImpl,
234
+ id: migration.id,
235
+ logger: options.logger,
236
+ // A `--migrate-url` is always set by this point (guarded above), so this
237
+ // is a production migration — gate it behind the operator's explicit
238
+ // `--migrate-yes`/`--yes` rather than auto-confirming.
239
+ prod: true,
240
+ subcommand: "up",
241
+ token: options.migrateToken,
242
+ url: options.migrateUrl,
243
+ yes: options.migrateYes === true
244
+ };
245
+ const migrateResult = await runMigrateDataCommand(migrateOptions);
246
+ if (migrateResult.code !== 0) {
247
+ options.logger.error(`--migrate: migration "${migration.id}" failed — see output above`);
248
+ return migrateResult.code;
249
+ }
250
+ options.logger.success(`--migrate: "${migration.id}" applied`);
251
+ }
252
+ return 0;
253
+ };
254
+ const validateMigrateDeployPreflight = (options) => {
255
+ if (!options.migrate || options.dryRun || options.preview) {
256
+ return void 0;
257
+ }
258
+ if (options.migrateUrl === void 0) {
259
+ const message = "--migrate requires --migrate-url <https://your-worker> — the deploy target URL is not captured automatically, refusing to default to localhost";
260
+ options.logger.error(message);
261
+ return message;
262
+ }
263
+ if (options.migrateYes !== true) {
264
+ const message = "--migrate runs production data migrations after deploy. Re-run with --migrate-yes to confirm.";
265
+ options.logger.error(message);
266
+ return message;
267
+ }
268
+ if ((options.migrateToken ?? process.env.LUNORA_ADMIN_TOKEN) === void 0 || (options.migrateToken ?? process.env.LUNORA_ADMIN_TOKEN) === "") {
269
+ const message = "admin token required for --migrate — pass --migrate-token or set LUNORA_ADMIN_TOKEN";
270
+ options.logger.error(message);
271
+ return message;
272
+ }
273
+ return void 0;
274
+ };
275
+ const runCodegenStep = (cwd, interactive, logger, apiSpec) => {
276
+ let codegenSpinner;
277
+ if (interactive) {
278
+ codegenSpinner = new Spinner({ name: "dots" });
279
+ codegenSpinner.start("running codegen");
280
+ } else {
281
+ logger.info("running codegen");
282
+ }
283
+ try {
284
+ const result = runCodegen({ apiSpec, projectRoot: cwd });
285
+ codegenSpinner?.succeed("codegen complete");
286
+ if (!codegenSpinner) {
287
+ logger.success("codegen complete");
288
+ }
289
+ return { result };
290
+ } catch (error) {
291
+ codegenSpinner?.failed("codegen failed");
292
+ const message = error instanceof Error ? error.message : String(error);
293
+ logger.error(`codegen failed: ${message}`);
294
+ return { error: `codegen failed: ${message}` };
295
+ }
296
+ };
297
+ const checkD1Placeholder = (cwd, logger) => {
298
+ const placeholderBinding = findD1PlaceholderBinding(cwd);
299
+ if (placeholderBinding === void 0) {
300
+ return void 0;
301
+ }
302
+ const message = `deploy blocked: the "${placeholderBinding}" D1 binding has a placeholder database_id ("${D1_PLACEHOLDER_ID}"). Run \`wrangler d1 create <name>\` to create the database, then replace the placeholder in wrangler.jsonc with the real id before deploying.`;
303
+ logger.error(message);
304
+ return message;
305
+ };
306
+ const checkLocalhostOriginVariables = (cwd, logger) => {
307
+ const wranglerPath = findWranglerFile(cwd);
308
+ if (!wranglerPath) {
309
+ return void 0;
310
+ }
311
+ const { parsed } = readWranglerJsonc(wranglerPath);
312
+ const variables = parsed?.vars;
313
+ if (!variables) {
314
+ return void 0;
315
+ }
316
+ const offenders = ORIGIN_VAR_NAMES.filter((name) => typeof variables[name] === "string" && isLocalhostUrl(variables[name]));
317
+ if (offenders.length === 0) {
318
+ return void 0;
319
+ }
320
+ const message = `deploy blocked: ${offenders.join(", ")} in wrangler.jsonc point at localhost. A deployed Worker can't reach a loopback address, so this silently breaks scheduled-job dispatch / auth callbacks. Set each to the deployed worker's public URL (or move it to a secret with \`wrangler secret put\`) before deploying.`;
321
+ logger.error(message);
322
+ return message;
323
+ };
324
+ const finalizeSuccessfulDeploy = async (options, cwd, descriptor, validation, reblessSchemaBaseline) => {
325
+ if (options.migrate) {
326
+ const migrateCode = await runPostDeployMigrations(options, cwd);
327
+ if (migrateCode === 0) {
328
+ reblessSchemaBaseline?.();
329
+ }
330
+ return { code: migrateCode, descriptor, validation };
331
+ }
332
+ reblessSchemaBaseline?.();
333
+ return { code: 0, descriptor, validation };
334
+ };
335
+ const runPreDeployGates = async (cwd, options) => {
336
+ const d1Error = checkD1Placeholder(cwd, options.logger);
337
+ if (d1Error !== void 0) {
338
+ return d1Error;
339
+ }
340
+ const localhostOriginError = checkLocalhostOriginVariables(cwd, options.logger);
341
+ if (localhostOriginError !== void 0) {
342
+ return localhostOriginError;
343
+ }
344
+ const sourceError = checkContainerSourcesExist(cwd, options.logger);
345
+ if (sourceError !== void 0) {
346
+ return sourceError;
347
+ }
348
+ const dockerError = checkContainerDockerPreflight(cwd, options.logger, options.dockerAvailable ?? isDockerAvailable);
349
+ if (dockerError !== void 0) {
350
+ return dockerError;
351
+ }
352
+ return buildContainerImages(cwd, options);
353
+ };
354
+ const buildWranglerDeployArgs = (cwd, options) => {
355
+ const args = options.preview ? ["exec", "wrangler", "versions", "upload"] : ["exec", "wrangler", "deploy"];
356
+ const composedEntry = resolveComposedWorkerEntry(cwd);
357
+ if (composedEntry !== void 0) {
358
+ args.push(composedEntry);
359
+ options.logger.info(`class-B composition: deploying ${composedEntry} (overrides wrangler main)`);
360
+ }
361
+ if (options.env !== void 0) {
362
+ args.push("--env", options.env);
363
+ }
364
+ if (options.temporary) {
365
+ args.push("--temporary");
366
+ options.logger.info("temporary account: deploying to a short-lived Cloudflare account (~60min); wrangler will print a claim URL");
367
+ }
368
+ if (options.dryRun) {
369
+ args.push("--dry-run");
370
+ options.logger.info("dry run: validating + bundling without publishing");
371
+ }
372
+ if (options.outDir !== void 0) {
373
+ args.push("--outdir", options.outDir, "--metafile");
374
+ options.logger.info(`build artifact: emitting bundle to ${options.outDir}`);
375
+ }
376
+ return args;
377
+ };
378
+ const executeDeploy = async (options) => {
379
+ const cwd = options.cwd ?? process.cwd();
380
+ const interactive = isInteractive(options);
381
+ let codegen;
382
+ if (!options.skipCodegen) {
383
+ const codegenStep = runCodegenStep(cwd, interactive, options.logger, options.apiSpec);
384
+ if (codegenStep.error !== void 0) {
385
+ return {
386
+ code: 1,
387
+ descriptor: void 0,
388
+ error: codegenStep.error,
389
+ validation: { problems: [], wranglerPath: void 0 }
390
+ };
391
+ }
392
+ codegen = codegenStep.result;
393
+ }
394
+ let reblessSchemaBaseline;
395
+ if (codegen !== void 0) {
396
+ const gate = runSchemaDriftGate({
397
+ allowDrift: options.allowSchemaDrift === true,
398
+ codegen,
399
+ logger: options.logger,
400
+ updateBaseline: options.updateSchemaBaseline === true
401
+ });
402
+ if (gate.blocked) {
403
+ return {
404
+ code: 1,
405
+ descriptor: void 0,
406
+ error: "schema drift gate blocked deploy",
407
+ schemaDrift: { blocked: true, reason: gate.reason },
408
+ validation: { problems: [], wranglerPath: void 0 }
409
+ };
410
+ }
411
+ reblessSchemaBaseline = gate.rebless;
412
+ }
413
+ await provisionBindings(cwd, options.logger);
414
+ const migratePreflightError = validateMigrateDeployPreflight(options);
415
+ if (migratePreflightError !== void 0) {
416
+ return { code: 1, descriptor: void 0, error: migratePreflightError, validation: { problems: [], wranglerPath: void 0 } };
417
+ }
418
+ const preflightError = await runPreDeployGates(cwd, options);
419
+ if (preflightError !== void 0) {
420
+ return { code: 1, descriptor: void 0, error: preflightError, validation: { problems: [], wranglerPath: void 0 } };
421
+ }
422
+ const validation = validateWranglerProject({ projectRoot: cwd });
423
+ if (validation.problems.length > 0) {
424
+ options.logger.error("wrangler.jsonc validation failed:");
425
+ for (const problem of validation.problems) {
426
+ options.logger.error(` - ${problem}`);
427
+ }
428
+ return {
429
+ code: 1,
430
+ descriptor: void 0,
431
+ error: "wrangler validation failed",
432
+ validation
433
+ };
434
+ }
435
+ warnDevVariablesNotPushed(cwd, options.logger);
436
+ const shouldAutoLink = !isJsonFormat(options.format) && options.dryRun !== true && options.preview !== true && readLinkedProject(cwd) === void 0;
437
+ const descriptor = {
438
+ args: buildWranglerDeployArgs(cwd, options),
439
+ captureStdout: shouldAutoLink,
440
+ command: "pnpm",
441
+ cwd,
442
+ // In `--format json` mode stdout is reserved for the single JSON document,
443
+ // so route wrangler's progress + deployed-URL output to stderr instead.
444
+ stdoutToStderr: isJsonFormat(options.format)
445
+ };
446
+ options.logger.info(`deploying via ${descriptor.command} ${descriptor.args.join(" ")}`);
447
+ const spawner = options.spawner ?? defaultSpawner;
448
+ const result = await spawner(descriptor);
449
+ if (result.code !== 0) {
450
+ return { code: result.code, descriptor, validation };
451
+ }
452
+ if (options.dryRun) {
453
+ return { code: 0, descriptor, validation };
454
+ }
455
+ if (options.preview) {
456
+ return { code: 0, descriptor, validation };
457
+ }
458
+ autoLinkFromDeployOutput({ cwd, env: options.env, logger: options.logger, output: result.stdout });
459
+ return finalizeSuccessfulDeploy(options, cwd, descriptor, validation, reblessSchemaBaseline);
460
+ };
461
+ const runDeployCommand = async (options) => {
462
+ const formatError = validateOutputFormat("deploy", options.format);
463
+ if (formatError !== void 0) {
464
+ options.logger.error(formatError);
465
+ return { code: 1, descriptor: void 0, error: formatError, validation: { problems: [], wranglerPath: void 0 } };
466
+ }
467
+ const result = await executeDeploy({ ...options, logger: loggerForFormat(options.format, options.logger) });
468
+ if (isJsonFormat(options.format)) {
469
+ printJson(result);
470
+ return result;
471
+ }
472
+ if (result.code === 0 && options.dryRun !== true && options.preview !== true) {
473
+ renderDeploySummary({ cwd: options.cwd ?? process.cwd(), env: options.env, logger: options.logger, migrated: options.migrate === true });
474
+ } else if (result.code === 0 && options.preview === true) {
475
+ options.logger.success("preview version uploaded — see the preview URL in the wrangler output above");
476
+ }
477
+ return result;
478
+ };
479
+ const execute = defineHandler(async ({ cwd, logger, options }) => {
480
+ const result = await runDeployCommand({
481
+ allowSchemaDrift: options.allowSchemaDrift === true,
482
+ apiSpec: parseApiSpec(options.apiSpec),
483
+ cwd,
484
+ dryRun: options.dryRun === true,
485
+ env: options.env,
486
+ format: options.format,
487
+ logger,
488
+ migrate: options.migrate === true,
489
+ migrateToken: options.migrateToken,
490
+ // Fall back to the `.lunora/project.json` link so a linked checkout no
491
+ // longer needs --migrate-url repeated on every `deploy --migrate`.
492
+ migrateUrl: resolveWorkerUrl({ cwd, url: options.migrateUrl }),
493
+ migrateYes: options.migrateYes === true,
494
+ preview: options.preview === true,
495
+ // `--prebuilt` trusts a prior `lunora build`/`prepare`: skip codegen (and
496
+ // thus the drift gate, which has no fresh snapshot to measure).
497
+ skipCodegen: options.prebuilt === true,
498
+ temporary: options.temporary === true,
499
+ updateSchemaBaseline: options.updateSchemaBaseline === true
500
+ });
501
+ return { code: result.code };
502
+ });
503
+
504
+ export { execute, runDeployCommand };