@lunora/cli 1.0.0-alpha.1 → 1.0.0-alpha.11

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 (47) hide show
  1. package/dist/bin.mjs +1 -1
  2. package/dist/index.d.mts +217 -113
  3. package/dist/index.d.ts +217 -113
  4. package/dist/index.mjs +6 -6
  5. package/dist/packem_chunks/handler.mjs +80 -6
  6. package/dist/packem_chunks/handler10.mjs +1 -1
  7. package/dist/packem_chunks/handler11.mjs +1 -1
  8. package/dist/packem_chunks/handler12.mjs +1 -1
  9. package/dist/packem_chunks/handler13.mjs +1 -1
  10. package/dist/packem_chunks/handler14.mjs +2 -2
  11. package/dist/packem_chunks/handler15.mjs +1 -1
  12. package/dist/packem_chunks/handler16.mjs +5 -3
  13. package/dist/packem_chunks/handler17.mjs +1 -1
  14. package/dist/packem_chunks/handler18.mjs +4 -6
  15. package/dist/packem_chunks/handler19.mjs +3 -3
  16. package/dist/packem_chunks/handler2.mjs +2 -2
  17. package/dist/packem_chunks/handler20.mjs +1 -1
  18. package/dist/packem_chunks/handler21.mjs +2 -2
  19. package/dist/packem_chunks/handler3.mjs +1 -1
  20. package/dist/packem_chunks/handler4.mjs +1 -1
  21. package/dist/packem_chunks/handler5.mjs +2 -2
  22. package/dist/packem_chunks/handler6.mjs +2 -2
  23. package/dist/packem_chunks/handler7.mjs +1 -1
  24. package/dist/packem_chunks/handler8.mjs +1 -1
  25. package/dist/packem_chunks/handler9.mjs +1 -1
  26. package/dist/packem_chunks/planDevCommand.mjs +6 -49
  27. package/dist/packem_chunks/runCodegenCommand.mjs +2 -2
  28. package/dist/packem_chunks/runDeployCommand.mjs +3 -3
  29. package/dist/packem_chunks/runInitCommand.mjs +954 -104
  30. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +5 -5
  31. package/dist/packem_chunks/runResetCommand.mjs +4 -4
  32. package/dist/packem_chunks/runRpcCommand.mjs +1 -1
  33. package/dist/packem_shared/{COMMANDS-1V_KEx35.mjs → COMMANDS-D3h9Iwvl.mjs} +45 -6
  34. package/dist/packem_shared/{command-BDXcJCCJ.mjs → command-BC30oSBW.mjs} +1 -1
  35. package/dist/packem_shared/{runAddCommand-BZGkRnBs.mjs → commands-zs-0Es9F.mjs} +143 -24
  36. package/dist/packem_shared/{createLogger-CHPNjFw2.mjs → createLogger-B40gPzQo.mjs} +9 -4
  37. package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +61 -0
  38. package/dist/packem_shared/{diffSnapshots-RR2ZE8Ya.mjs → diffSnapshots-BeDvvNiF.mjs} +1 -1
  39. package/dist/packem_shared/{output-format-7gyGR3h8.mjs → output-format-wUvAN6AL.mjs} +1 -1
  40. package/dist/packem_shared/runAddCommand-DWlzKNPC.mjs +4 -0
  41. package/dist/packem_shared/{schemaIrToSnapshot-aBTo7TM5.mjs → schemaIrToSnapshot-DdsljJT-.mjs} +1 -1
  42. package/dist/packem_shared/storage-7faZve2Z.mjs +84 -0
  43. package/dist/packem_shared/tui-prompts-Dvlh5xWt.mjs +663 -0
  44. package/package.json +11 -10
  45. package/skills/lunora-setup-storage/SKILL.md +7 -3
  46. package/dist/packem_shared/features-ocSSpZtS.mjs +0 -24
  47. /package/dist/packem_shared/{defaultSpawner-DxI3mebw.mjs → createRecordingSpawner-DxI3mebw.mjs} +0 -0
@@ -1,16 +1,21 @@
1
- import { existsSync, mkdirSync, writeFileSync, readdirSync, mkdtempSync, rmSync, readFileSync } from 'node:fs';
1
+ import { existsSync, mkdirSync, writeFileSync, readFileSync, readdirSync, rmSync, mkdtempSync, cpSync, renameSync } from 'node:fs';
2
2
  import { tmpdir } from 'node:os';
3
- import { fileURLToPath } from 'node:url';
4
- import { detectFramework as detectFramework$1, isInteractive, promptSelect, promptMultiSelect } from '@lunora/config';
3
+ import { detectFramework as detectFramework$1, isInteractive, LUNA_ART, LUNA_NAME, LUNA_SIGNOFF, paintAnswer, BADGES } from '@lunora/config';
5
4
  import { walkSync } from '@visulima/fs';
6
- import { resolve, join as join$1, relative, dirname as dirname$1 } from '@visulima/path';
5
+ import { join as join$1, dirname as dirname$1, basename, resolve, relative } from '@visulima/path';
7
6
  import { downloadTemplate } from 'giget';
7
+ import { modify, applyEdits } from 'jsonc-parser';
8
8
  import { join, dirname } from 'node:path';
9
- import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
9
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
10
+ import { a as detectInstalledManagers, i as installArgsFor } from '../packem_shared/detect-package-manager-DYp7n3mJ.mjs';
10
11
  import MagicString from 'magic-string';
11
12
  import { Project, SyntaxKind } from 'ts-morph';
12
- import { p as promptAuthProvider, E as EMAIL_ITEM } from '../packem_shared/features-ocSSpZtS.mjs';
13
- import { runAddCommand } from '../packem_shared/runAddCommand-BZGkRnBs.mjs';
13
+ import { c as resolveSourceRef, d as resolveDistTag, r as runAddCommand } from '../packem_shared/commands-zs-0Es9F.mjs';
14
+ import { defaultSpawner } from '../packem_shared/createRecordingSpawner-DxI3mebw.mjs';
15
+ import { d as tuiMascot, e as tuiStep, P as PromptCancelledError, f as tuiMoonrise, t as tuiText, g as tuiHeadline, h as tuiInfo, a as tuiSelect, w as withTuiSpinner, b as tuiConfirm, i as tuiNextSteps, j as tuiTasks, k as tuiMultiSelect, l as withTuiBadgeProgress } from '../packem_shared/tui-prompts-Dvlh5xWt.mjs';
16
+ import { logStep } from '../packem_shared/createLogger-B40gPzQo.mjs';
17
+ import { E as EMAIL_ITEM, p as promptBucketName, w as withStorageBucketName, M as MAIL_DESTINATION_PROMPT, r as resolveTypedDestination, f as withMailDestination, e as promptAuthProvider, c as promptDatabaseName, g as withAuthDatabaseName } from '../packem_shared/storage-7faZve2Z.mjs';
18
+ import dns from 'node:dns/promises';
14
19
 
15
20
  const GITHUB_CONTENT = `name: Deploy
16
21
 
@@ -20,6 +25,10 @@ on:
20
25
  pull_request:
21
26
  workflow_dispatch:
22
27
 
28
+ # Prerequisite: commit your pnpm-lock.yaml. \`pnpm install --frozen-lockfile\`
29
+ # (below) and the pnpm cache both require it — run \`pnpm install\` locally and
30
+ # commit the lockfile before pushing, or the first CI run fails.
31
+ #
23
32
  # Set these repository secrets (Settings → Secrets and variables → Actions):
24
33
  # CLOUDFLARE_API_TOKEN — a Workers-scoped API token
25
34
  # CLOUDFLARE_ACCOUNT_ID — your Cloudflare account id
@@ -66,6 +75,10 @@ jobs:
66
75
  const GITLAB_CONTENT = `stages:
67
76
  - deploy
68
77
 
78
+ # Prerequisite: commit your pnpm-lock.yaml. \`pnpm install --frozen-lockfile\`
79
+ # (below) requires it — run \`pnpm install\` locally and commit the lockfile
80
+ # before pushing, or the first pipeline fails.
81
+ #
69
82
  # Set these as masked CI/CD variables (Settings → CI/CD → Variables):
70
83
  # CLOUDFLARE_API_TOKEN — a Workers-scoped API token
71
84
  # CLOUDFLARE_ACCOUNT_ID — your Cloudflare account id
