@lunora/cli 1.0.0-alpha.2 → 1.0.0-alpha.20

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 (48) 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 +7 -7
  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 +999 -107
  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-hl0mRqqg.mjs} +177 -25
  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/{insertSchemaExtension-BuzF6-t2.mjs → insertSchemaExtension-DAqbfr9Z.mjs} +15 -10
  40. package/dist/packem_shared/{output-format-7gyGR3h8.mjs → output-format-wUvAN6AL.mjs} +1 -1
  41. package/dist/packem_shared/runAddCommand-vJdgiR5t.mjs +4 -0
  42. package/dist/packem_shared/{schemaIrToSnapshot-aBTo7TM5.mjs → schemaIrToSnapshot-DdsljJT-.mjs} +1 -1
  43. package/dist/packem_shared/storage-B7hHSTZP.mjs +84 -0
  44. package/dist/packem_shared/tui-prompts-M6OWsuyw.mjs +663 -0
  45. package/package.json +11 -10
  46. package/skills/lunora-setup-storage/SKILL.md +7 -3
  47. package/dist/packem_shared/features-ocSSpZtS.mjs +0 -24
  48. /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 resolveTagVersions, d as resolveSourceRef, e as resolveDistTag, r as runAddCommand } from '../packem_shared/commands-hl0mRqqg.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, b as tuiConfirm, w as withTuiSpinner, i as tuiNextSteps, j as tuiTasks, k as tuiMultiSelect, l as withTuiBadgeProgress } from '../packem_shared/tui-prompts-M6OWsuyw.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-B7hHSTZP.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,473 @@ 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 isLunoraDep$1 = (name) => name === "lunorash" || name.startsWith("@lunora/");
