@lunora/cli 0.0.0 → 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +109 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/bin.mjs +11 -0
  5. package/dist/index.d.mts +852 -0
  6. package/dist/index.d.ts +852 -0
  7. package/dist/index.mjs +19 -0
  8. package/dist/packem_chunks/handler.mjs +76 -0
  9. package/dist/packem_chunks/handler10.mjs +22 -0
  10. package/dist/packem_chunks/handler11.mjs +192 -0
  11. package/dist/packem_chunks/handler12.mjs +131 -0
  12. package/dist/packem_chunks/handler13.mjs +65 -0
  13. package/dist/packem_chunks/handler14.mjs +58 -0
  14. package/dist/packem_chunks/handler15.mjs +79 -0
  15. package/dist/packem_chunks/handler16.mjs +41 -0
  16. package/dist/packem_chunks/handler17.mjs +105 -0
  17. package/dist/packem_chunks/handler18.mjs +172 -0
  18. package/dist/packem_chunks/handler19.mjs +89 -0
  19. package/dist/packem_chunks/handler2.mjs +114 -0
  20. package/dist/packem_chunks/handler20.mjs +94 -0
  21. package/dist/packem_chunks/handler21.mjs +311 -0
  22. package/dist/packem_chunks/handler3.mjs +204 -0
  23. package/dist/packem_chunks/handler4.mjs +33 -0
  24. package/dist/packem_chunks/handler5.mjs +49 -0
  25. package/dist/packem_chunks/handler6.mjs +91 -0
  26. package/dist/packem_chunks/handler7.mjs +42 -0
  27. package/dist/packem_chunks/handler8.mjs +174 -0
  28. package/dist/packem_chunks/handler9.mjs +16 -0
  29. package/dist/packem_chunks/planDevCommand.mjs +543 -0
  30. package/dist/packem_chunks/runCodegenCommand.mjs +52 -0
  31. package/dist/packem_chunks/runDeployCommand.mjs +504 -0
  32. package/dist/packem_chunks/runInitCommand.mjs +652 -0
  33. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +397 -0
  34. package/dist/packem_chunks/runResetCommand.mjs +41 -0
  35. package/dist/packem_chunks/runRpcCommand.mjs +68 -0
  36. package/dist/packem_shared/COMMANDS-1V_KEx35.mjs +905 -0
  37. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +244 -0
  38. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +19 -0
  39. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +13 -0
  40. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +38 -0
  41. package/dist/packem_shared/command-BDXcJCCJ.mjs +14 -0
  42. package/dist/packem_shared/createLogger-CHPNjFw2.mjs +73 -0
  43. package/dist/packem_shared/defaultSpawner-DxI3mebw.mjs +43 -0
  44. package/dist/packem_shared/diffSnapshots-RR2ZE8Ya.mjs +161 -0
  45. package/dist/packem_shared/docker-hMQ97KSQ.mjs +21 -0
  46. package/dist/packem_shared/features-ocSSpZtS.mjs +24 -0
  47. package/dist/packem_shared/insertSchemaExtension-BuzF6-t2.mjs +59 -0
  48. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +41 -0
  49. package/dist/packem_shared/output-format-7gyGR3h8.mjs +17 -0
  50. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +56 -0
  51. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +94 -0
  52. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +16 -0
  53. package/dist/packem_shared/runAddCommand-BZGkRnBs.mjs +693 -0
  54. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +79 -0
  55. package/dist/packem_shared/schemaIrToSnapshot-aBTo7TM5.mjs +43 -0
  56. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +12 -0
  57. package/package.json +61 -18
  58. package/skills/README.md +29 -0
  59. package/skills/lunora/SKILL.md +83 -0
  60. package/skills/lunora-create-package/SKILL.md +129 -0
  61. package/skills/lunora-deploy/SKILL.md +150 -0
  62. package/skills/lunora-functions/SKILL.md +182 -0
  63. package/skills/lunora-migration-helper/SKILL.md +194 -0
  64. package/skills/lunora-performance-audit/SKILL.md +143 -0
  65. package/skills/lunora-quickstart/SKILL.md +240 -0
  66. package/skills/lunora-realtime/SKILL.md +177 -0
  67. package/skills/lunora-setup-auth/SKILL.md +170 -0
  68. package/skills/lunora-setup-hyperdrive/SKILL.md +154 -0
  69. package/skills/lunora-setup-hyperdrive-global/SKILL.md +171 -0
  70. package/skills/lunora-setup-mail/SKILL.md +151 -0
  71. package/skills/lunora-setup-scheduler/SKILL.md +157 -0
  72. package/skills/lunora-setup-storage/SKILL.md +154 -0