@@ -127,6 +140,9 @@ const scaffoldCiWorkflow = (projectRoot, provider, logger, options = {}) => {
127
140
  } else {
128
141
  logger.success(`--ci ${provider}: wrote ${spec.file}`);
129
142
  logger.info(`--ci ${provider}: set CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID as ${spec.secretsHint} to enable deploys.`);
143
+ logger.info(
144
+ `--ci ${provider}: run \`pnpm install\` and commit pnpm-lock.yaml before pushing — the pipeline runs \`pnpm install --frozen-lockfile\`.`
145
+ );
130
146
  }
131
147
  return result;
132
148
  } catch (error) {
@@ -271,26 +287,468 @@ const patchViteConfig = (source) => {
271
287
  return { changed: true, code: ms.toString() };
272
288
  };
273
289
 
290
+ const emitStep = async (type, message, answer) => {
291
+ if (isInteractive()) {
292
+ await tuiStep(BADGES[type], message, answer);
293
+ return;
294
+ }
295
+ process.stdout.write("\n");
296
+ if (answer === void 0 || answer === "") {
297
+ logStep(type, message);
298
+ return;
299
+ }
300
+ const dimmed = answer.split("\n").map((line) => paintAnswer(line)).join("\n");
301
+ logStep(type, `${message}
302
+ ${dimmed}`);
303
+ };
304
+ const emitMascot = async (logger) => {
305
+ if (isInteractive()) {
306
+ await tuiMascot();
307
+ return;
308
+ }
309
+ logger.info(`
310
+ ${LUNA_ART}
311
+ ${LUNA_NAME}: ${LUNA_SIGNOFF}`);
312
+ };
313
+
274
314
  const STACK_FEATURE_OPTIONS = [
275
315
  { description: "Sign-up / sign-in (asks which provider)", label: "Authentication", value: "auth" },
276
- { description: "Cloudflare Email Workers + a dev mail catcher", label: "Transactional email", value: "email" }
316
+ { description: "Cloudflare Email Workers + a dev mail catcher", label: "Transactional email", value: "email" },
317
+ { description: "Typed R2 buckets + signed URLs (@lunora/storage)", label: "File storage", value: "storage" },
318
+ { description: "Token-bucket / sliding-window limits (@lunora/ratelimit)", label: "Rate limiting", value: "ratelimit" },
319
+ { description: "Scheduled jobs via Cron Triggers (@lunora/scheduler)", label: "Cron jobs", value: "crons" },
320
+ { description: "Live presence / who's-online over hibernated WebSockets", label: "Presence", value: "presence" },
321
+ { description: "Snapshot + restore your Durable Object data", label: "Backups", value: "backup" }
277
322
  ];
323
+ const STACK_FEATURE_VALUES = STACK_FEATURE_OPTIONS.map((option) => option.value);
324
+ const featureItem = (feature) => feature === "email" ? EMAIL_ITEM : feature;
325
+ const parseFeatureList = (raw, warn) => {
326
+ const features = [];
327
+ for (const part of raw.split(",").map((entry) => entry.trim()).filter(Boolean)) {
328
+ if (STACK_FEATURE_VALUES.includes(part)) {
329
+ if (!features.includes(part)) {
330
+ features.push(part);
331
+ }
332
+ } else {
333
+ warn(`init: unknown --add feature "${part}" — expected ${STACK_FEATURE_VALUES.join(" | ")}; skipping.`);
334
+ }
335
+ }
336
+ return features;
337
+ };
338
+ const collectAuthFeature = async (deps) => {
339
+ const provider = await promptAuthProvider(deps.select);
340
+ const databaseName = await promptDatabaseName(deps.text, deps.projectName);
341
+ return { label: "auth", names: [provider], transformManifest: (manifest) => withAuthDatabaseName(manifest, databaseName) };
342
+ };
343
+ const collectEmailFeature = async (deps) => {
344
+ const answer = await deps.text(MAIL_DESTINATION_PROMPT, { placeholder: "you@yourdomain.com" });
345
+ const destination = resolveTypedDestination(answer, (message) => {
346
+ deps.logger.warn(message);
347
+ });
348
+ return {
349
+ label: "email",
350
+ names: [EMAIL_ITEM],
351
+ transformManifest: destination === void 0 ? void 0 : (manifest) => withMailDestination(manifest, destination)
352
+ };
353
+ };
354
+ const collectStorageFeature = async (deps) => {
355
+ const bucketName = await promptBucketName(deps.text, deps.projectName);
356
+ return { label: "storage", names: ["storage"], transformManifest: (manifest) => withStorageBucketName(manifest, bucketName) };
357
+ };
358
+ const FEATURE_COLLECTORS = {
359
+ auth: collectAuthFeature,
360
+ email: collectEmailFeature,
361
+ storage: collectStorageFeature
362
+ };
278
363
  const offerRegistryExtras = async (deps) => {
364
+ if (deps.preselected !== void 0 && deps.preselected.length > 0) {
365
+ await deps.applyAll(
366
+ deps.preselected.map((feature) => {
367
+ return { label: feature, names: [featureItem(feature)] };
368
+ })
369
+ );
370
+ return;
371
+ }
279
372
  if (!deps.interactive) {
280
- deps.logger.info("tip: add authentication or email later with `lunora add auth` / `lunora add email`.");
373
+ deps.logger.info("tip: add features later with `lunora add <auth|email|storage|ratelimit|crons|presence|backup>`.");
281
374
  return;
282
375
  }
283
376
  const picked = await deps.multiSelect("Which features do you want to add?", STACK_FEATURE_OPTIONS, { defaults: [] });
377
+ if (picked.length === 0) {
378
+ return;
379
+ }
380
+ const plans = [];
284
381
  for (const feature of picked) {
285
- if (feature === "auth") {
286
- const provider = await promptAuthProvider(deps.select);
287
- await deps.apply([provider]);
288
- } else {
289
- await deps.apply([EMAIL_ITEM]);
382
+ const collect = FEATURE_COLLECTORS[feature];
383
+ plans.push(collect ? await collect(deps) : { label: feature, names: [feature] });
384
+ }
385
+ await deps.applyAll(plans);
386
+ };
387
+
388
+ const READ_URL = `const url = (import.meta.env.VITE_LUNORA_URL as string | undefined) ?? globalThis.location.origin;`;
389
+ const REACT_MAIN = `import "./index.css";
390
+
391
+ import { LunoraProvider } from "@lunora/react";
392
+ import { LunoraClient } from "lunorash/client";
393
+ import { StrictMode } from "react";
394
+ import { createRoot } from "react-dom/client";
395
+
396
+ import App from "./App.tsx";
397
+
398
+ // \`@lunora/vite\` runs the Worker on the same origin as Vite, so default to
399
+ // \`location.origin\`. Point \`VITE_LUNORA_URL\` at a deployed Worker to develop
400
+ // the client against production data.
401
+ ${READ_URL}
402
+ const client = new LunoraClient({ url });
403
+
404
+ const root = document.getElementById("root");
405
+
406
+ if (!root) {
407
+ throw new Error("missing #root mount node");
408
+ }
409
+
410
+ createRoot(root).render(
411
+ <StrictMode>
412
+ <LunoraProvider client={client}>
413
+ <App />
414
+ </LunoraProvider>
415
+ </StrictMode>,
416
+ );
417
+ `;
418
+ const VUE_MAIN = `import "./style.css";
419
+
420
+ import { createLunora } from "@lunora/vue";
421
+ import { LunoraClient } from "lunorash/client";
422
+ import { createApp } from "vue";
423
+
424
+ import App from "./App.vue";
425
+
426
+ // Provide one LunoraClient at the app root via the Vue plugin form.
427
+ ${READ_URL}
428
+ createApp(App).use(createLunora(new LunoraClient({ url }))).mount("#app");
429
+ `;
430
+ const SOLID_INDEX = `import "./index.css";
431
+
432
+ import { LunoraContext } from "@lunora/solid";
433
+ import { LunoraClient } from "lunorash/client";
434
+ import { render } from "solid-js/web";
435
+
436
+ import App from "./App";
437
+
438
+ ${READ_URL}
439
+ const client = new LunoraClient({ url });
440
+ const root = document.getElementById("root");
441
+
442
+ render(
443
+ () => (
444
+ <LunoraContext.Provider value={client}>
445
+ <App />
446
+ </LunoraContext.Provider>
447
+ ),
448
+ root!,
449
+ );
450
+ `;
451
+ const SVELTE_ROOT = `<script lang="ts">
452
+ import { setLunoraClient } from "@lunora/svelte";
453
+ import { LunoraClient } from "lunorash/client";
454
+
455
+ import App from "./App.svelte";
456
+
457
+ ${READ_URL}
458
+ setLunoraClient(new LunoraClient({ url }));
459
+ <\/script>
460
+
461
+ <App />
462
+ `;
463
+ const SVELTE_MAIN = `import "./app.css";
464
+
465
+ import { mount } from "svelte";
466
+
467
+ import Root from "./Root.svelte";
468
+
469
+ // Mount \`Root\` (it sets the ambient LunoraClient) rather than \`App\` directly.
470
+ const app = mount(Root, { target: document.getElementById("app")! });
471
+
472
+ export default app;
473
+ `;
474
+ const VANILLA_MAIN = `import "./style.css";
475
+
476
+ import { LunoraClient } from "lunorash/client";
477
+
478
+ import { api } from "../lunora/_generated/api";
479
+
480
+ // Vanilla starter: no framework provider — talk to Lunora through the client
481
+ // directly. \`@lunora/vite\` runs the Worker on the same origin as Vite.
482
+ ${READ_URL}
483
+ const client = new LunoraClient({ url });
484
+
485
+ const root = document.querySelector<HTMLDivElement>("#app")!;
486
+
487
+ const heading = document.createElement("h1");
488
+ heading.textContent = "Vite + Lunora";
489
+
490
+ const output = document.createElement("pre");
491
+ root.replaceChildren(heading, output);
492
+
493
+ const render = (messages: unknown): void => {
494
+ // textContent (not innerHTML) — never inject server data as markup.
495
+ output.textContent = JSON.stringify(messages, null, 2);
496
+ };
497
+
498
+ // Live subscription: the list re-renders on every server delta.
499
+ client.onUpdate(api.messages.list, { channelId: "channel:demo" }, render);
500
+ `;
501
+ const ADAPTERS = {
502
+ react: { adapter: "@lunora/react", createViteTemplate: "react-ts", files: [{ contents: REACT_MAIN, path: "src/main.tsx" }], label: "React" },
503
+ solid: { adapter: "@lunora/solid", createViteTemplate: "solid", files: [{ contents: SOLID_INDEX, path: "src/index.tsx" }], label: "Solid" },
504
+ svelte: {
505
+ adapter: "@lunora/svelte",
506
+ createViteTemplate: "svelte-ts",
507
+ files: [
508
+ { contents: SVELTE_ROOT, path: "src/Root.svelte" },
509
+ { contents: SVELTE_MAIN, path: "src/main.ts" }
510
+ ],
511
+ label: "Svelte"
512
+ },
513
+ vanilla: { adapter: "lunorash/client", createViteTemplate: "vanilla-ts", files: [{ contents: VANILLA_MAIN, path: "src/main.ts" }], label: "Vanilla" },
514
+ vue: { adapter: "@lunora/vue", createViteTemplate: "vue-ts", files: [{ contents: VUE_MAIN, path: "src/main.ts" }], label: "Vue" }
515
+ };
516
+ const isOverlayFramework = (value) => Object.hasOwn(ADAPTERS, value);
517
+
518
+ const LUNORA_SCHEMA = `import { defineSchema, defineTable, v } from "lunorash/server";
519
+
520
+ export default defineSchema({
521
+ messages: defineTable({
522
+ channelId: v.string(),
523
+ text: v.string(),
524
+ })
525
+ .shardBy("channelId")
526
+ .index("by_channel", ["channelId"]),
527
+ });
528
+ `;
529
+ const LUNORA_MESSAGES = `import { mutation, query, v } from "./_generated/server.js";
530
+
531
+ export const list = query.input({ channelId: v.string(), limit: v.optional(v.number()) }).query(async ({ args }) => {
532
+ return { channelId: args.channelId, limit: args.limit ?? 50, messages: [] };
533
+ });
534
+
535
+ export const send = mutation.input({ channelId: v.string(), text: v.string() }).mutation(async ({ args }) => {
536
+ return { channelId: args.channelId, text: args.text };
537
+ });
538
+ `;
539
+ const SERVER_ENTRY = `import type { ShardNamespaceLike } from "lunorash/runtime";
540
+
541
+ import { defineApp } from "../lunora/_generated/app.js";
542
+
543
+ interface Env extends Record<string, unknown> {
544
+ SHARD: ShardNamespaceLike;
545
+ }
546
+
547
+ const app = defineApp<Env>()
548
+ .shard((env) => env.SHARD)
549
+ .build();
550
+
551
+ export const ShardDO = app.ShardDO;
552
+ export default app;
553
+ `;
554
+ const WRANGLER = `{
555
+ "$schema": "node_modules/wrangler/config-schema.json",
556
+ "name": "__NAME__",
557
+ "main": "src/server.ts",
558
+ "compatibility_date": "2026-06-10",
559
+ "compatibility_flags": ["nodejs_compat"],
560
+ "durable_objects": {
561
+ "bindings": [{ "name": "SHARD", "class_name": "ShardDO" }],
562
+ },
563
+ "migrations": [{ "tag": "v1", "new_sqlite_classes": ["ShardDO"] }],
564
+ "observability": { "enabled": true, "head_sampling_rate": 1 },
565
+ }
566
+ `;
567
+ const GITIGNORE_ADDITIONS = [".wrangler", ".env", ".env.*", "!.env.example", ".lunora/", ".lunora-cache", "lunora/_generated"];
568
+ const ENV_EXAMPLE = `# Lunora endpoint for the browser client.
569
+ # Vite statically replaces \`import.meta.env.VITE_LUNORA_URL\` at \`vite dev\` / build.
570
+ # Leave it unset to use the page origin; set it to point at a deployed Worker:
571
+ #
572
+ # VITE_LUNORA_URL=https://my-app.example.workers.dev
573
+ `;
574
+ const COMMON_DEV_DEPENDENCIES = {
575
+ "@cloudflare/workers-types": "^4.20260611.1",
576
+ wrangler: "^4.100.0"
577
+ };
578
+ const writeFile = (target, relativePath, contents, written) => {
579
+ const destination = join$1(target, relativePath);
580
+ mkdirSync(dirname$1(destination), { recursive: true });
581
+ writeFileSync(destination, contents, "utf8");
582
+ written.push(destination);
583
+ };
584
+ const NEWLINE = /\r?\n/;
585
+ const ensureGitignore = (target) => {
586
+ const path = join$1(target, ".gitignore");
587
+ const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
588
+ const missing = GITIGNORE_ADDITIONS.filter((entry) => !existing.split(NEWLINE).includes(entry));
589
+ if (missing.length === 0) {
590
+ return;
591
+ }
592
+ const prefix = existing.length > 0 && !existing.endsWith("\n") ? "\n" : "";
593
+ writeFileSync(path, `${existing}${prefix}
594
+ # Lunora
595
+ ${missing.join("\n")}
596
+ `, "utf8");
597
+ };
598
+ const stampRange = (name, range, distTag) => name === "lunorash" || name.startsWith("@lunora/") ? distTag : range;
599
+ const withDependency = (map, name, range, distTag) => {
600
+ return { ...map, [name]: stampRange(name, range, distTag) };
601
+ };
602
+ const restampLunora = (map, distTag) => Object.fromEntries(Object.entries(map).map(([name, range]) => [name, stampRange(name, range, distTag)]));
603
+ const patchPackageJson = (target, name, adapter, distTag) => {
604
+ const path = join$1(target, "package.json");
605
+ const parsed = JSON.parse(readFileSync(path, "utf8"));
606
+ let dependencies = withDependency(parsed.dependencies ?? {}, "lunorash", distTag, distTag);
607
+ if (adapter.adapter.startsWith("@lunora/")) {
608
+ dependencies = withDependency(dependencies, adapter.adapter, distTag, distTag);
609
+ }
610
+ for (const [depName, range] of Object.entries(adapter.extraDependencies ?? {})) {
611
+ dependencies = withDependency(dependencies, depName, range, distTag);
612
+ }
613
+ let devDependencies = withDependency(parsed.devDependencies ?? {}, "@lunora/vite", distTag, distTag);
614
+ for (const [depName, range] of Object.entries(COMMON_DEV_DEPENDENCIES)) {
615
+ devDependencies = withDependency(devDependencies, depName, range, distTag);
616
+ }
617
+ parsed.name = name;
618
+ parsed.dependencies = restampLunora(dependencies, distTag);
619
+ parsed.devDependencies = restampLunora(devDependencies, distTag);
620
+ parsed.scripts = { ...parsed.scripts, codegen: "lunora codegen", deploy: "vite build && lunora deploy" };
621
+ writeFileSync(path, `${JSON.stringify(parsed, void 0, 4)}
622
+ `, "utf8");
623
+ };
624
+ const patchBaseViteConfig = (target, logger) => {
625
+ const candidate = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"].map((file) => join$1(target, file)).find((path) => existsSync(path));
626
+ if (candidate === void 0) {
627
+ logger.warn("overlay: no vite.config found in the create-vite base — add `lunora()` to your Vite plugins manually.");
628
+ return;
629
+ }
630
+ const result = patchViteConfig(readFileSync(candidate, "utf8"));
631
+ if (result.changed) {
632
+ writeFileSync(candidate, result.code, "utf8");
633
+ }
634
+ };
635
+ const applyLunoraOverlay = (options) => {
636
+ const { adapter, distTag, logger, name, target } = options;
637
+ const written = [];
638
+ writeFile(target, join$1("lunora", "schema.ts"), LUNORA_SCHEMA, written);
639
+ writeFile(target, join$1("lunora", "messages.ts"), LUNORA_MESSAGES, written);
640
+ writeFile(target, join$1("src", "server.ts"), SERVER_ENTRY, written);
641
+ writeFile(target, "wrangler.jsonc", WRANGLER.replaceAll("__NAME__", name), written);
642
+ writeFile(target, ".env.example", ENV_EXAMPLE, written);
643
+ for (const file of adapter.files) {
644
+ writeFile(target, file.path, file.contents, written);
645
+ }
646
+ patchBaseViteConfig(target, logger);
647
+ patchPackageJson(target, name, adapter, distTag);
648
+ ensureGitignore(target);
649
+ return written;
650
+ };
651
+
652
+ const ADJECTIVES = [
653
+ "lunar",
654
+ "silver",
655
+ "silent",
656
+ "waning",
657
+ "waxing",
658
+ "crescent",
659
+ "cosmic",
660
+ "stellar",
661
+ "orbital",
662
+ "gibbous",
663
+ "twilight",
664
+ "midnight",
665
+ "shimmering",
666
+ "drifting",
667
+ "weightless"
668
+ ];
669
+ const NOUNS = [
670
+ "moon",
671
+ "tide",
672
+ "crater",
673
+ "comet",
674
+ "eclipse",
675
+ "halo",
676
+ "orbit",
677
+ "nebula",
678
+ "voyager",
679
+ "lander",
680
+ "rover",
681
+ "beacon",
682
+ "harbor",
683
+ "meadow",
684
+ "fox"
685
+ ];
686
+ const pick = (items) => (
687
+ // eslint-disable-next-line sonarjs/pseudo-random -- cosmetic default project name, not a security decision.
688
+ items[Math.floor(Math.random() * items.length)]
689
+ );
690
+ const generateProjectName = () => `${pick(ADJECTIVES)}-${pick(NOUNS)}`;
691
+
692
+ const isOnline = async () => dns.lookup("github.com").then(
693
+ () => true,
694
+ () => false
695
+ );
696
+ const GITHUB_SOURCE = /^(?:gh|github):([^/]+)\/([^#/]+)(?:\/[^#]*)?(?:#(.+))?$/;
697
+ const parseGitHubSource = (source) => {
698
+ const match = GITHUB_SOURCE.exec(source);
699
+ if (match === null) {
700
+ return void 0;
701
+ }
702
+ const [, owner, repo, ref] = match;
703
+ if (owner === void 0 || repo === void 0) {
704
+ return void 0;
705
+ }
706
+ return { owner, ref: ref ?? "HEAD", repo };
707
+ };
708
+ const templateRefExists = async (source) => {
709
+ const parsed = parseGitHubSource(source);
710
+ if (parsed === void 0) {
711
+ return void 0;
712
+ }
713
+ const url = `https://codeload.github.com/${parsed.owner}/${parsed.repo}/tar.gz/${parsed.ref}`;
714
+ try {
715
+ const response = await fetch(url, { method: "HEAD" });
716
+ if (response.status === 404) {
717
+ return false;
290
718
  }
719
+ return response.ok ? true : void 0;
720
+ } catch {
721
+ return void 0;
291
722
  }
292
723
  };
724
+ const verifyRemoteTemplate = async (params) => {
725
+ if (params.isLocal) {
726
+ return true;
727
+ }
728
+ const isGitHubBacked = params.source === void 0 || parseGitHubSource(params.source) !== void 0;
729
+ if (!isGitHubBacked) {
730
+ return true;
731
+ }
732
+ if (!await isOnline()) {
733
+ params.logger.error("you appear to be offline — connect to the internet and try again, or scaffold from a local template with `--from <dir>`.");
734
+ return false;
735
+ }
736
+ if (params.source !== void 0 && await templateRefExists(params.source) === false) {
737
+ params.logger.error(`template source not found: ${params.source} — double-check --ref / --source, or browse the templates at https://lunora.sh/docs.`);
738
+ return false;
739
+ }
740
+ return true;
741
+ };
293
742
 
743
+ const COPY = {
744
+ extras: "Let's finish setting up your app.",
745
+ framework: "Which framework should we launch?",
746
+ git: "Initialize a new git repository? (optional)",
747
+ install: "Install dependencies now?",
748
+ name: "Where should we land your project?",
749
+ nextHeader: "Liftoff confirmed — explore your project!",
750
+ packageManager: "Which package manager?"
751
+ };
294
752
  const TEXT_EXTENSIONS = /* @__PURE__ */ new Set([".gitignore", ".html", ".js", ".json", ".jsonc", ".md", ".mjs", ".ts", ".tsx"]);
295
753
  const VITE_CONFIG_CANDIDATES = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"];
296
754
  const MINIMAL_VITE_CONFIG = `import { defineConfig } from "vite";
@@ -324,32 +782,6 @@ export const send = mutation
324
782
  });
325
783
  `;
326
784
  const DEFAULT_SOURCE_BASE = "gh:anolilab/lunora/templates";
327
- const DEFAULT_SOURCE_REF_FALLBACK = "alpha";
328
- const resolveCliVersion = () => {
329
- try {
330
- let directory = dirname$1(fileURLToPath(import.meta.url));
331
- for (let index = 0; index < 5; index += 1) {
332
- const candidate = join$1(directory, "package.json");
333
- if (existsSync(candidate)) {
334
- const parsed = JSON.parse(readFileSync(candidate, "utf8"));
335
- if (parsed.name === "@lunora/cli" && typeof parsed.version === "string") {
336
- return parsed.version;
337
- }
338
- }
339
- const parent = dirname$1(directory);
340
- if (parent === directory) {
341
- break;
342
- }
343
- directory = parent;
344
- }
345
- } catch {
346
- }
347
- return "0.0.0";
348
- };
349
- const resolveDefaultSourceRef = () => {
350
- const version = resolveCliVersion();
351
- return version === "0.0.0" ? DEFAULT_SOURCE_REF_FALLBACK : `v${version}`;
352
- };
353
785
  const isTextFile = (filePath) => {
354
786
  const lastDot = filePath.lastIndexOf(".");
355
787
  if (lastDot === -1) {
@@ -358,6 +790,26 @@ const isTextFile = (filePath) => {
358
790
  return TEXT_EXTENSIONS.has(filePath.slice(lastDot));
359
791
  };
360
792
  const substitute = (content, name) => content.replaceAll("{{name}}", name);
793
+ const isLunoraDep = (name) => name === "lunorash" || name.startsWith("@lunora/");
794
+ const stampLunoraDeps = (packageJsonText, distTag) => {
795
+ let parsed;
796
+ try {
797
+ parsed = JSON.parse(packageJsonText);
798
+ } catch {
799
+ return packageJsonText;
800
+ }
801
+ let text = packageJsonText;
802
+ for (const section of ["dependencies", "devDependencies"]) {
803
+ for (const name of Object.keys(parsed[section] ?? {})) {
804
+ if (!isLunoraDep(name)) {
805
+ continue;
806
+ }
807
+ const edits = modify(text, [section, name], distTag, { formattingOptions: { insertSpaces: true, tabSize: 4 } });
808
+ text = applyEdits(text, edits);
809
+ }
810
+ }
811
+ return text;
812
+ };
361
813
  const collectFiles = (directory) => {
362
814
  const out = [];
363
815
  for (const entry of walkSync(directory, { includeDirs: false, includeFiles: true })) {
@@ -368,12 +820,16 @@ const collectFiles = (directory) => {
368
820
  const copyTemplate = (sourceDirectory, target, name) => {
369
821
  const files = collectFiles(sourceDirectory);
370
822
  const written = [];
823
+ const distTag = resolveDistTag();
371
824
  for (const source of files) {
372
825
  const relativePath = relative(sourceDirectory, source);
373
826
  const destination = join$1(target, relativePath);
374
827
  mkdirSync(dirname$1(destination), { recursive: true });
375
828
  const raw = readFileSync(source);
376
- const text = isTextFile(source) ? substitute(raw.toString("utf8"), name) : void 0;
829
+ let text = isTextFile(source) ? substitute(raw.toString("utf8"), name) : void 0;
830
+ if (text !== void 0 && basename(source) === "package.json") {
831
+ text = stampLunoraDeps(text, distTag);
832
+ }
377
833
  if (text === void 0) {
378
834
  writeFileSync(destination, raw);
379
835
  } else {
@@ -383,11 +839,11 @@ const copyTemplate = (sourceDirectory, target, name) => {
383
839
  }
384
840
  return written;
385
841
  };
386
- const resolveTemplateSource = (templateType, source) => {
842
+ const resolveTemplateSource = (templateType, source, ref) => {
387
843
  if (source !== void 0 && source.length > 0) {
388
844
  return source;
389
845
  }
390
- return `${DEFAULT_SOURCE_BASE}/${templateType}#${resolveDefaultSourceRef()}`;
846
+ return `${DEFAULT_SOURCE_BASE}/${templateType}#${resolveSourceRef(ref)}`;
391
847
  };
392
848
  const isSafeSource = (source) => {
393
849
  if (source.includes("..")) {
@@ -395,12 +851,143 @@ const isSafeSource = (source) => {
395
851
  }
396
852
  return source.startsWith("gh:") || source.startsWith("github:") || source.startsWith("https://");
397
853
  };
398
- const logScaffoldSuccess = (logger, written, target, name) => {
854
+ const logWould = (logger, action) => {
855
+ logger.info(`[dry-run] would ${action}`);
856
+ };
857
+ const logScaffoldSuccess = (logger, written, target) => {
858
+ if (isInteractive()) {
859
+ process.stdout.write("\n");
860
+ }
399
861
  logger.success(`scaffolded ${String(written.length)} files into ${target}`);
400
- logger.info("next steps:");
401
- logger.info(` cd ${name}`);
402
- logger.info(" pnpm install");
403
- logger.info(" pnpm dev");
862
+ };
863
+ const runScriptCommand = (manager, script) => {
864
+ if (manager === "npm") {
865
+ return `npm run ${script}`;
866
+ }
867
+ if (manager === "bun") {
868
+ return `bun run ${script}`;
869
+ }
870
+ return `${manager} ${script}`;
871
+ };
872
+ const isInsideMonorepo = (startDirectory) => {
873
+ let directory = resolve(startDirectory);
874
+ for (; ; ) {
875
+ if (existsSync(join$1(directory, "pnpm-workspace.yaml"))) {
876
+ return true;
877
+ }
878
+ const packagePath = join$1(directory, "package.json");
879
+ if (existsSync(packagePath)) {
880
+ try {
881
+ const parsed = JSON.parse(readFileSync(packagePath, "utf8"));
882
+ if (parsed.workspaces !== void 0) {
883
+ return true;
884
+ }
885
+ } catch {
886
+ }
887
+ }
888
+ const parent = dirname$1(directory);
889
+ if (parent === directory) {
890
+ return false;
891
+ }
892
+ directory = parent;
893
+ }
894
+ };
895
+ const isInsideGitRepo = (startDirectory) => {
896
+ let directory = resolve(startDirectory);
897
+ for (; ; ) {
898
+ if (existsSync(join$1(directory, ".git"))) {
899
+ return true;
900
+ }
901
+ const parent = dirname$1(directory);
902
+ if (parent === directory) {
903
+ return false;
904
+ }
905
+ directory = parent;
906
+ }
907
+ };
908
+ const maybeOfferGit = async (options, target) => {
909
+ if (options.yes === true || !isInteractive() || isInsideGitRepo(dirname$1(target))) {
910
+ return;
911
+ }
912
+ if (!await tuiConfirm(COPY.git, { badge: BADGES.git, defaultYes: false })) {
913
+ await tuiInfo("Sounds good! You can always run git init manually.");
914
+ return;
915
+ }
916
+ if (options.dryRun === true) {
917
+ logWould(options.logger, "initialize a git repository");
918
+ return;
919
+ }
920
+ const spawner = options.spawner ?? defaultSpawner;
921
+ const result = await withTuiSpinner("Initializing a git repository…", () => spawner({ args: ["init"], command: "git", cwd: target }));
922
+ if (result.code === 0) {
923
+ await emitStep("git", "Initialized an empty git repository.");
924
+ } else {
925
+ options.logger.warn("`git init` failed — initialize it yourself later with `git init`.");
926
+ }
927
+ };
928
+ const printNextSteps = async (name, installed, insideMonorepo) => {
929
+ const manager = installed ?? "pnpm";
930
+ const steps = [{ code: `cd ./${name}`, lead: "Enter your project directory using" }];
931
+ if (installed === void 0) {
932
+ steps.push({ code: `${manager} install`, lead: "Install dependencies with", tail: insideMonorepo ? " from the workspace root" : void 0 });
933
+ }
934
+ steps.push(
935
+ { code: runScriptCommand(manager, "dev"), lead: "Run", tail: " to start the dev server." },
936
+ { code: "lunora add", lead: "Add features like auth or storage using" }
937
+ );
938
+ const help = [
939
+ { code: "https://lunora.sh/docs", lead: "Read the docs at" },
940
+ { code: "https://lunora.sh/chat", lead: "Stuck? Join the chat at" }
941
+ ];
942
+ if (isInteractive()) {
943
+ await tuiNextSteps(BADGES.next, COPY.nextHeader, steps, help);
944
+ return;
945
+ }
946
+ const lines = steps.map((step) => `${step.lead} ${step.code}${step.tail ?? ""}`);
947
+ lines.push("", ...help.map((line) => `${line.lead} ${line.code}${line.tail ?? ""}`));
948
+ await emitStep("next", COPY.nextHeader, lines.join("\n"));
949
+ };
950
+ const offerInstallIsInteractive = (options) => options.yes !== true && (options.installPrompt !== void 0 || isInteractive());
951
+ const maybeOfferInstall = async (options, target) => {
952
+ if (!offerInstallIsInteractive(options)) {
953
+ return void 0;
954
+ }
955
+ if (isInsideMonorepo(dirname$1(target))) {
956
+ return void 0;
957
+ }
958
+ const managers = detectInstalledManagers(options.packageManagerProbe);
959
+ const [defaultManager] = managers;
960
+ if (defaultManager === void 0) {
961
+ return void 0;
962
+ }
963
+ const confirm = options.installPrompt?.confirmInstall ?? (async () => tuiConfirm(COPY.install, { badge: BADGES.deps, defaultYes: true }));
964
+ if (!await confirm()) {
965
+ await tuiInfo("No problem! Remember to install dependencies after setup.");
966
+ return void 0;
967
+ }
968
+ let manager = defaultManager;
969
+ if (managers.length > 1) {
970
+ manager = options.installPrompt ? await options.installPrompt.selectManager(managers) : await tuiSelect(
971
+ COPY.packageManager,
972
+ managers.map((candidate) => {
973
+ return { label: candidate, value: candidate };
974
+ }),
975
+ { badge: BADGES.deps, default: defaultManager }
976
+ ) ?? defaultManager;
977
+ }
978
+ if (options.dryRun === true) {
979
+ logWould(options.logger, `install dependencies with ${manager}`);
980
+ return void 0;
981
+ }
982
+ const spawner = options.spawner ?? defaultSpawner;
983
+ const { args, command } = installArgsFor(manager);
984
+ const result = await withTuiSpinner(`Installing dependencies with ${manager}…`, () => spawner({ args, command, cwd: target }));
985
+ if (result.code !== 0) {
986
+ options.logger.warn(`\`${command} install\` exited with code ${String(result.code)} — run it yourself in ${basename(target)}/.`);
987
+ return void 0;
988
+ }
989
+ await emitStep("deps", `Dependencies installed with ${manager}.`);
990
+ return manager;
404
991
  };
405
992
  const scaffoldFromLocal = (fromRoot, templateType, target, name, logger) => {
406
993
  const templateDirectory = join$1(fromRoot, templateType);
@@ -409,32 +996,54 @@ const scaffoldFromLocal = (fromRoot, templateType, target, name, logger) => {
409
996
  return { code: 1, files: [], target };
410
997
  }
411
998
  const written = copyTemplate(templateDirectory, target, name);
412
- logScaffoldSuccess(logger, written, target, name);
999
+ logScaffoldSuccess(logger, written, target);
413
1000
  return { code: 0, files: written, target };
414
1001
  };
415
- const scaffoldFromRemote = async (source, templateType, target, name, logger) => {
1002
+ const scaffoldFromRemote = async (options) => {
1003
+ const { logger, name, ref, source, target, templateType } = options;
416
1004
  const stagingRoot = mkdtempSync(join$1(tmpdir(), "lunora-init-fetch-"));
417
1005
  const stagingDirectory = join$1(stagingRoot, "template");
418
1006
  try {
419
- const remote = resolveTemplateSource(templateType, source);
420
- logger.info(`fetching template from ${remote}`);
421
- const downloaded = await downloadTemplate(remote, {
422
- cwd: stagingRoot,
423
- dir: stagingDirectory,
424
- force: true,
425
- install: false,
426
- silent: true
427
- });
1007
+ const remote = resolveTemplateSource(templateType, source, ref);
1008
+ let downloaded;
1009
+ let written = [];
1010
+ await tuiTasks(
1011
+ [
1012
+ {
1013
+ label: `${templateType} template fetched`,
1014
+ run: async () => {
1015
+ downloaded = await downloadTemplate(remote, {
1016
+ cwd: stagingRoot,
1017
+ dir: stagingDirectory,
1018
+ force: true,
1019
+ install: false,
1020
+ silent: true
1021
+ });
1022
+ }
1023
+ },
1024
+ {
1025
+ label: `files copied into ${name}/`,
1026
+ run: () => {
1027
+ written = copyTemplate(stagingDirectory, target, name);
1028
+ return Promise.resolve();
1029
+ }
1030
+ }
1031
+ ],
1032
+ { end: "Project initialized!", start: "Project initializing…" }
1033
+ );
428
1034
  const staged = collectFiles(stagingDirectory);
429
- if (downloaded.commit) {
430
- logger.info(`resolved ${downloaded.source} @ ${downloaded.commit} (${String(staged.length)} file(s))`);
431
- } else {
432
- logger.info(`resolved ${downloaded.source} (${String(staged.length)} file(s))`);
1035
+ if (isInteractive()) {
1036
+ process.stdout.write("\n");
433
1037
  }
434
- const written = copyTemplate(stagingDirectory, target, name);
435
- logScaffoldSuccess(logger, written, target, name);
1038
+ logger.info(
1039
+ downloaded?.commit ? `template: ${downloaded.source} @ ${downloaded.commit} (${String(staged.length)} files)` : `template: ${downloaded?.source ?? remote} (${String(staged.length)} files)`
1040
+ );
1041
+ logScaffoldSuccess(logger, written, target);
436
1042
  return { code: 0, files: written, target };
437
1043
  } catch (error) {
1044
+ if (error instanceof PromptCancelledError) {
1045
+ throw error;
1046
+ }
438
1047
  const message = error instanceof Error ? error.message : String(error);
439
1048
  logger.error(`failed to download template: ${message}`);
440
1049
  return { code: 1, files: [], target };
@@ -442,6 +1051,65 @@ const scaffoldFromRemote = async (source, templateType, target, name, logger) =>
442
1051
  rmSync(stagingRoot, { force: true, recursive: true });
443
1052
  }
444
1053
  };
1054
+ const renameCreateViteDotfiles = (directory) => {
1055
+ for (const file of ["_gitignore", "_npmrc", "_gitattributes"]) {
1056
+ const from = join$1(directory, file);
1057
+ if (existsSync(from)) {
1058
+ renameSync(from, join$1(directory, `.${file.slice(1)}`));
1059
+ }
1060
+ }
1061
+ };
1062
+ const scaffoldViteOverlay = async (options) => {
1063
+ const { framework, logger, name, overlayBaseFrom, target } = options;
1064
+ const adapter = ADAPTERS[framework];
1065
+ const stagingRoot = mkdtempSync(join$1(tmpdir(), "lunora-vite-base-"));
1066
+ try {
1067
+ let localBase;
1068
+ if (overlayBaseFrom !== void 0) {
1069
+ localBase = join$1(overlayBaseFrom, `template-${adapter.createViteTemplate}`);
1070
+ if (!existsSync(localBase)) {
1071
+ logger.error(`create-vite base not found on disk: ${localBase}`);
1072
+ return { code: 1, files: [], target };
1073
+ }
1074
+ }
1075
+ const copyBase = async () => {
1076
+ if (localBase !== void 0) {
1077
+ cpSync(localBase, target, { recursive: true });
1078
+ return;
1079
+ }
1080
+ const stagingDirectory = join$1(stagingRoot, "base");
1081
+ const remote = `github:vitejs/vite/packages/create-vite/template-${adapter.createViteTemplate}#main`;
1082
+ await downloadTemplate(remote, { cwd: stagingRoot, dir: stagingDirectory, force: true, install: false, silent: true });
1083
+ renameCreateViteDotfiles(stagingDirectory);
1084
+ cpSync(stagingDirectory, target, { recursive: true });
1085
+ };
1086
+ let written = [];
1087
+ await tuiTasks(
1088
+ [
1089
+ { label: `create-vite (${adapter.label}) base ready`, run: copyBase },
1090
+ {
1091
+ label: `Lunora overlay applied (${adapter.label})`,
1092
+ run: () => {
1093
+ written = applyLunoraOverlay({ adapter, distTag: resolveDistTag(), logger, name, target });
1094
+ return Promise.resolve();
1095
+ }
1096
+ }
1097
+ ],
1098
+ { end: "Project initialized!", start: "Project initializing…" }
1099
+ );
1100
+ logScaffoldSuccess(logger, written, target);
1101
+ return { code: 0, files: [...collectFiles(target)], target };
1102
+ } catch (error) {
1103
+ if (error instanceof PromptCancelledError) {
1104
+ throw error;
1105
+ }
1106
+ const message = error instanceof Error ? error.message : String(error);
1107
+ logger.error(`failed to scaffold the ${adapter.label} base: ${message}`);
1108
+ return { code: 1, files: [], target };
1109
+ } finally {
1110
+ rmSync(stagingRoot, { force: true, recursive: true });
1111
+ }
1112
+ };
445
1113
  const createMinimalViteConfig = (cwd, logger) => {
446
1114
  const target = join$1(cwd, "vite.config.ts");
447
1115
  try {
@@ -559,68 +1227,247 @@ const runInPlaceInit = (cwd, logger) => {
559
1227
  const offerIsInteractive = (options) => options.yes !== true && (options.prompt !== void 0 || (options.interactive ?? isInteractive()));
560
1228
  const maybeOfferExtras = async (options, projectDirectory) => {
561
1229
  const interactive = offerIsInteractive(options);
562
- const apply = async (names) => {
563
- const result = await runAddCommand({
564
- allowUnsafeSource: options.allowUnsafeSource,
565
- cwd: projectDirectory,
566
- from: options.registryFrom,
567
- logger: options.logger,
568
- names: [...names],
569
- source: options.registrySource,
570
- yes: true
1230
+ const preselected = options.add === void 0 ? [] : parseFeatureList(options.add, (message) => {
1231
+ options.logger.warn(message);
1232
+ });
1233
+ const applyAll = async (plans) => {
1234
+ if (plans.length === 0) {
1235
+ return true;
1236
+ }
1237
+ if (options.dryRun === true) {
1238
+ logWould(options.logger, `add ${plans.map((plan) => plan.label).join(", ")}`);
1239
+ return true;
1240
+ }
1241
+ const buffered = [];
1242
+ const applyLogger = isInteractive() ? {
1243
+ error: (message) => {
1244
+ buffered.push({ level: "error", message });
1245
+ },
1246
+ info: () => {
1247
+ },
1248
+ success: () => {
1249
+ },
1250
+ warn: (message) => {
1251
+ buffered.push({ level: "warn", message });
1252
+ }
1253
+ } : options.logger;
1254
+ const steps = plans.map((plan) => {
1255
+ return {
1256
+ running: `adding ${plan.label}…`,
1257
+ task: () => runAddCommand({
1258
+ allowUnsafeSource: options.allowUnsafeSource,
1259
+ cwd: projectDirectory,
1260
+ from: options.registryFrom,
1261
+ logger: applyLogger,
1262
+ names: [...plan.names],
1263
+ ref: options.ref,
1264
+ source: options.registrySource,
1265
+ transformManifest: plan.transformManifest,
1266
+ yes: true
1267
+ })
1268
+ };
571
1269
  });
572
- return result.code === 0;
1270
+ const done = `added ${plans.map((plan) => plan.label).join(", ")}`;
1271
+ const results = await withTuiBadgeProgress(BADGES.add, steps, done);
1272
+ for (const { level, message } of buffered) {
1273
+ options.logger[level](message);
1274
+ }
1275
+ return results.every((result) => result.code === 0);
573
1276
  };
574
- await offerRegistryExtras({
575
- apply,
1277
+ const deps = {
1278
+ applyAll,
576
1279
  interactive,
577
1280
  logger: options.logger,
578
- multiSelect: options.prompt?.multiSelect ?? ((message, choices, settings) => promptMultiSelect(message, choices, settings)),
579
- select: options.prompt?.select ?? ((message, choices, settings) => promptSelect(message, choices, settings))
580
- });
1281
+ multiSelect: options.prompt?.multiSelect ?? ((message, choices, settings) => tuiMultiSelect(message, choices, { ...settings, badge: BADGES.add })),
1282
+ preselected: preselected.length > 0 ? preselected : void 0,
1283
+ projectName: basename(projectDirectory),
1284
+ select: options.prompt?.select ?? ((message, choices, settings) => tuiSelect(message, choices, { ...settings, badge: BADGES.add })),
1285
+ text: options.prompt?.text ?? ((message, settings) => tuiText(message, { ...settings, badge: BADGES.add }))
1286
+ };
1287
+ if (preselected.length > 0) {
1288
+ await offerRegistryExtras(deps);
1289
+ return;
1290
+ }
1291
+ if (isInteractive()) {
1292
+ await tuiHeadline(COPY.extras);
1293
+ }
1294
+ await offerRegistryExtras(deps);
581
1295
  };
582
- const scaffoldNewProject = async (options, cwd) => {
583
- const name = options.name ?? "lunora-app";
584
- const templateType = options.templateType ?? "vite";
1296
+ const DEFAULT_FRAMEWORK = "react";
1297
+ const FRAMEWORK_CHOICES = [
1298
+ { description: "React SPA official create-vite base + the Lunora layer (the default)", label: "React", value: "react" },
1299
+ { description: "Vue SPA — create-vite base + Lunora", label: "Vue", value: "vue" },
1300
+ { description: "Solid SPA — create-vite base + Lunora", label: "Solid", value: "solid" },
1301
+ { description: "Svelte SPA — create-vite base + Lunora", label: "Svelte", value: "svelte" },
1302
+ { description: "TanStack Start (React) — SSR with live-loader routes", label: "TanStack Start · React", value: "tanstack-start-react" },
1303
+ { description: "TanStack Start (Solid)", label: "TanStack Start · Solid", value: "tanstack-start-solid" },
1304
+ { description: "React Router (v7, framework mode) — SSR composed into the Lunora worker", label: "React Router", value: "react-router" },
1305
+ { description: "Astro + a standalone Lunora worker", label: "Astro", value: "astro" },
1306
+ { description: "AnalogJS (Angular) — single-worker, Lunora mounted in Nitro", label: "Analog", value: "analog" },
1307
+ { description: "Nuxt (Vue) — single-worker, Lunora mounted in Nitro", label: "Nuxt", value: "nuxt" },
1308
+ { description: "SvelteKit + a standalone Lunora worker", label: "SvelteKit", value: "sveltekit" },
1309
+ { description: "Worker only — no frontend", label: "Standalone", value: "standalone" }
1310
+ ];
1311
+ const OVERLAY_VALUES = Object.keys(ADAPTERS).join("|");
1312
+ const TEMPLATE_VALUES = FRAMEWORK_CHOICES.filter((choice) => !isOverlayFramework(choice.value)).map((choice) => choice.value).join("|");
1313
+ const toScaffoldChoice = (value) => isOverlayFramework(value) ? { framework: value, kind: "overlay" } : { kind: "template", templateType: value };
1314
+ const resolveScaffoldChoice = async (options) => {
1315
+ if (options.vite !== void 0) {
1316
+ return { framework: options.vite, kind: "overlay" };
1317
+ }
1318
+ if (options.templateType !== void 0) {
1319
+ return { kind: "template", templateType: options.templateType };
1320
+ }
1321
+ if (!isInteractive() || options.yes === true) {
1322
+ return { framework: DEFAULT_FRAMEWORK, kind: "overlay" };
1323
+ }
1324
+ return toScaffoldChoice(await tuiSelect(COPY.framework, FRAMEWORK_CHOICES, { badge: BADGES.tmpl, default: DEFAULT_FRAMEWORK }) ?? DEFAULT_FRAMEWORK);
1325
+ };
1326
+ const nonInteractiveInitError = (options) => {
1327
+ if (isInteractive() || options.yes === true) {
1328
+ return void 0;
1329
+ }
1330
+ const missing = [];
1331
+ if (options.name === void 0) {
1332
+ missing.push("a project name (`lunora init <name>`)");
1333
+ }
1334
+ if (options.templateType === void 0 && options.vite === void 0) {
1335
+ missing.push(`a framework — \`--vite <${OVERLAY_VALUES}>\` for an SPA, or \`-t <${TEMPLATE_VALUES}>\` for a bespoke template`);
1336
+ }
1337
+ if (missing.length === 0) {
1338
+ return void 0;
1339
+ }
1340
+ return `lunora init can't prompt in a non-interactive terminal — provide ${missing.join(" and ")}, or pass --yes to accept the defaults.`;
1341
+ };
1342
+ const scaffoldOverlayPath = async (options, framework, name, target) => {
1343
+ if (!isOverlayFramework(framework)) {
1344
+ options.logger.error(`init: unknown framework "${framework}". Supported overlays: ${Object.keys(ADAPTERS).join(", ")}.`);
1345
+ return { code: 1, files: [], target };
1346
+ }
1347
+ if (!await verifyRemoteTemplate({ isLocal: options.overlayBaseFrom !== void 0, logger: options.logger })) {
1348
+ return { code: 1, files: [], target };
1349
+ }
1350
+ mkdirSync(target, { recursive: true });
1351
+ return scaffoldViteOverlay({ framework, logger: options.logger, name, overlayBaseFrom: options.overlayBaseFrom, target });
1352
+ };
1353
+ const scaffoldTemplatePath = async (options, templateType, name, target) => {
585
1354
  if (templateType === "next") {
586
- options.logger.warn('template "next" is not yet available — re-run with `-t vite` or `-t standalone`.');
1355
+ options.logger.warn('template "next" is not yet available — re-run with `--vite react` or `-t standalone`.');
1356
+ return { code: 1, files: [], target };
1357
+ }
1358
+ if (options.from !== void 0) {
1359
+ return scaffoldFromLocal(options.from, templateType, target, name, options.logger);
1360
+ }
1361
+ if (options.source !== void 0 && options.source.length > 0 && !options.allowUnsafeSource && !isSafeSource(options.source)) {
1362
+ options.logger.error(
1363
+ `init: refusing --source ${options.source} — only gh:, github:, or https:// sources are allowed (and may not contain ".."). Re-run with --allow-unsafe-source if you really want this.`
1364
+ );
1365
+ return { code: 1, files: [], target };
1366
+ }
1367
+ if (!await verifyRemoteTemplate({ isLocal: false, logger: options.logger, source: resolveTemplateSource(templateType, options.source, options.ref) })) {
1368
+ return { code: 1, files: [], target };
1369
+ }
1370
+ return scaffoldFromRemote({ logger: options.logger, name, ref: options.ref, source: options.source, target, templateType });
1371
+ };
1372
+ const scaffoldNewProject = async (options, cwd, recordTarget) => {
1373
+ await tuiMoonrise("realtime backend on Cloudflare Workers + Durable Objects");
1374
+ const blocked = nonInteractiveInitError(options);
1375
+ if (blocked !== void 0) {
1376
+ options.logger.error(blocked);
587
1377
  return { code: 1, files: [], target: "" };
588
1378
  }
1379
+ const suggestedName = generateProjectName();
1380
+ const name = options.name ?? await tuiText(COPY.name, { badge: BADGES.dir, default: suggestedName, placeholder: suggestedName });
1381
+ const choice = await resolveScaffoldChoice(options);
589
1382
  if (name.includes("/") || name.includes("\\") || name === ".." || name === ".") {
590
1383
  options.logger.error(`init: refusing project name "${name}" — must not contain path separators or be "." / "..".`);
591
1384
  return { code: 1, files: [], target: "" };
592
1385
  }
593
1386
  const target = resolve(cwd, name);
594
- if (existsSync(target)) {
1387
+ const targetPreExisted = existsSync(target);
1388
+ if (targetPreExisted) {
595
1389
  const entries = readdirSync(target);
596
1390
  if (entries.length > 0) {
597
1391
  options.logger.error(`target directory not empty: ${target}`);
598
1392
  return { code: 1, files: [], target };
599
1393
  }
600
1394
  }
601
- if (options.from !== void 0) {
602
- return scaffoldFromLocal(options.from, templateType, target, name, options.logger);
1395
+ if (options.dryRun === true) {
1396
+ const what = choice.kind === "overlay" ? `the ${choice.framework} create-vite overlay` : `the ${choice.templateType} template`;
1397
+ logWould(options.logger, `scaffold ${what} into ${target}`);
1398
+ return { code: 0, files: [], target };
603
1399
  }
604
- if (options.source !== void 0 && options.source.length > 0 && !options.allowUnsafeSource && !isSafeSource(options.source)) {
605
- options.logger.error(
606
- `init: refusing --source ${options.source} — only gh:, github:, or https:// sources are allowed (and may not contain ".."). Re-run with --allow-unsafe-source if you really want this.`
607
- );
608
- return { code: 1, files: [], target };
1400
+ recordTarget(target, targetPreExisted);
1401
+ return choice.kind === "overlay" ? scaffoldOverlayPath(options, choice.framework, name, target) : scaffoldTemplatePath(options, choice.templateType, name, target);
1402
+ };
1403
+ const resetScaffoldOnCancel = (cleanup, logger) => {
1404
+ const { target, targetPreExisted } = cleanup;
1405
+ if (target === void 0 || !existsSync(target)) {
1406
+ return;
1407
+ }
1408
+ if (targetPreExisted === true) {
1409
+ for (const entry of readdirSync(target)) {
1410
+ rmSync(join$1(target, entry), { force: true, recursive: true });
1411
+ }
1412
+ } else {
1413
+ rmSync(target, { force: true, recursive: true });
609
1414
  }
610
- return scaffoldFromRemote(options.source, templateType, target, name, options.logger);
1415
+ logger.info(`removed the partially-created project at ${target}`);
1416
+ };
1417
+ const runScaffoldStep = async (options, cwd, recordTarget) => {
1418
+ if (options.inPlace !== true) {
1419
+ return scaffoldNewProject(options, cwd, recordTarget);
1420
+ }
1421
+ if (options.dryRun === true) {
1422
+ logWould(options.logger, `configure Lunora into ${cwd}`);
1423
+ return { code: 0, files: [], target: cwd };
1424
+ }
1425
+ return runInPlaceInit(cwd, options.logger);
1426
+ };
1427
+ const runPostScaffold = async (options, result, cwd) => {
1428
+ await maybeOfferExtras(options, result.target);
1429
+ const installedManager = options.inPlace === true ? void 0 : await maybeOfferInstall(options, result.target);
1430
+ if (options.inPlace !== true) {
1431
+ await maybeOfferGit(options, result.target);
1432
+ await printNextSteps(basename(result.target), installedManager, isInsideMonorepo(cwd));
1433
+ await emitMascot(options.logger);
1434
+ }
1435
+ };
1436
+ const scaffoldCiPipeline = (options, result, cwd) => {
1437
+ if (result.code !== 0 || options.ci === void 0) {
1438
+ return;
1439
+ }
1440
+ if (options.dryRun === true) {
1441
+ logWould(options.logger, `scaffold a ${options.ci} CI deploy pipeline`);
1442
+ return;
1443
+ }
1444
+ scaffoldCiWorkflow(options.inPlace === true ? cwd : result.target, options.ci, options.logger);
611
1445
  };
612
1446
  const runInitCommand = async (options) => {
613
1447
  const cwd = options.cwd ?? process.cwd();
614
- const result = options.inPlace === true ? runInPlaceInit(cwd, options.logger) : await scaffoldNewProject(options, cwd);
615
- if (result.code === 0 && result.target !== "") {
616
- await maybeOfferExtras(options, result.target);
617
- }
618
- if (result.code === 0 && options.ci !== void 0) {
619
- scaffoldCiWorkflow(options.inPlace === true ? cwd : result.target, options.ci, options.logger);
1448
+ const cleanup = {};
1449
+ let result;
1450
+ try {
1451
+ result = await runScaffoldStep(options, cwd, (target, preExisted) => {
1452
+ cleanup.target = target;
1453
+ cleanup.targetPreExisted = preExisted;
1454
+ });
1455
+ if (result.code === 0 && result.target !== "") {
1456
+ cleanup.target = void 0;
1457
+ await runPostScaffold(options, result, cwd);
1458
+ }
1459
+ } catch (error) {
1460
+ if (error instanceof PromptCancelledError) {
1461
+ resetScaffoldOnCancel(cleanup, options.logger);
1462
+ process.stdout.write("\n ✖ Setup cancelled — run `lunora init` again whenever you're ready. 🌙\n");
1463
+ return { code: 130, files: [], target: "" };
1464
+ }
1465
+ throw error;
620
1466
  }
1467
+ scaffoldCiPipeline(options, result, cwd);
621
1468
  return result;
622
1469
  };
623
- const isTemplate = (value) => value === "astro" || value === "next" || value === "nuxt" || value === "standalone" || value === "sveltekit" || value === "tanstack-start-react" || value === "tanstack-start-solid" || value === "vite";
1470
+ const isTemplate = (value) => value === "analog" || value === "astro" || value === "next" || value === "nuxt" || value === "react-router" || value === "standalone" || value === "sveltekit" || value === "tanstack-start-react" || value === "tanstack-start-solid";
624
1471
  const resolveCiProvider = (raw, logger) => {
625
1472
  if (raw === void 0) {
626
1473
  return void 0;
@@ -632,21 +1479,24 @@ const resolveCiProvider = (raw, logger) => {
632
1479
  return void 0;
633
1480
  };
634
1481
  const execute = defineHandler(({ argument, cwd, logger, options }) => {
635
- const templateRaw = options.template ?? "vite";
636
- const template = isTemplate(templateRaw) ? templateRaw : "vite";
1482
+ const templateType = options.template !== void 0 && isTemplate(options.template) ? options.template : void 0;
637
1483
  return runInitCommand({
1484
+ add: options.add,
638
1485
  allowUnsafeSource: options.allowUnsafeSource === true,
639
1486
  cwd,
640
1487
  ci: resolveCiProvider(options.ci, logger),
1488
+ dryRun: options.dryRun === true,
641
1489
  from: options.from,
642
1490
  inPlace: options.here === true,
643
1491
  interactive: options.interactive === true ? true : void 0,
644
1492
  logger,
645
1493
  name: argument[0],
1494
+ ref: options.ref,
646
1495
  source: options.source,
647
- templateType: template,
1496
+ templateType,
1497
+ vite: options.vite,
648
1498
  yes: options.yes === true
649
1499
  });
650
1500
  });
651
1501
 
652
- export { execute, isTemplate, runInitCommand };
1502
+ export { execute, isTemplate, resolveTemplateSource, runInitCommand };