@lunora/cli 0.0.0 → 1.0.0-alpha.10

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 (75) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +109 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/bin.mjs +11 -0
  5. package/dist/index.d.mts +956 -0
  6. package/dist/index.d.ts +956 -0
  7. package/dist/index.mjs +19 -0
  8. package/dist/packem_chunks/handler.mjs +150 -0
  9. package/dist/packem_chunks/handler10.mjs +22 -0
  10. package/dist/packem_chunks/handler11.mjs +192 -0
  11. package/dist/packem_chunks/handler12.mjs +131 -0
  12. package/dist/packem_chunks/handler13.mjs +65 -0
  13. package/dist/packem_chunks/handler14.mjs +58 -0
  14. package/dist/packem_chunks/handler15.mjs +79 -0
  15. package/dist/packem_chunks/handler16.mjs +43 -0
  16. package/dist/packem_chunks/handler17.mjs +105 -0
  17. package/dist/packem_chunks/handler18.mjs +170 -0
  18. package/dist/packem_chunks/handler19.mjs +89 -0
  19. package/dist/packem_chunks/handler2.mjs +114 -0
  20. package/dist/packem_chunks/handler20.mjs +94 -0
  21. package/dist/packem_chunks/handler21.mjs +311 -0
  22. package/dist/packem_chunks/handler3.mjs +204 -0
  23. package/dist/packem_chunks/handler4.mjs +33 -0
  24. package/dist/packem_chunks/handler5.mjs +49 -0
  25. package/dist/packem_chunks/handler6.mjs +91 -0
  26. package/dist/packem_chunks/handler7.mjs +42 -0
  27. package/dist/packem_chunks/handler8.mjs +174 -0
  28. package/dist/packem_chunks/handler9.mjs +16 -0
  29. package/dist/packem_chunks/planDevCommand.mjs +500 -0
  30. package/dist/packem_chunks/runCodegenCommand.mjs +52 -0
  31. package/dist/packem_chunks/runDeployCommand.mjs +504 -0
  32. package/dist/packem_chunks/runInitCommand.mjs +1498 -0
  33. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +397 -0
  34. package/dist/packem_chunks/runResetCommand.mjs +41 -0
  35. package/dist/packem_chunks/runRpcCommand.mjs +68 -0
  36. package/dist/packem_shared/COMMANDS-D3h9Iwvl.mjs +944 -0
  37. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +244 -0
  38. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +19 -0
  39. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +13 -0
  40. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +38 -0
  41. package/dist/packem_shared/command-BC30oSBW.mjs +14 -0
  42. package/dist/packem_shared/commands-DPKWlqqX.mjs +812 -0
  43. package/dist/packem_shared/createLogger-B40gPzQo.mjs +78 -0
  44. package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +43 -0
  45. package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +61 -0
  46. package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +161 -0
  47. package/dist/packem_shared/docker-hMQ97KSQ.mjs +21 -0
  48. package/dist/packem_shared/insertSchemaExtension-BuzF6-t2.mjs +59 -0
  49. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +41 -0
  50. package/dist/packem_shared/output-format-wUvAN6AL.mjs +17 -0
  51. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +56 -0
  52. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +94 -0
  53. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +16 -0
  54. package/dist/packem_shared/runAddCommand-CTRA_JlL.mjs +4 -0
  55. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +79 -0
  56. package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +43 -0
  57. package/dist/packem_shared/storage-2RJBhUC4.mjs +84 -0
  58. package/dist/packem_shared/tui-prompts-DEiPCKV-.mjs +661 -0
  59. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +12 -0
  60. package/package.json +62 -18
  61. package/skills/README.md +29 -0
  62. package/skills/lunora/SKILL.md +83 -0
  63. package/skills/lunora-create-package/SKILL.md +129 -0
  64. package/skills/lunora-deploy/SKILL.md +150 -0
  65. package/skills/lunora-functions/SKILL.md +182 -0
  66. package/skills/lunora-migration-helper/SKILL.md +194 -0
  67. package/skills/lunora-performance-audit/SKILL.md +143 -0
  68. package/skills/lunora-quickstart/SKILL.md +240 -0
  69. package/skills/lunora-realtime/SKILL.md +177 -0
  70. package/skills/lunora-setup-auth/SKILL.md +170 -0
  71. package/skills/lunora-setup-hyperdrive/SKILL.md +154 -0
  72. package/skills/lunora-setup-hyperdrive-global/SKILL.md +171 -0
  73. package/skills/lunora-setup-mail/SKILL.md +151 -0
  74. package/skills/lunora-setup-scheduler/SKILL.md +157 -0
  75. package/skills/lunora-setup-storage/SKILL.md +158 -0