599
+ const stampRange = (name, range, distTag, versions) => isLunoraDep$1(name) ? versions?.get(name) ?? distTag : range;
600
+ const withDependency = (map, name, range, distTag) => {
601
+ return { ...map, [name]: stampRange(name, range, distTag) };
602
+ };
603
+ const restampLunora = (map, distTag, versions) => Object.fromEntries(Object.entries(map).map(([name, range]) => [name, stampRange(name, range, distTag, versions)]));
604
+ const patchPackageJson = async (target, name, adapter, distTag) => {
605
+ const path = join$1(target, "package.json");
606
+ const parsed = JSON.parse(readFileSync(path, "utf8"));
607
+ let dependencies = withDependency(parsed.dependencies ?? {}, "lunorash", distTag, distTag);
608
+ if (adapter.adapter.startsWith("@lunora/")) {
609
+ dependencies = withDependency(dependencies, adapter.adapter, distTag, distTag);
610
+ }
611
+ for (const [depName, range] of Object.entries(adapter.extraDependencies ?? {})) {
612
+ dependencies = withDependency(dependencies, depName, range, distTag);
613
+ }
614
+ let devDependencies = withDependency(parsed.devDependencies ?? {}, "@lunora/vite", distTag, distTag);
615
+ devDependencies = withDependency(devDependencies, "@lunora/studio", distTag, distTag);
616
+ for (const [depName, range] of Object.entries(COMMON_DEV_DEPENDENCIES)) {
617
+ devDependencies = withDependency(devDependencies, depName, range, distTag);
618
+ }
619
+ const lunoraNames = [...Object.keys(dependencies), ...Object.keys(devDependencies)].filter((depName) => isLunoraDep$1(depName));
620
+ const versions = await resolveTagVersions(lunoraNames, distTag);
621
+ parsed.name = name;
622
+ parsed.imports = { ...parsed.imports, "#lunora/*": "./lunora/*" };
623
+ parsed.dependencies = restampLunora(dependencies, distTag, versions);
624
+ parsed.devDependencies = restampLunora(devDependencies, distTag, versions);
625
+ parsed.scripts = { ...parsed.scripts, codegen: "lunora codegen", deploy: "vite build && lunora deploy" };
626
+ writeFileSync(path, `${JSON.stringify(parsed, void 0, 4)}
627
+ `, "utf8");
628
+ };
629
+ const patchBaseViteConfig = (target, logger) => {
630
+ const candidate = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"].map((file) => join$1(target, file)).find((path) => existsSync(path));
631
+ if (candidate === void 0) {
632
+ logger.warn("overlay: no vite.config found in the create-vite base — add `lunora()` to your Vite plugins manually.");
633
+ return;
634
+ }
635
+ const result = patchViteConfig(readFileSync(candidate, "utf8"));
636
+ if (result.changed) {
637
+ writeFileSync(candidate, result.code, "utf8");
638
+ }
639
+ };
640
+ const applyLunoraOverlay = async (options) => {
641
+ const { adapter, distTag, logger, name, target } = options;
642
+ const written = [];
643
+ writeFile(target, join$1("lunora", "schema.ts"), LUNORA_SCHEMA, written);
644
+ writeFile(target, join$1("lunora", "messages.ts"), LUNORA_MESSAGES, written);
645
+ writeFile(target, join$1("src", "server.ts"), SERVER_ENTRY, written);
646
+ writeFile(target, "wrangler.jsonc", WRANGLER.replaceAll("__NAME__", name), written);
647
+ writeFile(target, ".env.example", ENV_EXAMPLE, written);
648
+ for (const file of adapter.files) {
649
+ writeFile(target, file.path, file.contents, written);
650
+ }
651
+ patchBaseViteConfig(target, logger);
652
+ await patchPackageJson(target, name, adapter, distTag);
653
+ ensureGitignore(target);
654
+ return written;
655
+ };
656
+
657
+ const ADJECTIVES = [
658
+ "lunar",
659
+ "silver",
660
+ "silent",
661
+ "waning",
662
+ "waxing",
663
+ "crescent",
664
+ "cosmic",
665
+ "stellar",
666
+ "orbital",
667
+ "gibbous",
668
+ "twilight",
669
+ "midnight",
670
+ "shimmering",
671
+ "drifting",
672
+ "weightless"
673
+ ];
674
+ const NOUNS = [
675
+ "moon",
676
+ "tide",
677
+ "crater",
678
+ "comet",
679
+ "eclipse",
680
+ "halo",
681
+ "orbit",
682
+ "nebula",
683
+ "voyager",
684
+ "lander",
685
+ "rover",
686
+ "beacon",
687
+ "harbor",
688
+ "meadow",
689
+ "fox"
690
+ ];
691
+ const pick = (items) => (
692
+ // eslint-disable-next-line sonarjs/pseudo-random -- cosmetic default project name, not a security decision.
693
+ items[Math.floor(Math.random() * items.length)]
694
+ );
695
+ const generateProjectName = () => `${pick(ADJECTIVES)}-${pick(NOUNS)}`;
696
+
697
+ const isOnline = async () => dns.lookup("github.com").then(
698
+ () => true,
699
+ () => false
700
+ );
701
+ const GITHUB_SOURCE = /^(?:gh|github):([^/]+)\/([^#/]+)(?:\/[^#]*)?(?:#(.+))?$/;
702
+ const parseGitHubSource = (source) => {
703
+ const match = GITHUB_SOURCE.exec(source);
704
+ if (match === null) {
705
+ return void 0;
706
+ }
707
+ const [, owner, repo, ref] = match;
708
+ if (owner === void 0 || repo === void 0) {
709
+ return void 0;
710
+ }
711
+ return { owner, ref: ref ?? "HEAD", repo };
712
+ };
713
+ const templateRefExists = async (source) => {
714
+ const parsed = parseGitHubSource(source);
715
+ if (parsed === void 0) {
716
+ return void 0;
717
+ }
718
+ const url = `https://codeload.github.com/${parsed.owner}/${parsed.repo}/tar.gz/${parsed.ref}`;
719
+ try {
720
+ const response = await fetch(url, { method: "HEAD" });
721
+ if (response.status === 404) {
722
+ return false;
290
723
  }
724
+ return response.ok ? true : void 0;
725
+ } catch {
726
+ return void 0;
727
+ }
728
+ };
729
+ const verifyRemoteTemplate = async (params) => {
730
+ if (params.isLocal) {
731
+ return true;
732
+ }
733
+ const isGitHubBacked = params.source === void 0 || parseGitHubSource(params.source) !== void 0;
734
+ if (!isGitHubBacked) {
735
+ return true;
736
+ }
737
+ if (!await isOnline()) {
738
+ params.logger.error("you appear to be offline — connect to the internet and try again, or scaffold from a local template with `--from <dir>`.");
739
+ return false;
740
+ }
741
+ if (params.source !== void 0 && await templateRefExists(params.source) === false) {
742
+ params.logger.error(`template source not found: ${params.source} — double-check --ref / --source, or browse the templates at https://lunora.sh/docs.`);
743
+ return false;
291
744
  }
745
+ return true;
292
746
  };
293
747
 
748
+ const COPY = {
749
+ extras: "Let's finish setting up your app.",
750
+ framework: "Which framework should we launch?",
751
+ git: "Initialize a new git repository? (optional)",
752
+ install: "Install dependencies now?",
753
+ name: "Where should we land your project?",
754
+ nextHeader: "Liftoff confirmed — explore your project!",
755
+ packageManager: "Which package manager?"
756
+ };
294
757
  const TEXT_EXTENSIONS = /* @__PURE__ */ new Set([".gitignore", ".html", ".js", ".json", ".jsonc", ".md", ".mjs", ".ts", ".tsx"]);
295
758
  const VITE_CONFIG_CANDIDATES = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"];
296
759
  const MINIMAL_VITE_CONFIG = `import { defineConfig } from "vite";
@@ -324,32 +787,6 @@ export const send = mutation
324
787
  });
325
788
  `;
326
789
  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
790
  const isTextFile = (filePath) => {
354
791
  const lastDot = filePath.lastIndexOf(".");
355
792
  if (lastDot === -1) {
@@ -358,6 +795,57 @@ const isTextFile = (filePath) => {
358
795
  return TEXT_EXTENSIONS.has(filePath.slice(lastDot));
359
796
  };
360
797
  const substitute = (content, name) => content.replaceAll("{{name}}", name);
798
+ const isLunoraDep = (name) => name === "lunorash" || name.startsWith("@lunora/");
799
+ const resolveLunoraVersions = async (files, distTag) => {
800
+ const names = /* @__PURE__ */ new Set();
801
+ for (const file of files) {
802
+ if (basename(file) !== "package.json") {
803
+ continue;
804
+ }
805
+ try {
806
+ const parsed = JSON.parse(readFileSync(file, "utf8"));
807
+ for (const section of ["dependencies", "devDependencies"]) {
808
+ for (const name of Object.keys(parsed[section] ?? {})) {
809
+ if (isLunoraDep(name)) {
810
+ names.add(name);
811
+ }
812
+ }
813
+ }
814
+ } catch {
815
+ }
816
+ }
817
+ return resolveTagVersions(names, distTag);
818
+ };
819
+ const stampLunoraDeps = (packageJsonText, distTag, versions) => {
820
+ let parsed;
821
+ try {
822
+ parsed = JSON.parse(packageJsonText);
823
+ } catch {
824
+ return packageJsonText;
825
+ }
826
+ let text = packageJsonText;
827
+ for (const section of ["dependencies", "devDependencies"]) {
828
+ for (const name of Object.keys(parsed[section] ?? {})) {
829
+ if (!isLunoraDep(name)) {
830
+ continue;
831
+ }
832
+ const pin = versions.get(name) ?? distTag;
833
+ const edits = modify(text, [section, name], pin, { formattingOptions: { insertSpaces: true, tabSize: 4 } });
834
+ text = applyEdits(text, edits);
835
+ }
836
+ }
837
+ return text;
838
+ };
839
+ const PNPM_BUILT_DEPENDENCIES = ["esbuild", "sharp", "workerd"];
840
+ const PNPM_WORKSPACE_FILENAME = "pnpm-workspace.yaml";
841
+ const pnpmWorkspaceYaml = () => [
842
+ "# pnpm reads its settings from here (the package.json `pnpm` field is no longer read).",
843
+ "# Pre-approve the toolchain's native build scripts so `pnpm install` runs them",
844
+ "# without the interactive `pnpm approve-builds` step.",
845
+ "allowBuilds:",
846
+ ...PNPM_BUILT_DEPENDENCIES.map((name) => ` ${name}: true`),
847
+ ""
848
+ ].join("\n");
361
849
  const collectFiles = (directory) => {
362
850
  const out = [];
363
851
  for (const entry of walkSync(directory, { includeDirs: false, includeFiles: true })) {
@@ -365,15 +853,20 @@ const collectFiles = (directory) => {
365
853
  }
366
854
  return out;
367
855
  };
368
- const copyTemplate = (sourceDirectory, target, name) => {
856
+ const copyTemplate = async (sourceDirectory, target, name) => {
369
857
  const files = collectFiles(sourceDirectory);
370
858
  const written = [];
859
+ const distTag = resolveDistTag();
860
+ const versions = await resolveLunoraVersions(files, distTag);
371
861
  for (const source of files) {
372
862
  const relativePath = relative(sourceDirectory, source);
373
863
  const destination = join$1(target, relativePath);
374
864
  mkdirSync(dirname$1(destination), { recursive: true });
375
865
  const raw = readFileSync(source);
376
- const text = isTextFile(source) ? substitute(raw.toString("utf8"), name) : void 0;
866
+ let text = isTextFile(source) ? substitute(raw.toString("utf8"), name) : void 0;
867
+ if (text !== void 0 && basename(source) === "package.json") {
868
+ text = stampLunoraDeps(text, distTag, versions);
869
+ }
377
870
  if (text === void 0) {
378
871
  writeFileSync(destination, raw);
379
872
  } else {
@@ -383,11 +876,11 @@ const copyTemplate = (sourceDirectory, target, name) => {
383
876
  }
384
877
  return written;
385
878
  };
386
- const resolveTemplateSource = (templateType, source) => {
879
+ const resolveTemplateSource = (templateType, source, ref) => {
387
880
  if (source !== void 0 && source.length > 0) {
388
881
  return source;
389
882
  }
390
- return `${DEFAULT_SOURCE_BASE}/${templateType}#${resolveDefaultSourceRef()}`;
883
+ return `${DEFAULT_SOURCE_BASE}/${templateType}#${resolveSourceRef(ref)}`;
391
884
  };
392
885
  const isSafeSource = (source) => {
393
886
  if (source.includes("..")) {
@@ -395,46 +888,205 @@ const isSafeSource = (source) => {
395
888
  }
396
889
  return source.startsWith("gh:") || source.startsWith("github:") || source.startsWith("https://");
397
890
  };
398
- const logScaffoldSuccess = (logger, written, target, name) => {
891
+ const logWould = (logger, action) => {
892
+ logger.info(`[dry-run] would ${action}`);
893
+ };
894
+ const logScaffoldSuccess = (logger, written, target) => {
895
+ if (isInteractive()) {
896
+ process.stdout.write("\n");
897
+ }
399
898
  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");
404
899
  };
405
- const scaffoldFromLocal = (fromRoot, templateType, target, name, logger) => {
900
+ const runScriptCommand = (manager, script) => {
901
+ if (manager === "npm") {
902
+ return `npm run ${script}`;
903
+ }
904
+ if (manager === "bun") {
905
+ return `bun run ${script}`;
906
+ }
907
+ return `${manager} ${script}`;
908
+ };
909
+ const isInsideMonorepo = (startDirectory) => {
910
+ let directory = resolve(startDirectory);
911
+ for (; ; ) {
912
+ if (existsSync(join$1(directory, "pnpm-workspace.yaml"))) {
913
+ return true;
914
+ }
915
+ const packagePath = join$1(directory, "package.json");
916
+ if (existsSync(packagePath)) {
917
+ try {
918
+ const parsed = JSON.parse(readFileSync(packagePath, "utf8"));
919
+ if (parsed.workspaces !== void 0) {
920
+ return true;
921
+ }
922
+ } catch {
923
+ }
924
+ }
925
+ const parent = dirname$1(directory);
926
+ if (parent === directory) {
927
+ return false;
928
+ }
929
+ directory = parent;
930
+ }
931
+ };
932
+ const isInsideGitRepo = (startDirectory) => {
933
+ let directory = resolve(startDirectory);
934
+ for (; ; ) {
935
+ if (existsSync(join$1(directory, ".git"))) {
936
+ return true;
937
+ }
938
+ const parent = dirname$1(directory);
939
+ if (parent === directory) {
940
+ return false;
941
+ }
942
+ directory = parent;
943
+ }
944
+ };
945
+ const maybeOfferGit = async (options, target) => {
946
+ if (options.yes === true || !isInteractive() || isInsideGitRepo(dirname$1(target))) {
947
+ return;
948
+ }
949
+ if (!await tuiConfirm(COPY.git, { badge: BADGES.git, defaultYes: false })) {
950
+ await tuiInfo("Sounds good! You can always run git init manually.");
951
+ return;
952
+ }
953
+ if (options.dryRun === true) {
954
+ logWould(options.logger, "initialize a git repository");
955
+ return;
956
+ }
957
+ const spawner = options.spawner ?? defaultSpawner;
958
+ const result = await withTuiSpinner("Initializing a git repository…", () => spawner({ args: ["init"], command: "git", cwd: target }));
959
+ if (result.code === 0) {
960
+ await emitStep("git", "Initialized an empty git repository.");
961
+ } else {
962
+ options.logger.warn("`git init` failed — initialize it yourself later with `git init`.");
963
+ }
964
+ };
965
+ const printNextSteps = async (name, installed, insideMonorepo) => {
966
+ const manager = installed ?? "pnpm";
967
+ const steps = [{ code: `cd ./${name}`, lead: "Enter your project directory using" }];
968
+ if (installed === void 0) {
969
+ steps.push({ code: `${manager} install`, lead: "Install dependencies with", tail: insideMonorepo ? " from the workspace root" : void 0 });
970
+ }
971
+ steps.push(
972
+ { code: runScriptCommand(manager, "dev"), lead: "Run", tail: " to start the dev server." },
973
+ { code: "lunora add", lead: "Add features like auth or storage using" }
974
+ );
975
+ const help = [
976
+ { code: "https://lunora.sh/docs", lead: "Read the docs at" },
977
+ { code: "https://lunora.sh/chat", lead: "Stuck? Join the chat at" }
978
+ ];
979
+ if (isInteractive()) {
980
+ await tuiNextSteps(BADGES.next, COPY.nextHeader, steps, help);
981
+ return;
982
+ }
983
+ const lines = steps.map((step) => `${step.lead} ${step.code}${step.tail ?? ""}`);
984
+ lines.push("", ...help.map((line) => `${line.lead} ${line.code}${line.tail ?? ""}`));
985
+ await emitStep("next", COPY.nextHeader, lines.join("\n"));
986
+ };
987
+ const offerInstallIsInteractive = (options) => options.yes !== true && (options.installPrompt !== void 0 || isInteractive());
988
+ const maybeOfferInstall = async (options, target) => {
989
+ if (!offerInstallIsInteractive(options)) {
990
+ return void 0;
991
+ }
992
+ if (isInsideMonorepo(dirname$1(target))) {
993
+ return void 0;
994
+ }
995
+ const managers = detectInstalledManagers(options.packageManagerProbe);
996
+ const [defaultManager] = managers;
997
+ if (defaultManager === void 0) {
998
+ return void 0;
999
+ }
1000
+ const confirm = options.installPrompt?.confirmInstall ?? (async () => tuiConfirm(COPY.install, { badge: BADGES.deps, defaultYes: true }));
1001
+ if (!await confirm()) {
1002
+ await tuiInfo("No problem! Remember to install dependencies after setup.");
1003
+ return void 0;
1004
+ }
1005
+ let manager = defaultManager;
1006
+ if (managers.length > 1) {
1007
+ manager = options.installPrompt ? await options.installPrompt.selectManager(managers) : await tuiSelect(
1008
+ COPY.packageManager,
1009
+ managers.map((candidate) => {
1010
+ return { label: candidate, value: candidate };
1011
+ }),
1012
+ { badge: BADGES.deps, default: defaultManager }
1013
+ ) ?? defaultManager;
1014
+ }
1015
+ if (options.dryRun === true) {
1016
+ logWould(options.logger, `install dependencies with ${manager}`);
1017
+ return void 0;
1018
+ }
1019
+ if (manager === "pnpm") {
1020
+ const workspacePath = join$1(target, PNPM_WORKSPACE_FILENAME);
1021
+ if (!existsSync(workspacePath)) {
1022
+ writeFileSync(workspacePath, pnpmWorkspaceYaml(), "utf8");
1023
+ }
1024
+ }
1025
+ const spawner = options.spawner ?? defaultSpawner;
1026
+ const { args, command } = installArgsFor(manager);
1027
+ await emitStep("deps", `Installing dependencies with ${manager}…`);
1028
+ const result = await spawner({ args, command, cwd: target });
1029
+ if (result.code !== 0) {
1030
+ options.logger.warn(`\`${command} install\` exited with code ${String(result.code)} — run it yourself in ${basename(target)}/.`);
1031
+ return void 0;
1032
+ }
1033
+ await emitStep("deps", `Dependencies installed with ${manager}.`);
1034
+ return manager;
1035
+ };
1036
+ const scaffoldFromLocal = async (fromRoot, templateType, target, name, logger) => {
406
1037
  const templateDirectory = join$1(fromRoot, templateType);
407
1038
  if (!existsSync(templateDirectory)) {
408
1039
  logger.error(`template not found in local source: ${templateDirectory}`);
409
1040
  return { code: 1, files: [], target };
410
1041
  }
411
- const written = copyTemplate(templateDirectory, target, name);
412
- logScaffoldSuccess(logger, written, target, name);
1042
+ const written = await copyTemplate(templateDirectory, target, name);
1043
+ logScaffoldSuccess(logger, written, target);
413
1044
  return { code: 0, files: written, target };
414
1045
  };
415
- const scaffoldFromRemote = async (source, templateType, target, name, logger) => {
1046
+ const scaffoldFromRemote = async (options) => {
1047
+ const { logger, name, ref, source, target, templateType } = options;
416
1048
  const stagingRoot = mkdtempSync(join$1(tmpdir(), "lunora-init-fetch-"));
417
1049
  const stagingDirectory = join$1(stagingRoot, "template");
418
1050
  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
- });
1051
+ const remote = resolveTemplateSource(templateType, source, ref);
1052
+ let downloaded;
1053
+ let written = [];
1054
+ await tuiTasks(
1055
+ [
1056
+ {
1057
+ label: `${templateType} template fetched`,
1058
+ run: async () => {
1059
+ downloaded = await downloadTemplate(remote, {
1060
+ cwd: stagingRoot,
1061
+ dir: stagingDirectory,
1062
+ force: true,
1063
+ install: false,
1064
+ silent: true
1065
+ });
1066
+ }
1067
+ },
1068
+ {
1069
+ label: `files copied into ${name}/`,
1070
+ run: async () => {
1071
+ written = await copyTemplate(stagingDirectory, target, name);
1072
+ }
1073
+ }
1074
+ ],
1075
+ { end: "Project initialized!", start: "Project initializing…" }
1076
+ );
428
1077
  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))`);
1078
+ if (isInteractive()) {
1079
+ process.stdout.write("\n");
433
1080
  }
434
- const written = copyTemplate(stagingDirectory, target, name);
435
- logScaffoldSuccess(logger, written, target, name);
1081
+ logger.info(
1082
+ downloaded?.commit ? `template: ${downloaded.source} @ ${downloaded.commit} (${String(staged.length)} files)` : `template: ${downloaded?.source ?? remote} (${String(staged.length)} files)`
1083
+ );
1084
+ logScaffoldSuccess(logger, written, target);
436
1085
  return { code: 0, files: written, target };
437
1086
  } catch (error) {
1087
+ if (error instanceof PromptCancelledError) {
1088
+ throw error;
1089
+ }
438
1090
  const message = error instanceof Error ? error.message : String(error);
439
1091
  logger.error(`failed to download template: ${message}`);
440
1092
  return { code: 1, files: [], target };
@@ -442,6 +1094,64 @@ const scaffoldFromRemote = async (source, templateType, target, name, logger) =>
442
1094
  rmSync(stagingRoot, { force: true, recursive: true });
443
1095
  }
444
1096
  };
1097
+ const renameCreateViteDotfiles = (directory) => {
1098
+ for (const file of ["_gitignore", "_npmrc", "_gitattributes"]) {
1099
+ const from = join$1(directory, file);
1100
+ if (existsSync(from)) {
1101
+ renameSync(from, join$1(directory, `.${file.slice(1)}`));
1102
+ }
1103
+ }
1104
+ };
1105
+ const scaffoldViteOverlay = async (options) => {
1106
+ const { framework, logger, name, overlayBaseFrom, target } = options;
1107
+ const adapter = ADAPTERS[framework];
1108
+ const stagingRoot = mkdtempSync(join$1(tmpdir(), "lunora-vite-base-"));
1109
+ try {
1110
+ let localBase;
1111
+ if (overlayBaseFrom !== void 0) {
1112
+ localBase = join$1(overlayBaseFrom, `template-${adapter.createViteTemplate}`);
1113
+ if (!existsSync(localBase)) {
1114
+ logger.error(`create-vite base not found on disk: ${localBase}`);
1115
+ return { code: 1, files: [], target };
1116
+ }
1117
+ }
1118
+ const copyBase = async () => {
1119
+ if (localBase !== void 0) {
1120
+ cpSync(localBase, target, { recursive: true });
1121
+ return;
1122
+ }
1123
+ const stagingDirectory = join$1(stagingRoot, "base");
1124
+ const remote = `github:vitejs/vite/packages/create-vite/template-${adapter.createViteTemplate}#main`;
1125
+ await downloadTemplate(remote, { cwd: stagingRoot, dir: stagingDirectory, force: true, install: false, silent: true });
1126
+ renameCreateViteDotfiles(stagingDirectory);
1127
+ cpSync(stagingDirectory, target, { recursive: true });
1128
+ };
1129
+ let written = [];
1130
+ await tuiTasks(
1131
+ [
1132
+ { label: `create-vite (${adapter.label}) base ready`, run: copyBase },
1133
+ {
1134
+ label: `Lunora overlay applied (${adapter.label})`,
1135
+ run: async () => {
1136
+ written = await applyLunoraOverlay({ adapter, distTag: resolveDistTag(), logger, name, target });
1137
+ }
1138
+ }
1139
+ ],
1140
+ { end: "Project initialized!", start: "Project initializing…" }
1141
+ );
1142
+ logScaffoldSuccess(logger, written, target);
1143
+ return { code: 0, files: [...collectFiles(target)], target };
1144
+ } catch (error) {
1145
+ if (error instanceof PromptCancelledError) {
1146
+ throw error;
1147
+ }
1148
+ const message = error instanceof Error ? error.message : String(error);
1149
+ logger.error(`failed to scaffold the ${adapter.label} base: ${message}`);
1150
+ return { code: 1, files: [], target };
1151
+ } finally {
1152
+ rmSync(stagingRoot, { force: true, recursive: true });
1153
+ }
1154
+ };
445
1155
  const createMinimalViteConfig = (cwd, logger) => {
446
1156
  const target = join$1(cwd, "vite.config.ts");
447
1157
  try {
@@ -559,68 +1269,247 @@ const runInPlaceInit = (cwd, logger) => {
559
1269
  const offerIsInteractive = (options) => options.yes !== true && (options.prompt !== void 0 || (options.interactive ?? isInteractive()));
560
1270
  const maybeOfferExtras = async (options, projectDirectory) => {
561
1271
  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
1272
+ const preselected = options.add === void 0 ? [] : parseFeatureList(options.add, (message) => {
1273
+ options.logger.warn(message);
1274
+ });
1275
+ const applyAll = async (plans) => {
1276
+ if (plans.length === 0) {
1277
+ return true;
1278
+ }
1279
+ if (options.dryRun === true) {
1280
+ logWould(options.logger, `add ${plans.map((plan) => plan.label).join(", ")}`);
1281
+ return true;
1282
+ }
1283
+ const buffered = [];
1284
+ const applyLogger = isInteractive() ? {
1285
+ error: (message) => {
1286
+ buffered.push({ level: "error", message });
1287
+ },
1288
+ info: () => {
1289
+ },
1290
+ success: () => {
1291
+ },
1292
+ warn: (message) => {
1293
+ buffered.push({ level: "warn", message });
1294
+ }
1295
+ } : options.logger;
1296
+ const steps = plans.map((plan) => {
1297
+ return {
1298
+ running: `adding ${plan.label}…`,
1299
+ task: () => runAddCommand({
1300
+ allowUnsafeSource: options.allowUnsafeSource,
1301
+ cwd: projectDirectory,
1302
+ from: options.registryFrom,
1303
+ logger: applyLogger,
1304
+ names: [...plan.names],
1305
+ ref: options.ref,
1306
+ source: options.registrySource,
1307
+ transformManifest: plan.transformManifest,
1308
+ yes: true
1309
+ })
1310
+ };
571
1311
  });
572
- return result.code === 0;
1312
+ const done = `added ${plans.map((plan) => plan.label).join(", ")}`;
1313
+ const results = await withTuiBadgeProgress(BADGES.add, steps, done);
1314
+ for (const { level, message } of buffered) {
1315
+ options.logger[level](message);
1316
+ }
1317
+ return results.every((result) => result.code === 0);
573
1318
  };
574
- await offerRegistryExtras({
575
- apply,
1319
+ const deps = {
1320
+ applyAll,
576
1321
  interactive,
577
1322
  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
- });
1323
+ multiSelect: options.prompt?.multiSelect ?? ((message, choices, settings) => tuiMultiSelect(message, choices, { ...settings, badge: BADGES.add })),
1324
+ preselected: preselected.length > 0 ? preselected : void 0,
1325
+ projectName: basename(projectDirectory),
1326
+ select: options.prompt?.select ?? ((message, choices, settings) => tuiSelect(message, choices, { ...settings, badge: BADGES.add })),
1327
+ text: options.prompt?.text ?? ((message, settings) => tuiText(message, { ...settings, badge: BADGES.add }))
1328
+ };
1329
+ if (preselected.length > 0) {
1330
+ await offerRegistryExtras(deps);
1331
+ return;
1332
+ }
1333
+ if (isInteractive()) {
1334
+ await tuiHeadline(COPY.extras);
1335
+ }
1336
+ await offerRegistryExtras(deps);
1337
+ };
1338
+ const DEFAULT_FRAMEWORK = "react";
1339
+ const FRAMEWORK_CHOICES = [
1340
+ { description: "React SPA — official create-vite base + the Lunora layer (the default)", label: "React", value: "react" },
1341
+ { description: "Vue SPA — create-vite base + Lunora", label: "Vue", value: "vue" },
1342
+ { description: "Solid SPA — create-vite base + Lunora", label: "Solid", value: "solid" },
1343
+ { description: "Svelte SPA — create-vite base + Lunora", label: "Svelte", value: "svelte" },
1344
+ { description: "TanStack Start (React) — SSR with live-loader routes", label: "TanStack Start · React", value: "tanstack-start-react" },
1345
+ { description: "TanStack Start (Solid)", label: "TanStack Start · Solid", value: "tanstack-start-solid" },
1346
+ { description: "React Router (v7, framework mode) — SSR composed into the Lunora worker", label: "React Router", value: "react-router" },
1347
+ { description: "Astro + a standalone Lunora worker", label: "Astro", value: "astro" },
1348
+ { description: "AnalogJS (Angular) — single-worker, Lunora mounted in Nitro", label: "Analog", value: "analog" },
1349
+ { description: "Nuxt (Vue) — single-worker, Lunora mounted in Nitro", label: "Nuxt", value: "nuxt" },
1350
+ { description: "SvelteKit + a standalone Lunora worker", label: "SvelteKit", value: "sveltekit" },
1351
+ { description: "Worker only — no frontend", label: "Standalone", value: "standalone" }
1352
+ ];
1353
+ const OVERLAY_VALUES = Object.keys(ADAPTERS).join("|");
1354
+ const TEMPLATE_VALUES = FRAMEWORK_CHOICES.filter((choice) => !isOverlayFramework(choice.value)).map((choice) => choice.value).join("|");
1355
+ const toScaffoldChoice = (value) => isOverlayFramework(value) ? { framework: value, kind: "overlay" } : { kind: "template", templateType: value };
1356
+ const resolveScaffoldChoice = async (options) => {
1357
+ if (options.vite !== void 0) {
1358
+ return { framework: options.vite, kind: "overlay" };
1359
+ }
1360
+ if (options.templateType !== void 0) {
1361
+ return { kind: "template", templateType: options.templateType };
1362
+ }
1363
+ if (!isInteractive() || options.yes === true) {
1364
+ return { framework: DEFAULT_FRAMEWORK, kind: "overlay" };
1365
+ }
1366
+ return toScaffoldChoice(await tuiSelect(COPY.framework, FRAMEWORK_CHOICES, { badge: BADGES.tmpl, default: DEFAULT_FRAMEWORK }) ?? DEFAULT_FRAMEWORK);
1367
+ };
1368
+ const nonInteractiveInitError = (options) => {
1369
+ if (isInteractive() || options.yes === true) {
1370
+ return void 0;
1371
+ }
1372
+ const missing = [];
1373
+ if (options.name === void 0) {
1374
+ missing.push("a project name (`lunora init <name>`)");
1375
+ }
1376
+ if (options.templateType === void 0 && options.vite === void 0) {
1377
+ missing.push(`a framework — \`--vite <${OVERLAY_VALUES}>\` for an SPA, or \`-t <${TEMPLATE_VALUES}>\` for a bespoke template`);
1378
+ }
1379
+ if (missing.length === 0) {
1380
+ return void 0;
1381
+ }
1382
+ return `lunora init can't prompt in a non-interactive terminal — provide ${missing.join(" and ")}, or pass --yes to accept the defaults.`;
1383
+ };
1384
+ const scaffoldOverlayPath = async (options, framework, name, target) => {
1385
+ if (!isOverlayFramework(framework)) {
1386
+ options.logger.error(`init: unknown framework "${framework}". Supported overlays: ${Object.keys(ADAPTERS).join(", ")}.`);
1387
+ return { code: 1, files: [], target };
1388
+ }
1389
+ if (!await verifyRemoteTemplate({ isLocal: options.overlayBaseFrom !== void 0, logger: options.logger })) {
1390
+ return { code: 1, files: [], target };
1391
+ }
1392
+ mkdirSync(target, { recursive: true });
1393
+ return scaffoldViteOverlay({ framework, logger: options.logger, name, overlayBaseFrom: options.overlayBaseFrom, target });
581
1394
  };
582
- const scaffoldNewProject = async (options, cwd) => {
583
- const name = options.name ?? "lunora-app";
584
- const templateType = options.templateType ?? "vite";
1395
+ const scaffoldTemplatePath = async (options, templateType, name, target) => {
585
1396
  if (templateType === "next") {
586
- options.logger.warn('template "next" is not yet available — re-run with `-t vite` or `-t standalone`.');
1397
+ options.logger.warn('template "next" is not yet available — re-run with `--vite react` or `-t standalone`.');
1398
+ return { code: 1, files: [], target };
1399
+ }
1400
+ if (options.from !== void 0) {
1401
+ return await scaffoldFromLocal(options.from, templateType, target, name, options.logger);
1402
+ }
1403
+ if (options.source !== void 0 && options.source.length > 0 && !options.allowUnsafeSource && !isSafeSource(options.source)) {
1404
+ options.logger.error(
1405
+ `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.`
1406
+ );
1407
+ return { code: 1, files: [], target };
1408
+ }
1409
+ if (!await verifyRemoteTemplate({ isLocal: false, logger: options.logger, source: resolveTemplateSource(templateType, options.source, options.ref) })) {
1410
+ return { code: 1, files: [], target };
1411
+ }
1412
+ return scaffoldFromRemote({ logger: options.logger, name, ref: options.ref, source: options.source, target, templateType });
1413
+ };
1414
+ const scaffoldNewProject = async (options, cwd, recordTarget) => {
1415
+ await tuiMoonrise("realtime backend on Cloudflare Workers + Durable Objects");
1416
+ const blocked = nonInteractiveInitError(options);
1417
+ if (blocked !== void 0) {
1418
+ options.logger.error(blocked);
587
1419
  return { code: 1, files: [], target: "" };
588
1420
  }
1421
+ const suggestedName = generateProjectName();
1422
+ const name = options.name ?? await tuiText(COPY.name, { badge: BADGES.dir, default: suggestedName, placeholder: suggestedName });
1423
+ const choice = await resolveScaffoldChoice(options);
589
1424
  if (name.includes("/") || name.includes("\\") || name === ".." || name === ".") {
590
1425
  options.logger.error(`init: refusing project name "${name}" — must not contain path separators or be "." / "..".`);
591
1426
  return { code: 1, files: [], target: "" };
592
1427
  }
593
1428
  const target = resolve(cwd, name);
594
- if (existsSync(target)) {
1429
+ const targetPreExisted = existsSync(target);
1430
+ if (targetPreExisted) {
595
1431
  const entries = readdirSync(target);
596
1432
  if (entries.length > 0) {
597
1433
  options.logger.error(`target directory not empty: ${target}`);
598
1434
  return { code: 1, files: [], target };
599
1435
  }
600
1436
  }
601
- if (options.from !== void 0) {
602
- return scaffoldFromLocal(options.from, templateType, target, name, options.logger);
1437
+ if (options.dryRun === true) {
1438
+ const what = choice.kind === "overlay" ? `the ${choice.framework} create-vite overlay` : `the ${choice.templateType} template`;
1439
+ logWould(options.logger, `scaffold ${what} into ${target}`);
1440
+ return { code: 0, files: [], target };
603
1441
  }
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 };
1442
+ recordTarget(target, targetPreExisted);
1443
+ return choice.kind === "overlay" ? scaffoldOverlayPath(options, choice.framework, name, target) : scaffoldTemplatePath(options, choice.templateType, name, target);
1444
+ };
1445
+ const resetScaffoldOnCancel = (cleanup, logger) => {
1446
+ const { target, targetPreExisted } = cleanup;
1447
+ if (target === void 0 || !existsSync(target)) {
1448
+ return;
1449
+ }
1450
+ if (targetPreExisted === true) {
1451
+ for (const entry of readdirSync(target)) {
1452
+ rmSync(join$1(target, entry), { force: true, recursive: true });
1453
+ }
1454
+ } else {
1455
+ rmSync(target, { force: true, recursive: true });
1456
+ }
1457
+ logger.info(`removed the partially-created project at ${target}`);
1458
+ };
1459
+ const runScaffoldStep = async (options, cwd, recordTarget) => {
1460
+ if (options.inPlace !== true) {
1461
+ return scaffoldNewProject(options, cwd, recordTarget);
1462
+ }
1463
+ if (options.dryRun === true) {
1464
+ logWould(options.logger, `configure Lunora into ${cwd}`);
1465
+ return { code: 0, files: [], target: cwd };
1466
+ }
1467
+ return runInPlaceInit(cwd, options.logger);
1468
+ };
1469
+ const runPostScaffold = async (options, result, cwd) => {
1470
+ await maybeOfferExtras(options, result.target);
1471
+ const installedManager = options.inPlace === true ? void 0 : await maybeOfferInstall(options, result.target);
1472
+ if (options.inPlace !== true) {
1473
+ await maybeOfferGit(options, result.target);
1474
+ await printNextSteps(basename(result.target), installedManager, isInsideMonorepo(cwd));
1475
+ await emitMascot(options.logger);
1476
+ }
1477
+ };
1478
+ const scaffoldCiPipeline = (options, result, cwd) => {
1479
+ if (result.code !== 0 || options.ci === void 0) {
1480
+ return;
1481
+ }
1482
+ if (options.dryRun === true) {
1483
+ logWould(options.logger, `scaffold a ${options.ci} CI deploy pipeline`);
1484
+ return;
609
1485
  }
610
- return scaffoldFromRemote(options.source, templateType, target, name, options.logger);
1486
+ scaffoldCiWorkflow(options.inPlace === true ? cwd : result.target, options.ci, options.logger);
611
1487
  };
612
1488
  const runInitCommand = async (options) => {
613
1489
  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);
1490
+ const cleanup = {};
1491
+ let result;
1492
+ try {
1493
+ result = await runScaffoldStep(options, cwd, (target, preExisted) => {
1494
+ cleanup.target = target;
1495
+ cleanup.targetPreExisted = preExisted;
1496
+ });
1497
+ if (result.code === 0 && result.target !== "") {
1498
+ cleanup.target = void 0;
1499
+ await runPostScaffold(options, result, cwd);
1500
+ }
1501
+ } catch (error) {
1502
+ if (error instanceof PromptCancelledError) {
1503
+ resetScaffoldOnCancel(cleanup, options.logger);
1504
+ process.stdout.write("\n ✖ Setup cancelled — run `lunora init` again whenever you're ready. 🌙\n");
1505
+ return { code: 130, files: [], target: "" };
1506
+ }
1507
+ throw error;
620
1508
  }
1509
+ scaffoldCiPipeline(options, result, cwd);
621
1510
  return result;
622
1511
  };
623
- const isTemplate = (value) => value === "astro" || value === "next" || value === "nuxt" || value === "standalone" || value === "sveltekit" || value === "tanstack-start-react" || value === "tanstack-start-solid" || value === "vite";
1512
+ 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
1513
  const resolveCiProvider = (raw, logger) => {
625
1514
  if (raw === void 0) {
626
1515
  return void 0;
@@ -632,21 +1521,24 @@ const resolveCiProvider = (raw, logger) => {
632
1521
  return void 0;
633
1522
  };
634
1523
  const execute = defineHandler(({ argument, cwd, logger, options }) => {
635
- const templateRaw = options.template ?? "vite";
636
- const template = isTemplate(templateRaw) ? templateRaw : "vite";
1524
+ const templateType = options.template !== void 0 && isTemplate(options.template) ? options.template : void 0;
637
1525
  return runInitCommand({
1526
+ add: options.add,
638
1527
  allowUnsafeSource: options.allowUnsafeSource === true,
639
1528
  cwd,
640
1529
  ci: resolveCiProvider(options.ci, logger),
1530
+ dryRun: options.dryRun === true,
641
1531
  from: options.from,
642
1532
  inPlace: options.here === true,
643
1533
  interactive: options.interactive === true ? true : void 0,
644
1534
  logger,
645
1535
  name: argument[0],
1536
+ ref: options.ref,
646
1537
  source: options.source,
647
- templateType: template,
1538
+ templateType,
1539
+ vite: options.vite,
648
1540
  yes: options.yes === true
649
1541
  });
650
1542
  });
651
1543
 
652
- export { execute, isTemplate, runInitCommand };
1544
+ export { execute, isTemplate, resolveTemplateSource, runInitCommand };