@@ -0,0 +1,652 @@
1
+ import { existsSync, mkdirSync, writeFileSync, readdirSync, mkdtempSync, rmSync, readFileSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { detectFramework as detectFramework$1, isInteractive, promptSelect, promptMultiSelect } from '@lunora/config';
5
+ import { walkSync } from '@visulima/fs';
6
+ import { resolve, join as join$1, relative, dirname as dirname$1 } from '@visulima/path';
7
+ import { downloadTemplate } from 'giget';
8
+ import { join, dirname } from 'node:path';
9
+ import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
10
+ import MagicString from 'magic-string';
11
+ 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';
14
+
15
+ const GITHUB_CONTENT = `name: Deploy
16
+
17
+ on:
18
+ push:
19
+ branches: [main]
20
+ pull_request:
21
+ workflow_dispatch:
22
+
23
+ # Set these repository secrets (Settings → Secrets and variables → Actions):
24
+ # CLOUDFLARE_API_TOKEN — a Workers-scoped API token
25
+ # CLOUDFLARE_ACCOUNT_ID — your Cloudflare account id
26
+ env:
27
+ CLOUDFLARE_API_TOKEN: \${{ secrets.CLOUDFLARE_API_TOKEN }}
28
+ CLOUDFLARE_ACCOUNT_ID: \${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
29
+
30
+ jobs:
31
+ # Production deploy on push to the default branch.
32
+ deploy:
33
+ if: github.event_name != 'pull_request'
34
+ runs-on: ubuntu-latest
35
+ permissions:
36
+ contents: read
37
+ steps:
38
+ - uses: actions/checkout@v4
39
+ - uses: pnpm/action-setup@v4
40
+ - uses: actions/setup-node@v4
41
+ with:
42
+ node-version: 22
43
+ cache: pnpm
44
+ - run: pnpm install --frozen-lockfile
45
+ # Codegen + wrangler validation gate (no deploy) — fails fast on drift.
46
+ - run: pnpm exec lunora prepare
47
+ - run: pnpm exec lunora deploy
48
+
49
+ # Preview version on every pull request — uploads a versioned preview URL;
50
+ # production traffic is untouched.
51
+ preview:
52
+ if: github.event_name == 'pull_request'
53
+ runs-on: ubuntu-latest
54
+ permissions:
55
+ contents: read
56
+ steps:
57
+ - uses: actions/checkout@v4
58
+ - uses: pnpm/action-setup@v4
59
+ - uses: actions/setup-node@v4
60
+ with:
61
+ node-version: 22
62
+ cache: pnpm
63
+ - run: pnpm install --frozen-lockfile
64
+ - run: pnpm exec lunora deploy --preview
65
+ `;
66
+ const GITLAB_CONTENT = `stages:
67
+ - deploy
68
+
69
+ # Set these as masked CI/CD variables (Settings → CI/CD → Variables):
70
+ # CLOUDFLARE_API_TOKEN — a Workers-scoped API token
71
+ # CLOUDFLARE_ACCOUNT_ID — your Cloudflare account id
72
+ .lunora_base:
73
+ image: node:22
74
+ stage: deploy
75
+ before_script:
76
+ - corepack enable
77
+ - pnpm install --frozen-lockfile
78
+
79
+ # Production deploy on the default branch.
80
+ deploy:
81
+ extends: .lunora_base
82
+ rules:
83
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
84
+ script:
85
+ # Codegen + wrangler validation gate (no deploy) — fails fast on drift.
86
+ - pnpm exec lunora prepare
87
+ - pnpm exec lunora deploy
88
+
89
+ # Preview version on every merge request (versioned preview URL; production
90
+ # traffic is untouched).
91
+ preview:
92
+ extends: .lunora_base
93
+ rules:
94
+ - if: $CI_PIPELINE_SOURCE == "merge_request_event"
95
+ script:
96
+ - pnpm exec lunora deploy --preview
97
+ `;
98
+ const WORKFLOWS = {
99
+ github: {
100
+ content: GITHUB_CONTENT,
101
+ file: join(".github", "workflows", "deploy.yml"),
102
+ secretsHint: "repository secrets (Settings → Secrets and variables → Actions)"
103
+ },
104
+ gitlab: {
105
+ content: GITLAB_CONTENT,
106
+ file: ".gitlab-ci.yml",
107
+ secretsHint: "masked CI/CD variables (Settings → CI/CD → Variables)"
108
+ }
109
+ };
110
+ const isCiProvider = (value) => value === "github" || value === "gitlab";
111
+ const writeCiWorkflow = (projectRoot, provider, options = {}) => {
112
+ const spec = WORKFLOWS[provider];
113
+ const path = join(projectRoot, spec.file);
114
+ if (existsSync(path) && options.overwrite !== true) {
115
+ return { path, skipped: true, written: false };
116
+ }
117
+ mkdirSync(dirname(path), { recursive: true });
118
+ writeFileSync(path, spec.content, "utf8");
119
+ return { path, skipped: false, written: true };
120
+ };
121
+ const scaffoldCiWorkflow = (projectRoot, provider, logger, options = {}) => {
122
+ const spec = WORKFLOWS[provider];
123
+ try {
124
+ const result = writeCiWorkflow(projectRoot, provider, options);
125
+ if (result.skipped) {
126
+ logger.info(`--ci ${provider}: ${spec.file} already exists — left unchanged (re-run with overwrite to replace).`);
127
+ } else {
128
+ logger.success(`--ci ${provider}: wrote ${spec.file}`);
129
+ logger.info(`--ci ${provider}: set CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID as ${spec.secretsHint} to enable deploys.`);
130
+ }
131
+ return result;
132
+ } catch (error) {
133
+ const message = error instanceof Error ? error.message : String(error);
134
+ logger.warn(`--ci ${provider}: could not write ${spec.file} (${message})`);
135
+ return { path: join(projectRoot, spec.file), skipped: false, written: false };
136
+ }
137
+ };
138
+
139
+ const ADAPTER_BY_FRAMEWORK = {
140
+ astro: "@lunora/react",
141
+ none: "@lunora/react",
142
+ nuxt: "@lunora/vue",
143
+ "react-router": "@lunora/react",
144
+ "solid-start": "@lunora/solid",
145
+ sveltekit: "@lunora/svelte",
146
+ "tanstack-start": "@lunora/react",
147
+ "tanstack-start-solid": "@lunora/solid"
148
+ };
149
+ const detectFramework = (root) => {
150
+ const base = detectFramework$1(root);
151
+ return { ...base, adapter: ADAPTER_BY_FRAMEWORK[base.framework] };
152
+ };
153
+
154
+ const LUNORA_CALL = "lunora()";
155
+ const LUNORA_IMPORT = 'import { lunora } from "@lunora/vite";';
156
+ const LUNORA_CALL_RE = /\blunora\s*\(/u;
157
+ const LUNORA_VITE_DOUBLE = '"@lunora/vite"';
158
+ const LUNORA_VITE_SINGLE = "'@lunora/vite'";
159
+ const findDefineConfigObject = (sf) => {
160
+ for (const statement of sf.getStatements()) {
161
+ if (statement.getKind() !== SyntaxKind.ExportAssignment) {
162
+ continue;
163
+ }
164
+ const expr = statement.asKindOrThrow(SyntaxKind.ExportAssignment).getExpression();
165
+ if (expr.getKind() !== SyntaxKind.CallExpression) {
166
+ continue;
167
+ }
168
+ const call = expr.asKindOrThrow(SyntaxKind.CallExpression);
169
+ if (call.getExpression().getText() !== "defineConfig") {
170
+ continue;
171
+ }
172
+ const argument = call.getArguments()[0];
173
+ if (argument?.getKind() === SyntaxKind.ObjectLiteralExpression) {
174
+ return argument.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
175
+ }
176
+ }
177
+ return void 0;
178
+ };
179
+ const findPlainExportObject = (sf) => {
180
+ for (const statement of sf.getStatements()) {
181
+ if (statement.getKind() !== SyntaxKind.ExportAssignment) {
182
+ continue;
183
+ }
184
+ const expr = statement.asKindOrThrow(SyntaxKind.ExportAssignment).getExpression();
185
+ if (expr.getKind() === SyntaxKind.ObjectLiteralExpression) {
186
+ return expr.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
187
+ }
188
+ }
189
+ return void 0;
190
+ };
191
+ const findConfigObject = (sourceText) => {
192
+ const project = new Project({
193
+ compilerOptions: { allowJs: true },
194
+ useInMemoryFileSystem: true
195
+ });
196
+ const sf = project.createSourceFile("vite.config.ts", sourceText, { overwrite: true });
197
+ return findDefineConfigObject(sf) ?? findPlainExportObject(sf);
198
+ };
199
+ const importInsertPosition = (sourceText) => {
200
+ const project = new Project({
201
+ compilerOptions: { allowJs: true },
202
+ useInMemoryFileSystem: true
203
+ });
204
+ const sf = project.createSourceFile("vite.config.ts", sourceText, { overwrite: true });
205
+ const imports = sf.getImportDeclarations();
206
+ if (imports.length === 0) {
207
+ return 0;
208
+ }
209
+ const last = imports[imports.length - 1];
210
+ if (last === void 0) {
211
+ return 0;
212
+ }
213
+ return last.getEnd();
214
+ };
215
+ const addImport = (ms, source) => {
216
+ const insertAt = importInsertPosition(source);
217
+ if (insertAt === 0) {
218
+ ms.prepend(`${LUNORA_IMPORT}
219
+ `);
220
+ } else {
221
+ ms.appendLeft(insertAt, `
222
+ ${LUNORA_IMPORT}`);
223
+ }
224
+ };
225
+ const patchPluginsArray = (ms, configObject) => {
226
+ const pluginsProp = configObject.getProperty("plugins");
227
+ if (pluginsProp === void 0) {
228
+ const properties = configObject.getProperties();
229
+ const openBrace = configObject.getStart() + 1;
230
+ if (properties.length === 0) {
231
+ ms.appendLeft(openBrace, ` plugins: [${LUNORA_CALL}] `);
232
+ } else {
233
+ const firstProp = properties[0];
234
+ if (firstProp !== void 0) {
235
+ ms.appendLeft(firstProp.getStart(), `plugins: [${LUNORA_CALL}],
236
+ `);
237
+ }
238
+ }
239
+ return;
240
+ }
241
+ const arrayLit = pluginsProp.getDescendantsOfKind(SyntaxKind.ArrayLiteralExpression)[0];
242
+ if (arrayLit === void 0) {
243
+ return;
244
+ }
245
+ const elements = arrayLit.getElements();
246
+ if (elements.length === 0) {
247
+ ms.appendLeft(arrayLit.getStart() + 1, LUNORA_CALL);
248
+ } else {
249
+ const firstElement = elements[0];
250
+ if (firstElement !== void 0) {
251
+ ms.appendLeft(firstElement.getStart(), `${LUNORA_CALL}, `);
252
+ }
253
+ }
254
+ };
255
+ const patchViteConfig = (source) => {
256
+ if (LUNORA_CALL_RE.test(source)) {
257
+ return { changed: false, code: source, reason: "lunora plugin already present" };
258
+ }
259
+ const configObject = findConfigObject(source);
260
+ if (configObject === void 0) {
261
+ return { changed: false, code: source, reason: "could not locate a Vite config plugins array to patch" };
262
+ }
263
+ const ms = new MagicString(source);
264
+ const alreadyImported = source.includes(LUNORA_VITE_DOUBLE) || source.includes(LUNORA_VITE_SINGLE);
265
+ if (alreadyImported) {
266
+ patchPluginsArray(ms, configObject);
267
+ } else {
268
+ addImport(ms, source);
269
+ patchPluginsArray(ms, configObject);
270
+ }
271
+ return { changed: true, code: ms.toString() };
272
+ };
273
+
274
+ const STACK_FEATURE_OPTIONS = [
275
+ { 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" }
277
+ ];
278
+ const offerRegistryExtras = async (deps) => {
279
+ if (!deps.interactive) {
280
+ deps.logger.info("tip: add authentication or email later with `lunora add auth` / `lunora add email`.");
281
+ return;
282
+ }
283
+ const picked = await deps.multiSelect("Which features do you want to add?", STACK_FEATURE_OPTIONS, { defaults: [] });
284
+ 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]);
290
+ }
291
+ }
292
+ };
293
+
294
+ const TEXT_EXTENSIONS = /* @__PURE__ */ new Set([".gitignore", ".html", ".js", ".json", ".jsonc", ".md", ".mjs", ".ts", ".tsx"]);
295
+ const VITE_CONFIG_CANDIDATES = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"];
296
+ const MINIMAL_VITE_CONFIG = `import { defineConfig } from "vite";
297
+ import { lunora } from "@lunora/vite";
298
+
299
+ export default defineConfig({ plugins: [lunora()] });
300
+ `;
301
+ const SAMPLE_SCHEMA = `import { defineSchema, defineTable, v } from "@lunora/server";
302
+
303
+ export default defineSchema({
304
+ messages: defineTable({
305
+ channelId: v.id("channels"),
306
+ text: v.string(),
307
+ })
308
+ .shardBy("channelId")
309
+ .index("by_channel", ["channelId"]),
310
+ });
311
+ `;
312
+ const SAMPLE_FUNCTION = `import { mutation, query, v } from "./_generated/server";
313
+
314
+ export const list = query
315
+ .input({ channelId: v.id("channels"), limit: v.optional(v.number()) })
316
+ .query(async ({ args }) => {
317
+ return { channelId: args.channelId, limit: args.limit ?? 50, messages: [] };
318
+ });
319
+
320
+ export const send = mutation
321
+ .input({ channelId: v.id("channels"), text: v.string() })
322
+ .mutation(async ({ args }) => {
323
+ return { channelId: args.channelId, text: args.text };
324
+ });
325
+ `;
326
+ 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
+ const isTextFile = (filePath) => {
354
+ const lastDot = filePath.lastIndexOf(".");
355
+ if (lastDot === -1) {
356
+ return false;
357
+ }
358
+ return TEXT_EXTENSIONS.has(filePath.slice(lastDot));
359
+ };
360
+ const substitute = (content, name) => content.replaceAll("{{name}}", name);
361
+ const collectFiles = (directory) => {
362
+ const out = [];
363
+ for (const entry of walkSync(directory, { includeDirs: false, includeFiles: true })) {
364
+ out.push(entry.path);
365
+ }
366
+ return out;
367
+ };
368
+ const copyTemplate = (sourceDirectory, target, name) => {
369
+ const files = collectFiles(sourceDirectory);
370
+ const written = [];
371
+ for (const source of files) {
372
+ const relativePath = relative(sourceDirectory, source);
373
+ const destination = join$1(target, relativePath);
374
+ mkdirSync(dirname$1(destination), { recursive: true });
375
+ const raw = readFileSync(source);
376
+ const text = isTextFile(source) ? substitute(raw.toString("utf8"), name) : void 0;
377
+ if (text === void 0) {
378
+ writeFileSync(destination, raw);
379
+ } else {
380
+ writeFileSync(destination, text, "utf8");
381
+ }
382
+ written.push(destination);
383
+ }
384
+ return written;
385
+ };
386
+ const resolveTemplateSource = (templateType, source) => {
387
+ if (source !== void 0 && source.length > 0) {
388
+ return source;
389
+ }
390
+ return `${DEFAULT_SOURCE_BASE}/${templateType}#${resolveDefaultSourceRef()}`;
391
+ };
392
+ const isSafeSource = (source) => {
393
+ if (source.includes("..")) {
394
+ return false;
395
+ }
396
+ return source.startsWith("gh:") || source.startsWith("github:") || source.startsWith("https://");
397
+ };
398
+ const logScaffoldSuccess = (logger, written, target, name) => {
399
+ 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
+ };
405
+ const scaffoldFromLocal = (fromRoot, templateType, target, name, logger) => {
406
+ const templateDirectory = join$1(fromRoot, templateType);
407
+ if (!existsSync(templateDirectory)) {
408
+ logger.error(`template not found in local source: ${templateDirectory}`);
409
+ return { code: 1, files: [], target };
410
+ }
411
+ const written = copyTemplate(templateDirectory, target, name);
412
+ logScaffoldSuccess(logger, written, target, name);
413
+ return { code: 0, files: written, target };
414
+ };
415
+ const scaffoldFromRemote = async (source, templateType, target, name, logger) => {
416
+ const stagingRoot = mkdtempSync(join$1(tmpdir(), "lunora-init-fetch-"));
417
+ const stagingDirectory = join$1(stagingRoot, "template");
418
+ 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
+ });
428
+ 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))`);
433
+ }
434
+ const written = copyTemplate(stagingDirectory, target, name);
435
+ logScaffoldSuccess(logger, written, target, name);
436
+ return { code: 0, files: written, target };
437
+ } catch (error) {
438
+ const message = error instanceof Error ? error.message : String(error);
439
+ logger.error(`failed to download template: ${message}`);
440
+ return { code: 1, files: [], target };
441
+ } finally {
442
+ rmSync(stagingRoot, { force: true, recursive: true });
443
+ }
444
+ };
445
+ const createMinimalViteConfig = (cwd, logger) => {
446
+ const target = join$1(cwd, "vite.config.ts");
447
+ try {
448
+ writeFileSync(target, MINIMAL_VITE_CONFIG, "utf8");
449
+ } catch (error) {
450
+ const message = error instanceof Error ? error.message : String(error);
451
+ logger.error(`init --in-place: could not write ${target}: ${message}`);
452
+ return { code: 1, files: [], target: cwd };
453
+ }
454
+ logger.success(`created ${target} with lunora() plugin`);
455
+ return { code: 0, files: [target], target: cwd };
456
+ };
457
+ const patchExistingViteConfig = (viteConfigPath, cwd, logger) => {
458
+ let source;
459
+ try {
460
+ source = readFileSync(viteConfigPath, "utf8");
461
+ } catch (error) {
462
+ const message = error instanceof Error ? error.message : String(error);
463
+ logger.error(`init --in-place: could not read ${viteConfigPath}: ${message}`);
464
+ return { code: 1, files: [], target: cwd };
465
+ }
466
+ const result = patchViteConfig(source);
467
+ if (!result.changed) {
468
+ logger.info(`${viteConfigPath}: ${result.reason ?? "no changes needed"}`);
469
+ return { code: 0, files: [], target: cwd };
470
+ }
471
+ try {
472
+ writeFileSync(viteConfigPath, result.code, "utf8");
473
+ } catch (error) {
474
+ const message = error instanceof Error ? error.message : String(error);
475
+ logger.error(`init --in-place: could not write ${viteConfigPath}: ${message}`);
476
+ return { code: 1, files: [], target: cwd };
477
+ }
478
+ logger.success(`patched ${viteConfigPath} — added lunora() plugin`);
479
+ return { code: 0, files: [viteConfigPath], target: cwd };
480
+ };
481
+ const scaffoldLunoraDirectory = (cwd, logger) => {
482
+ const lunoraDirectory = join$1(cwd, "lunora");
483
+ const schemaPath = join$1(lunoraDirectory, "schema.ts");
484
+ if (existsSync(schemaPath)) {
485
+ logger.info(`lunora/ already present — left ${schemaPath} untouched`);
486
+ return [];
487
+ }
488
+ const written = [];
489
+ try {
490
+ mkdirSync(lunoraDirectory, { recursive: true });
491
+ writeFileSync(schemaPath, SAMPLE_SCHEMA, "utf8");
492
+ written.push(schemaPath);
493
+ const functionPath = join$1(lunoraDirectory, "messages.ts");
494
+ if (!existsSync(functionPath)) {
495
+ writeFileSync(functionPath, SAMPLE_FUNCTION, "utf8");
496
+ written.push(functionPath);
497
+ }
498
+ logger.success(`scaffolded lunora/ (${String(written.length)} file(s))`);
499
+ } catch (error) {
500
+ const message = error instanceof Error ? error.message : String(error);
501
+ logger.error(`init --here: could not scaffold lunora/: ${message}`);
502
+ }
503
+ return written;
504
+ };
505
+ const printFrameworkNextSteps = (detection, logger) => {
506
+ const { adapter, class: frameworkClass, framework } = detection;
507
+ logger.info("");
508
+ logger.info(`detected framework: ${framework} (class ${frameworkClass})`);
509
+ logger.info("next steps:");
510
+ logger.info(` 1. install the adapter: pnpm add ${adapter} @lunora/client @lunora/runtime @lunora/server`);
511
+ logger.info(" 2. run codegen: lunora codegen");
512
+ if (frameworkClass === "A") {
513
+ logger.info(" 3. compose one worker: wrap your worker entry with");
514
+ logger.info(" createWorker({ httpRouter: <your framework SSR handler>, shardDO: ShardDO, ... })");
515
+ logger.info(` 4. add the provider: mount the ${adapter} provider in your root layout/route`);
516
+ logger.info(" 5. make a loader live: preloadQuery() in a loader, usePreloadedQuery() in the component");
517
+ logger.info(" see https://lunora.sh/docs/frameworks/reactive-loaders");
518
+ } else if (frameworkClass === "B") {
519
+ logger.info(" 3. inject Lunora: mount Lunora realtime under /_lunora/* in your server hook");
520
+ logger.info(` (${framework} owns its Cloudflare adapter — Lunora composes into its server entry)`);
521
+ logger.info(` 4. add the provider: mount the ${adapter} provider in your root layout`);
522
+ logger.info(" 5. read the guide: https://lunora.sh/docs/frameworks/deploy");
523
+ } else {
524
+ logger.info(" 3. add the provider: wrap your app with the LunoraProvider from @lunora/react");
525
+ logger.info(" 4. read the guide: https://lunora.sh/docs/frameworks/bring-your-framework");
526
+ }
527
+ logger.info("");
528
+ };
529
+ const findExistingViteConfig = (cwd) => {
530
+ for (const candidate of VITE_CONFIG_CANDIDATES) {
531
+ const full = join$1(cwd, candidate);
532
+ if (existsSync(full)) {
533
+ return full;
534
+ }
535
+ }
536
+ return void 0;
537
+ };
538
+ const patchOrCreateViteConfig = (cwd, framework, logger) => {
539
+ const viteConfigPath = findExistingViteConfig(cwd);
540
+ if (viteConfigPath === void 0) {
541
+ if (framework === "sveltekit" || framework === "nuxt" || framework === "astro") {
542
+ logger.info(`no Vite config found — ${framework} wires Lunora through its server entry (see next steps)`);
543
+ return { code: 0, files: [], target: cwd };
544
+ }
545
+ return createMinimalViteConfig(cwd, logger);
546
+ }
547
+ return patchExistingViteConfig(viteConfigPath, cwd, logger);
548
+ };
549
+ const runInPlaceInit = (cwd, logger) => {
550
+ const detection = detectFramework(cwd);
551
+ const viteResult = patchOrCreateViteConfig(cwd, detection.framework, logger);
552
+ if (viteResult.code !== 0) {
553
+ return viteResult;
554
+ }
555
+ const scaffolded = scaffoldLunoraDirectory(cwd, logger);
556
+ printFrameworkNextSteps(detection, logger);
557
+ return { code: 0, files: [...viteResult.files, ...scaffolded], target: cwd };
558
+ };
559
+ const offerIsInteractive = (options) => options.yes !== true && (options.prompt !== void 0 || (options.interactive ?? isInteractive()));
560
+ const maybeOfferExtras = async (options, projectDirectory) => {
561
+ 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
571
+ });
572
+ return result.code === 0;
573
+ };
574
+ await offerRegistryExtras({
575
+ apply,
576
+ interactive,
577
+ 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
+ });
581
+ };
582
+ const scaffoldNewProject = async (options, cwd) => {
583
+ const name = options.name ?? "lunora-app";
584
+ const templateType = options.templateType ?? "vite";
585
+ if (templateType === "next") {
586
+ options.logger.warn('template "next" is not yet available — re-run with `-t vite` or `-t standalone`.');
587
+ return { code: 1, files: [], target: "" };
588
+ }
589
+ if (name.includes("/") || name.includes("\\") || name === ".." || name === ".") {
590
+ options.logger.error(`init: refusing project name "${name}" — must not contain path separators or be "." / "..".`);
591
+ return { code: 1, files: [], target: "" };
592
+ }
593
+ const target = resolve(cwd, name);
594
+ if (existsSync(target)) {
595
+ const entries = readdirSync(target);
596
+ if (entries.length > 0) {
597
+ options.logger.error(`target directory not empty: ${target}`);
598
+ return { code: 1, files: [], target };
599
+ }
600
+ }
601
+ if (options.from !== void 0) {
602
+ return scaffoldFromLocal(options.from, templateType, target, name, options.logger);
603
+ }
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 };
609
+ }
610
+ return scaffoldFromRemote(options.source, templateType, target, name, options.logger);
611
+ };
612
+ const runInitCommand = async (options) => {
613
+ 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);
620
+ }
621
+ return result;
622
+ };
623
+ const isTemplate = (value) => value === "astro" || value === "next" || value === "nuxt" || value === "standalone" || value === "sveltekit" || value === "tanstack-start-react" || value === "tanstack-start-solid" || value === "vite";
624
+ const resolveCiProvider = (raw, logger) => {
625
+ if (raw === void 0) {
626
+ return void 0;
627
+ }
628
+ if (isCiProvider(raw)) {
629
+ return raw;
630
+ }
631
+ logger.warn(`init: unknown --ci "${raw}" — expected github | gitlab; skipping CI scaffold.`);
632
+ return void 0;
633
+ };
634
+ const execute = defineHandler(({ argument, cwd, logger, options }) => {
635
+ const templateRaw = options.template ?? "vite";
636
+ const template = isTemplate(templateRaw) ? templateRaw : "vite";
637
+ return runInitCommand({
638
+ allowUnsafeSource: options.allowUnsafeSource === true,
639
+ cwd,
640
+ ci: resolveCiProvider(options.ci, logger),
641
+ from: options.from,
642
+ inPlace: options.here === true,
643
+ interactive: options.interactive === true ? true : void 0,
644
+ logger,
645
+ name: argument[0],
646
+ source: options.source,
647
+ templateType: template,
648
+ yes: options.yes === true
649
+ });
650
+ });
651
+
652
+ export { execute, isTemplate, runInitCommand };