@@ -0,0 +1,1498 @@
1
+ import { existsSync, mkdirSync, writeFileSync, readFileSync, readdirSync, rmSync, mkdtempSync, cpSync, renameSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { detectFramework as detectFramework$1, isInteractive, LUNA_ART, LUNA_NAME, LUNA_SIGNOFF, paintAnswer, BADGES } from '@lunora/config';
4
+ import { walkSync } from '@visulima/fs';
5
+ import { join as join$1, dirname as dirname$1, basename, resolve, relative } from '@visulima/path';
6
+ import { downloadTemplate } from 'giget';
7
+ import { modify, applyEdits } from 'jsonc-parser';
8
+ import { join, dirname } from 'node:path';
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';
11
+ import MagicString from 'magic-string';
12
+ import { Project, SyntaxKind } from 'ts-morph';
13
+ import { c as resolveSourceRef, d as resolveDistTag, r as runAddCommand } from '../packem_shared/commands-DPKWlqqX.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-DEiPCKV-.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-2RJBhUC4.mjs';
18
+ import dns from 'node:dns/promises';
19
+
20
+ const GITHUB_CONTENT = `name: Deploy
21
+
22
+ on:
23
+ push:
24
+ branches: [main]
25
+ pull_request:
26
+ workflow_dispatch:
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
+ #
32
+ # Set these repository secrets (Settings → Secrets and variables → Actions):
33
+ # CLOUDFLARE_API_TOKEN — a Workers-scoped API token
34
+ # CLOUDFLARE_ACCOUNT_ID — your Cloudflare account id
35
+ env:
36
+ CLOUDFLARE_API_TOKEN: \${{ secrets.CLOUDFLARE_API_TOKEN }}
37
+ CLOUDFLARE_ACCOUNT_ID: \${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
38
+
39
+ jobs:
40
+ # Production deploy on push to the default branch.
41
+ deploy:
42
+ if: github.event_name != 'pull_request'
43
+ runs-on: ubuntu-latest
44
+ permissions:
45
+ contents: read
46
+ steps:
47
+ - uses: actions/checkout@v4
48
+ - uses: pnpm/action-setup@v4
49
+ - uses: actions/setup-node@v4
50
+ with:
51
+ node-version: 22
52
+ cache: pnpm
53
+ - run: pnpm install --frozen-lockfile
54
+ # Codegen + wrangler validation gate (no deploy) — fails fast on drift.
55
+ - run: pnpm exec lunora prepare
56
+ - run: pnpm exec lunora deploy
57
+
58
+ # Preview version on every pull request — uploads a versioned preview URL;
59
+ # production traffic is untouched.
60
+ preview:
61
+ if: github.event_name == 'pull_request'
62
+ runs-on: ubuntu-latest
63
+ permissions:
64
+ contents: read
65
+ steps:
66
+ - uses: actions/checkout@v4
67
+ - uses: pnpm/action-setup@v4
68
+ - uses: actions/setup-node@v4
69
+ with:
70
+ node-version: 22
71
+ cache: pnpm
72
+ - run: pnpm install --frozen-lockfile
73
+ - run: pnpm exec lunora deploy --preview
74
+ `;
75
+ const GITLAB_CONTENT = `stages:
76
+ - deploy
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
+ #
82
+ # Set these as masked CI/CD variables (Settings → CI/CD → Variables):
83
+ # CLOUDFLARE_API_TOKEN — a Workers-scoped API token
84
+ # CLOUDFLARE_ACCOUNT_ID — your Cloudflare account id
85
+ .lunora_base:
86
+ image: node:22
87
+ stage: deploy
88
+ before_script:
89
+ - corepack enable
90
+ - pnpm install --frozen-lockfile
91
+
92
+ # Production deploy on the default branch.
93
+ deploy:
94
+ extends: .lunora_base
95
+ rules:
96
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
97
+ script:
98
+ # Codegen + wrangler validation gate (no deploy) — fails fast on drift.
99
+ - pnpm exec lunora prepare
100
+ - pnpm exec lunora deploy
101
+
102
+ # Preview version on every merge request (versioned preview URL; production
103
+ # traffic is untouched).
104
+ preview:
105
+ extends: .lunora_base
106
+ rules:
107
+ - if: $CI_PIPELINE_SOURCE == "merge_request_event"
108
+ script:
109
+ - pnpm exec lunora deploy --preview
110
+ `;
111
+ const WORKFLOWS = {
112
+ github: {
113
+ content: GITHUB_CONTENT,
114
+ file: join(".github", "workflows", "deploy.yml"),
115
+ secretsHint: "repository secrets (Settings → Secrets and variables → Actions)"
116
+ },
117
+ gitlab: {
118
+ content: GITLAB_CONTENT,
119
+ file: ".gitlab-ci.yml",
120
+ secretsHint: "masked CI/CD variables (Settings → CI/CD → Variables)"
121
+ }
122
+ };
123
+ const isCiProvider = (value) => value === "github" || value === "gitlab";
124
+ const writeCiWorkflow = (projectRoot, provider, options = {}) => {
125
+ const spec = WORKFLOWS[provider];
126
+ const path = join(projectRoot, spec.file);
127
+ if (existsSync(path) && options.overwrite !== true) {
128
+ return { path, skipped: true, written: false };
129
+ }
130
+ mkdirSync(dirname(path), { recursive: true });
131
+ writeFileSync(path, spec.content, "utf8");
132
+ return { path, skipped: false, written: true };
133
+ };
134
+ const scaffoldCiWorkflow = (projectRoot, provider, logger, options = {}) => {
135
+ const spec = WORKFLOWS[provider];
136
+ try {
137
+ const result = writeCiWorkflow(projectRoot, provider, options);
138
+ if (result.skipped) {
139
+ logger.info(`--ci ${provider}: ${spec.file} already exists — left unchanged (re-run with overwrite to replace).`);
140
+ } else {
141
+ logger.success(`--ci ${provider}: wrote ${spec.file}`);
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
+ );
146
+ }
147
+ return result;
148
+ } catch (error) {
149
+ const message = error instanceof Error ? error.message : String(error);
150
+ logger.warn(`--ci ${provider}: could not write ${spec.file} (${message})`);
151
+ return { path: join(projectRoot, spec.file), skipped: false, written: false };
152
+ }
153
+ };
154
+
155
+ const ADAPTER_BY_FRAMEWORK = {
156
+ astro: "@lunora/react",
157
+ none: "@lunora/react",
158
+ nuxt: "@lunora/vue",
159
+ "react-router": "@lunora/react",
160
+ "solid-start": "@lunora/solid",
161
+ sveltekit: "@lunora/svelte",
162
+ "tanstack-start": "@lunora/react",
163
+ "tanstack-start-solid": "@lunora/solid"
164
+ };
165
+ const detectFramework = (root) => {
166
+ const base = detectFramework$1(root);
167
+ return { ...base, adapter: ADAPTER_BY_FRAMEWORK[base.framework] };
168
+ };
169
+
170
+ const LUNORA_CALL = "lunora()";
171
+ const LUNORA_IMPORT = 'import { lunora } from "@lunora/vite";';
172
+ const LUNORA_CALL_RE = /\blunora\s*\(/u;
173
+ const LUNORA_VITE_DOUBLE = '"@lunora/vite"';
174
+ const LUNORA_VITE_SINGLE = "'@lunora/vite'";
175
+ const findDefineConfigObject = (sf) => {
176
+ for (const statement of sf.getStatements()) {
177
+ if (statement.getKind() !== SyntaxKind.ExportAssignment) {
178
+ continue;
179
+ }
180
+ const expr = statement.asKindOrThrow(SyntaxKind.ExportAssignment).getExpression();
181
+ if (expr.getKind() !== SyntaxKind.CallExpression) {
182
+ continue;
183
+ }
184
+ const call = expr.asKindOrThrow(SyntaxKind.CallExpression);
185
+ if (call.getExpression().getText() !== "defineConfig") {
186
+ continue;
187
+ }
188
+ const argument = call.getArguments()[0];
189
+ if (argument?.getKind() === SyntaxKind.ObjectLiteralExpression) {
190
+ return argument.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
191
+ }
192
+ }
193
+ return void 0;
194
+ };
195
+ const findPlainExportObject = (sf) => {
196
+ for (const statement of sf.getStatements()) {
197
+ if (statement.getKind() !== SyntaxKind.ExportAssignment) {
198
+ continue;
199
+ }
200
+ const expr = statement.asKindOrThrow(SyntaxKind.ExportAssignment).getExpression();
201
+ if (expr.getKind() === SyntaxKind.ObjectLiteralExpression) {
202
+ return expr.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
203
+ }
204
+ }
205
+ return void 0;
206
+ };
207
+ const findConfigObject = (sourceText) => {
208
+ const project = new Project({
209
+ compilerOptions: { allowJs: true },
210
+ useInMemoryFileSystem: true
211
+ });
212
+ const sf = project.createSourceFile("vite.config.ts", sourceText, { overwrite: true });
213
+ return findDefineConfigObject(sf) ?? findPlainExportObject(sf);
214
+ };
215
+ const importInsertPosition = (sourceText) => {
216
+ const project = new Project({
217
+ compilerOptions: { allowJs: true },
218
+ useInMemoryFileSystem: true
219
+ });
220
+ const sf = project.createSourceFile("vite.config.ts", sourceText, { overwrite: true });
221
+ const imports = sf.getImportDeclarations();
222
+ if (imports.length === 0) {
223
+ return 0;
224
+ }
225
+ const last = imports[imports.length - 1];
226
+ if (last === void 0) {
227
+ return 0;
228
+ }
229
+ return last.getEnd();
230
+ };
231
+ const addImport = (ms, source) => {
232
+ const insertAt = importInsertPosition(source);
233
+ if (insertAt === 0) {
234
+ ms.prepend(`${LUNORA_IMPORT}
235
+ `);
236
+ } else {
237
+ ms.appendLeft(insertAt, `
238
+ ${LUNORA_IMPORT}`);
239
+ }
240
+ };
241
+ const patchPluginsArray = (ms, configObject) => {
242
+ const pluginsProp = configObject.getProperty("plugins");
243
+ if (pluginsProp === void 0) {
244
+ const properties = configObject.getProperties();
245
+ const openBrace = configObject.getStart() + 1;
246
+ if (properties.length === 0) {
247
+ ms.appendLeft(openBrace, ` plugins: [${LUNORA_CALL}] `);
248
+ } else {
249
+ const firstProp = properties[0];
250
+ if (firstProp !== void 0) {
251
+ ms.appendLeft(firstProp.getStart(), `plugins: [${LUNORA_CALL}],
252
+ `);
253
+ }
254
+ }
255
+ return;
256
+ }
257
+ const arrayLit = pluginsProp.getDescendantsOfKind(SyntaxKind.ArrayLiteralExpression)[0];
258
+ if (arrayLit === void 0) {
259
+ return;
260
+ }
261
+ const elements = arrayLit.getElements();
262
+ if (elements.length === 0) {
263
+ ms.appendLeft(arrayLit.getStart() + 1, LUNORA_CALL);
264
+ } else {
265
+ const firstElement = elements[0];
266
+ if (firstElement !== void 0) {
267
+ ms.appendLeft(firstElement.getStart(), `${LUNORA_CALL}, `);
268
+ }
269
+ }
270
+ };
271
+ const patchViteConfig = (source) => {
272
+ if (LUNORA_CALL_RE.test(source)) {
273
+ return { changed: false, code: source, reason: "lunora plugin already present" };
274
+ }
275
+ const configObject = findConfigObject(source);
276
+ if (configObject === void 0) {
277
+ return { changed: false, code: source, reason: "could not locate a Vite config plugins array to patch" };
278
+ }
279
+ const ms = new MagicString(source);
280
+ const alreadyImported = source.includes(LUNORA_VITE_DOUBLE) || source.includes(LUNORA_VITE_SINGLE);
281
+ if (alreadyImported) {
282
+ patchPluginsArray(ms, configObject);
283
+ } else {
284
+ addImport(ms, source);
285
+ patchPluginsArray(ms, configObject);
286
+ }
287
+ return { changed: true, code: ms.toString() };
288
+ };
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
+
314
+ const STACK_FEATURE_OPTIONS = [
315
+ { description: "Sign-up / sign-in (asks which provider)", label: "Authentication", value: "auth" },
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" }
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
+ };
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
+ }
372
+ if (!deps.interactive) {
373
+ deps.logger.info("tip: add features later with `lunora add <auth|email|storage|ratelimit|crons|presence|backup>`.");
374
+ return;
375
+ }
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 = [];
381
+ for (const feature of picked) {
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;
718
+ }
719
+ return response.ok ? true : void 0;
720
+ } catch {
721
+ return void 0;
722
+ }
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
+ };
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
+ };
752
+ const TEXT_EXTENSIONS = /* @__PURE__ */ new Set([".gitignore", ".html", ".js", ".json", ".jsonc", ".md", ".mjs", ".ts", ".tsx"]);
753
+ const VITE_CONFIG_CANDIDATES = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"];
754
+ const MINIMAL_VITE_CONFIG = `import { defineConfig } from "vite";
755
+ import { lunora } from "@lunora/vite";
756
+
757
+ export default defineConfig({ plugins: [lunora()] });
758
+ `;
759
+ const SAMPLE_SCHEMA = `import { defineSchema, defineTable, v } from "@lunora/server";
760
+
761
+ export default defineSchema({
762
+ messages: defineTable({
763
+ channelId: v.id("channels"),
764
+ text: v.string(),
765
+ })
766
+ .shardBy("channelId")
767
+ .index("by_channel", ["channelId"]),
768
+ });
769
+ `;
770
+ const SAMPLE_FUNCTION = `import { mutation, query, v } from "./_generated/server";
771
+
772
+ export const list = query
773
+ .input({ channelId: v.id("channels"), limit: v.optional(v.number()) })
774
+ .query(async ({ args }) => {
775
+ return { channelId: args.channelId, limit: args.limit ?? 50, messages: [] };
776
+ });
777
+
778
+ export const send = mutation
779
+ .input({ channelId: v.id("channels"), text: v.string() })
780
+ .mutation(async ({ args }) => {
781
+ return { channelId: args.channelId, text: args.text };
782
+ });
783
+ `;
784
+ const DEFAULT_SOURCE_BASE = "gh:anolilab/lunora/templates";
785
+ const isTextFile = (filePath) => {
786
+ const lastDot = filePath.lastIndexOf(".");
787
+ if (lastDot === -1) {
788
+ return false;
789
+ }
790
+ return TEXT_EXTENSIONS.has(filePath.slice(lastDot));
791
+ };
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
+ };
813
+ const collectFiles = (directory) => {
814
+ const out = [];
815
+ for (const entry of walkSync(directory, { includeDirs: false, includeFiles: true })) {
816
+ out.push(entry.path);
817
+ }
818
+ return out;
819
+ };
820
+ const copyTemplate = (sourceDirectory, target, name) => {
821
+ const files = collectFiles(sourceDirectory);
822
+ const written = [];
823
+ const distTag = resolveDistTag();
824
+ for (const source of files) {
825
+ const relativePath = relative(sourceDirectory, source);
826
+ const destination = join$1(target, relativePath);
827
+ mkdirSync(dirname$1(destination), { recursive: true });
828
+ const raw = readFileSync(source);
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
+ }
833
+ if (text === void 0) {
834
+ writeFileSync(destination, raw);
835
+ } else {
836
+ writeFileSync(destination, text, "utf8");
837
+ }
838
+ written.push(destination);
839
+ }
840
+ return written;
841
+ };
842
+ const resolveTemplateSource = (templateType, source, ref) => {
843
+ if (source !== void 0 && source.length > 0) {
844
+ return source;
845
+ }
846
+ return `${DEFAULT_SOURCE_BASE}/${templateType}#${resolveSourceRef(ref)}`;
847
+ };
848
+ const isSafeSource = (source) => {
849
+ if (source.includes("..")) {
850
+ return false;
851
+ }
852
+ return source.startsWith("gh:") || source.startsWith("github:") || source.startsWith("https://");
853
+ };
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
+ }
861
+ logger.success(`scaffolded ${String(written.length)} files into ${target}`);
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;
991
+ };
992
+ const scaffoldFromLocal = (fromRoot, templateType, target, name, logger) => {
993
+ const templateDirectory = join$1(fromRoot, templateType);
994
+ if (!existsSync(templateDirectory)) {
995
+ logger.error(`template not found in local source: ${templateDirectory}`);
996
+ return { code: 1, files: [], target };
997
+ }
998
+ const written = copyTemplate(templateDirectory, target, name);
999
+ logScaffoldSuccess(logger, written, target);
1000
+ return { code: 0, files: written, target };
1001
+ };
1002
+ const scaffoldFromRemote = async (options) => {
1003
+ const { logger, name, ref, source, target, templateType } = options;
1004
+ const stagingRoot = mkdtempSync(join$1(tmpdir(), "lunora-init-fetch-"));
1005
+ const stagingDirectory = join$1(stagingRoot, "template");
1006
+ try {
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
+ );
1034
+ const staged = collectFiles(stagingDirectory);
1035
+ if (isInteractive()) {
1036
+ process.stdout.write("\n");
1037
+ }
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);
1042
+ return { code: 0, files: written, target };
1043
+ } catch (error) {
1044
+ if (error instanceof PromptCancelledError) {
1045
+ throw error;
1046
+ }
1047
+ const message = error instanceof Error ? error.message : String(error);
1048
+ logger.error(`failed to download template: ${message}`);
1049
+ return { code: 1, files: [], target };
1050
+ } finally {
1051
+ rmSync(stagingRoot, { force: true, recursive: true });
1052
+ }
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
+ };
1113
+ const createMinimalViteConfig = (cwd, logger) => {
1114
+ const target = join$1(cwd, "vite.config.ts");
1115
+ try {
1116
+ writeFileSync(target, MINIMAL_VITE_CONFIG, "utf8");
1117
+ } catch (error) {
1118
+ const message = error instanceof Error ? error.message : String(error);
1119
+ logger.error(`init --in-place: could not write ${target}: ${message}`);
1120
+ return { code: 1, files: [], target: cwd };
1121
+ }
1122
+ logger.success(`created ${target} with lunora() plugin`);
1123
+ return { code: 0, files: [target], target: cwd };
1124
+ };
1125
+ const patchExistingViteConfig = (viteConfigPath, cwd, logger) => {
1126
+ let source;
1127
+ try {
1128
+ source = readFileSync(viteConfigPath, "utf8");
1129
+ } catch (error) {
1130
+ const message = error instanceof Error ? error.message : String(error);
1131
+ logger.error(`init --in-place: could not read ${viteConfigPath}: ${message}`);
1132
+ return { code: 1, files: [], target: cwd };
1133
+ }
1134
+ const result = patchViteConfig(source);
1135
+ if (!result.changed) {
1136
+ logger.info(`${viteConfigPath}: ${result.reason ?? "no changes needed"}`);
1137
+ return { code: 0, files: [], target: cwd };
1138
+ }
1139
+ try {
1140
+ writeFileSync(viteConfigPath, result.code, "utf8");
1141
+ } catch (error) {
1142
+ const message = error instanceof Error ? error.message : String(error);
1143
+ logger.error(`init --in-place: could not write ${viteConfigPath}: ${message}`);
1144
+ return { code: 1, files: [], target: cwd };
1145
+ }
1146
+ logger.success(`patched ${viteConfigPath} — added lunora() plugin`);
1147
+ return { code: 0, files: [viteConfigPath], target: cwd };
1148
+ };
1149
+ const scaffoldLunoraDirectory = (cwd, logger) => {
1150
+ const lunoraDirectory = join$1(cwd, "lunora");
1151
+ const schemaPath = join$1(lunoraDirectory, "schema.ts");
1152
+ if (existsSync(schemaPath)) {
1153
+ logger.info(`lunora/ already present — left ${schemaPath} untouched`);
1154
+ return [];
1155
+ }
1156
+ const written = [];
1157
+ try {
1158
+ mkdirSync(lunoraDirectory, { recursive: true });
1159
+ writeFileSync(schemaPath, SAMPLE_SCHEMA, "utf8");
1160
+ written.push(schemaPath);
1161
+ const functionPath = join$1(lunoraDirectory, "messages.ts");
1162
+ if (!existsSync(functionPath)) {
1163
+ writeFileSync(functionPath, SAMPLE_FUNCTION, "utf8");
1164
+ written.push(functionPath);
1165
+ }
1166
+ logger.success(`scaffolded lunora/ (${String(written.length)} file(s))`);
1167
+ } catch (error) {
1168
+ const message = error instanceof Error ? error.message : String(error);
1169
+ logger.error(`init --here: could not scaffold lunora/: ${message}`);
1170
+ }
1171
+ return written;
1172
+ };
1173
+ const printFrameworkNextSteps = (detection, logger) => {
1174
+ const { adapter, class: frameworkClass, framework } = detection;
1175
+ logger.info("");
1176
+ logger.info(`detected framework: ${framework} (class ${frameworkClass})`);
1177
+ logger.info("next steps:");
1178
+ logger.info(` 1. install the adapter: pnpm add ${adapter} @lunora/client @lunora/runtime @lunora/server`);
1179
+ logger.info(" 2. run codegen: lunora codegen");
1180
+ if (frameworkClass === "A") {
1181
+ logger.info(" 3. compose one worker: wrap your worker entry with");
1182
+ logger.info(" createWorker({ httpRouter: <your framework SSR handler>, shardDO: ShardDO, ... })");
1183
+ logger.info(` 4. add the provider: mount the ${adapter} provider in your root layout/route`);
1184
+ logger.info(" 5. make a loader live: preloadQuery() in a loader, usePreloadedQuery() in the component");
1185
+ logger.info(" see https://lunora.sh/docs/frameworks/reactive-loaders");
1186
+ } else if (frameworkClass === "B") {
1187
+ logger.info(" 3. inject Lunora: mount Lunora realtime under /_lunora/* in your server hook");
1188
+ logger.info(` (${framework} owns its Cloudflare adapter — Lunora composes into its server entry)`);
1189
+ logger.info(` 4. add the provider: mount the ${adapter} provider in your root layout`);
1190
+ logger.info(" 5. read the guide: https://lunora.sh/docs/frameworks/deploy");
1191
+ } else {
1192
+ logger.info(" 3. add the provider: wrap your app with the LunoraProvider from @lunora/react");
1193
+ logger.info(" 4. read the guide: https://lunora.sh/docs/frameworks/bring-your-framework");
1194
+ }
1195
+ logger.info("");
1196
+ };
1197
+ const findExistingViteConfig = (cwd) => {
1198
+ for (const candidate of VITE_CONFIG_CANDIDATES) {
1199
+ const full = join$1(cwd, candidate);
1200
+ if (existsSync(full)) {
1201
+ return full;
1202
+ }
1203
+ }
1204
+ return void 0;
1205
+ };
1206
+ const patchOrCreateViteConfig = (cwd, framework, logger) => {
1207
+ const viteConfigPath = findExistingViteConfig(cwd);
1208
+ if (viteConfigPath === void 0) {
1209
+ if (framework === "sveltekit" || framework === "nuxt" || framework === "astro") {
1210
+ logger.info(`no Vite config found — ${framework} wires Lunora through its server entry (see next steps)`);
1211
+ return { code: 0, files: [], target: cwd };
1212
+ }
1213
+ return createMinimalViteConfig(cwd, logger);
1214
+ }
1215
+ return patchExistingViteConfig(viteConfigPath, cwd, logger);
1216
+ };
1217
+ const runInPlaceInit = (cwd, logger) => {
1218
+ const detection = detectFramework(cwd);
1219
+ const viteResult = patchOrCreateViteConfig(cwd, detection.framework, logger);
1220
+ if (viteResult.code !== 0) {
1221
+ return viteResult;
1222
+ }
1223
+ const scaffolded = scaffoldLunoraDirectory(cwd, logger);
1224
+ printFrameworkNextSteps(detection, logger);
1225
+ return { code: 0, files: [...viteResult.files, ...scaffolded], target: cwd };
1226
+ };
1227
+ const offerIsInteractive = (options) => options.yes !== true && (options.prompt !== void 0 || (options.interactive ?? isInteractive()));
1228
+ const maybeOfferExtras = async (options, projectDirectory) => {
1229
+ const interactive = offerIsInteractive(options);
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 applyLogger = isInteractive() ? {
1242
+ error: (message) => {
1243
+ options.logger.error(message);
1244
+ },
1245
+ info: () => {
1246
+ },
1247
+ success: () => {
1248
+ },
1249
+ warn: (message) => {
1250
+ options.logger.warn(message);
1251
+ }
1252
+ } : options.logger;
1253
+ const steps = plans.map((plan) => {
1254
+ return {
1255
+ running: `adding ${plan.label}…`,
1256
+ task: () => runAddCommand({
1257
+ allowUnsafeSource: options.allowUnsafeSource,
1258
+ cwd: projectDirectory,
1259
+ from: options.registryFrom,
1260
+ logger: applyLogger,
1261
+ names: [...plan.names],
1262
+ ref: options.ref,
1263
+ source: options.registrySource,
1264
+ transformManifest: plan.transformManifest,
1265
+ yes: true
1266
+ })
1267
+ };
1268
+ });
1269
+ const done = `added ${plans.map((plan) => plan.label).join(", ")}`;
1270
+ const results = await withTuiBadgeProgress(BADGES.add, steps, done);
1271
+ return results.every((result) => result.code === 0);
1272
+ };
1273
+ const deps = {
1274
+ applyAll,
1275
+ interactive,
1276
+ logger: options.logger,
1277
+ multiSelect: options.prompt?.multiSelect ?? ((message, choices, settings) => tuiMultiSelect(message, choices, { ...settings, badge: BADGES.add })),
1278
+ preselected: preselected.length > 0 ? preselected : void 0,
1279
+ projectName: basename(projectDirectory),
1280
+ select: options.prompt?.select ?? ((message, choices, settings) => tuiSelect(message, choices, { ...settings, badge: BADGES.add })),
1281
+ text: options.prompt?.text ?? ((message, settings) => tuiText(message, { ...settings, badge: BADGES.add }))
1282
+ };
1283
+ if (preselected.length > 0) {
1284
+ await offerRegistryExtras(deps);
1285
+ return;
1286
+ }
1287
+ if (isInteractive()) {
1288
+ await tuiHeadline(COPY.extras);
1289
+ }
1290
+ await offerRegistryExtras(deps);
1291
+ };
1292
+ const DEFAULT_FRAMEWORK = "react";
1293
+ const FRAMEWORK_CHOICES = [
1294
+ { description: "React SPA — official create-vite base + the Lunora layer (the default)", label: "React", value: "react" },
1295
+ { description: "Vue SPA — create-vite base + Lunora", label: "Vue", value: "vue" },
1296
+ { description: "Solid SPA — create-vite base + Lunora", label: "Solid", value: "solid" },
1297
+ { description: "Svelte SPA — create-vite base + Lunora", label: "Svelte", value: "svelte" },
1298
+ { description: "TanStack Start (React) — SSR with live-loader routes", label: "TanStack Start · React", value: "tanstack-start-react" },
1299
+ { description: "TanStack Start (Solid)", label: "TanStack Start · Solid", value: "tanstack-start-solid" },
1300
+ { description: "React Router (v7, framework mode) — SSR composed into the Lunora worker", label: "React Router", value: "react-router" },
1301
+ { description: "Astro + a standalone Lunora worker", label: "Astro", value: "astro" },
1302
+ { description: "AnalogJS (Angular) — single-worker, Lunora mounted in Nitro", label: "Analog", value: "analog" },
1303
+ { description: "Nuxt (Vue) — single-worker, Lunora mounted in Nitro", label: "Nuxt", value: "nuxt" },
1304
+ { description: "SvelteKit + a standalone Lunora worker", label: "SvelteKit", value: "sveltekit" },
1305
+ { description: "Worker only — no frontend", label: "Standalone", value: "standalone" }
1306
+ ];
1307
+ const OVERLAY_VALUES = Object.keys(ADAPTERS).join("|");
1308
+ const TEMPLATE_VALUES = FRAMEWORK_CHOICES.filter((choice) => !isOverlayFramework(choice.value)).map((choice) => choice.value).join("|");
1309
+ const toScaffoldChoice = (value) => isOverlayFramework(value) ? { framework: value, kind: "overlay" } : { kind: "template", templateType: value };
1310
+ const resolveScaffoldChoice = async (options) => {
1311
+ if (options.vite !== void 0) {
1312
+ return { framework: options.vite, kind: "overlay" };
1313
+ }
1314
+ if (options.templateType !== void 0) {
1315
+ return { kind: "template", templateType: options.templateType };
1316
+ }
1317
+ if (!isInteractive() || options.yes === true) {
1318
+ return { framework: DEFAULT_FRAMEWORK, kind: "overlay" };
1319
+ }
1320
+ return toScaffoldChoice(await tuiSelect(COPY.framework, FRAMEWORK_CHOICES, { badge: BADGES.tmpl, default: DEFAULT_FRAMEWORK }) ?? DEFAULT_FRAMEWORK);
1321
+ };
1322
+ const nonInteractiveInitError = (options) => {
1323
+ if (isInteractive() || options.yes === true) {
1324
+ return void 0;
1325
+ }
1326
+ const missing = [];
1327
+ if (options.name === void 0) {
1328
+ missing.push("a project name (`lunora init <name>`)");
1329
+ }
1330
+ if (options.templateType === void 0 && options.vite === void 0) {
1331
+ missing.push(`a framework — \`--vite <${OVERLAY_VALUES}>\` for an SPA, or \`-t <${TEMPLATE_VALUES}>\` for a bespoke template`);
1332
+ }
1333
+ if (missing.length === 0) {
1334
+ return void 0;
1335
+ }
1336
+ return `lunora init can't prompt in a non-interactive terminal — provide ${missing.join(" and ")}, or pass --yes to accept the defaults.`;
1337
+ };
1338
+ const scaffoldOverlayPath = async (options, framework, name, target) => {
1339
+ if (!isOverlayFramework(framework)) {
1340
+ options.logger.error(`init: unknown framework "${framework}". Supported overlays: ${Object.keys(ADAPTERS).join(", ")}.`);
1341
+ return { code: 1, files: [], target };
1342
+ }
1343
+ if (!await verifyRemoteTemplate({ isLocal: options.overlayBaseFrom !== void 0, logger: options.logger })) {
1344
+ return { code: 1, files: [], target };
1345
+ }
1346
+ mkdirSync(target, { recursive: true });
1347
+ return scaffoldViteOverlay({ framework, logger: options.logger, name, overlayBaseFrom: options.overlayBaseFrom, target });
1348
+ };
1349
+ const scaffoldTemplatePath = async (options, templateType, name, target) => {
1350
+ if (templateType === "next") {
1351
+ options.logger.warn('template "next" is not yet available — re-run with `--vite react` or `-t standalone`.');
1352
+ return { code: 1, files: [], target };
1353
+ }
1354
+ if (options.from !== void 0) {
1355
+ return scaffoldFromLocal(options.from, templateType, target, name, options.logger);
1356
+ }
1357
+ if (options.source !== void 0 && options.source.length > 0 && !options.allowUnsafeSource && !isSafeSource(options.source)) {
1358
+ options.logger.error(
1359
+ `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.`
1360
+ );
1361
+ return { code: 1, files: [], target };
1362
+ }
1363
+ if (!await verifyRemoteTemplate({ isLocal: false, logger: options.logger, source: resolveTemplateSource(templateType, options.source, options.ref) })) {
1364
+ return { code: 1, files: [], target };
1365
+ }
1366
+ return scaffoldFromRemote({ logger: options.logger, name, ref: options.ref, source: options.source, target, templateType });
1367
+ };
1368
+ const scaffoldNewProject = async (options, cwd, recordTarget) => {
1369
+ await tuiMoonrise("realtime backend on Cloudflare Workers + Durable Objects");
1370
+ const blocked = nonInteractiveInitError(options);
1371
+ if (blocked !== void 0) {
1372
+ options.logger.error(blocked);
1373
+ return { code: 1, files: [], target: "" };
1374
+ }
1375
+ const suggestedName = generateProjectName();
1376
+ const name = options.name ?? await tuiText(COPY.name, { badge: BADGES.dir, default: suggestedName, placeholder: suggestedName });
1377
+ const choice = await resolveScaffoldChoice(options);
1378
+ if (name.includes("/") || name.includes("\\") || name === ".." || name === ".") {
1379
+ options.logger.error(`init: refusing project name "${name}" — must not contain path separators or be "." / "..".`);
1380
+ return { code: 1, files: [], target: "" };
1381
+ }
1382
+ const target = resolve(cwd, name);
1383
+ const targetPreExisted = existsSync(target);
1384
+ if (targetPreExisted) {
1385
+ const entries = readdirSync(target);
1386
+ if (entries.length > 0) {
1387
+ options.logger.error(`target directory not empty: ${target}`);
1388
+ return { code: 1, files: [], target };
1389
+ }
1390
+ }
1391
+ if (options.dryRun === true) {
1392
+ const what = choice.kind === "overlay" ? `the ${choice.framework} create-vite overlay` : `the ${choice.templateType} template`;
1393
+ logWould(options.logger, `scaffold ${what} into ${target}`);
1394
+ return { code: 0, files: [], target };
1395
+ }
1396
+ recordTarget(target, targetPreExisted);
1397
+ return choice.kind === "overlay" ? scaffoldOverlayPath(options, choice.framework, name, target) : scaffoldTemplatePath(options, choice.templateType, name, target);
1398
+ };
1399
+ const resetScaffoldOnCancel = (cleanup, logger) => {
1400
+ const { target, targetPreExisted } = cleanup;
1401
+ if (target === void 0 || !existsSync(target)) {
1402
+ return;
1403
+ }
1404
+ if (targetPreExisted === true) {
1405
+ for (const entry of readdirSync(target)) {
1406
+ rmSync(join$1(target, entry), { force: true, recursive: true });
1407
+ }
1408
+ } else {
1409
+ rmSync(target, { force: true, recursive: true });
1410
+ }
1411
+ logger.info(`removed the partially-created project at ${target}`);
1412
+ };
1413
+ const runScaffoldStep = async (options, cwd, recordTarget) => {
1414
+ if (options.inPlace !== true) {
1415
+ return scaffoldNewProject(options, cwd, recordTarget);
1416
+ }
1417
+ if (options.dryRun === true) {
1418
+ logWould(options.logger, `configure Lunora into ${cwd}`);
1419
+ return { code: 0, files: [], target: cwd };
1420
+ }
1421
+ return runInPlaceInit(cwd, options.logger);
1422
+ };
1423
+ const runPostScaffold = async (options, result, cwd) => {
1424
+ await maybeOfferExtras(options, result.target);
1425
+ const installedManager = options.inPlace === true ? void 0 : await maybeOfferInstall(options, result.target);
1426
+ if (options.inPlace !== true) {
1427
+ await maybeOfferGit(options, result.target);
1428
+ await printNextSteps(basename(result.target), installedManager, isInsideMonorepo(cwd));
1429
+ await emitMascot(options.logger);
1430
+ }
1431
+ };
1432
+ const scaffoldCiPipeline = (options, result, cwd) => {
1433
+ if (result.code !== 0 || options.ci === void 0) {
1434
+ return;
1435
+ }
1436
+ if (options.dryRun === true) {
1437
+ logWould(options.logger, `scaffold a ${options.ci} CI deploy pipeline`);
1438
+ return;
1439
+ }
1440
+ scaffoldCiWorkflow(options.inPlace === true ? cwd : result.target, options.ci, options.logger);
1441
+ };
1442
+ const runInitCommand = async (options) => {
1443
+ const cwd = options.cwd ?? process.cwd();
1444
+ const cleanup = {};
1445
+ let result;
1446
+ try {
1447
+ result = await runScaffoldStep(options, cwd, (target, preExisted) => {
1448
+ cleanup.target = target;
1449
+ cleanup.targetPreExisted = preExisted;
1450
+ });
1451
+ if (result.code === 0 && result.target !== "") {
1452
+ cleanup.target = void 0;
1453
+ await runPostScaffold(options, result, cwd);
1454
+ }
1455
+ } catch (error) {
1456
+ if (error instanceof PromptCancelledError) {
1457
+ resetScaffoldOnCancel(cleanup, options.logger);
1458
+ process.stdout.write("\n ✖ Setup cancelled — run `lunora init` again whenever you're ready. 🌙\n");
1459
+ return { code: 130, files: [], target: "" };
1460
+ }
1461
+ throw error;
1462
+ }
1463
+ scaffoldCiPipeline(options, result, cwd);
1464
+ return result;
1465
+ };
1466
+ 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";
1467
+ const resolveCiProvider = (raw, logger) => {
1468
+ if (raw === void 0) {
1469
+ return void 0;
1470
+ }
1471
+ if (isCiProvider(raw)) {
1472
+ return raw;
1473
+ }
1474
+ logger.warn(`init: unknown --ci "${raw}" — expected github | gitlab; skipping CI scaffold.`);
1475
+ return void 0;
1476
+ };
1477
+ const execute = defineHandler(({ argument, cwd, logger, options }) => {
1478
+ const templateType = options.template !== void 0 && isTemplate(options.template) ? options.template : void 0;
1479
+ return runInitCommand({
1480
+ add: options.add,
1481
+ allowUnsafeSource: options.allowUnsafeSource === true,
1482
+ cwd,
1483
+ ci: resolveCiProvider(options.ci, logger),
1484
+ dryRun: options.dryRun === true,
1485
+ from: options.from,
1486
+ inPlace: options.here === true,
1487
+ interactive: options.interactive === true ? true : void 0,
1488
+ logger,
1489
+ name: argument[0],
1490
+ ref: options.ref,
1491
+ source: options.source,
1492
+ templateType,
1493
+ vite: options.vite,
1494
+ yes: options.yes === true
1495
+ });
1496
+ });
1497
+
1498
+ export { execute, isTemplate, resolveTemplateSource, runInitCommand };