@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
package/dist/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ export { COMMANDS, VERSION, runCli } from './packem_shared/COMMANDS-D3h9Iwvl.mjs';
2
+ export { runCodegenCommand } from './packem_chunks/runCodegenCommand.mjs';
3
+ export { DEFAULT_IMPORT_BATCH_SIZE, runExportCommand, runImportCommand } from './packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs';
4
+ export { runDeployCommand } from './packem_chunks/runDeployCommand.mjs';
5
+ export { planDevCommand, runDevCommand } from './packem_chunks/planDevCommand.mjs';
6
+ export { runInitCommand } from './packem_chunks/runInitCommand.mjs';
7
+ export { runMigrateGenerateCommand } from './packem_chunks/runMigrateGenerateCommand.mjs';
8
+ export { runResetCommand } from './packem_chunks/runResetCommand.mjs';
9
+ export { runRpcCommand } from './packem_chunks/runRpcCommand.mjs';
10
+ export { default as parseArgs } from './packem_shared/parseArgs-YXFuKdEk.mjs';
11
+ export { insertSchemaExtension } from './packem_shared/insertSchemaExtension-BuzF6-t2.mjs';
12
+ export { createLogger, pail } from './packem_shared/createLogger-B40gPzQo.mjs';
13
+ export { diffSnapshots, renderAddColumn, renderCreateIndex, renderCreateTable, renderDropIndex, renderDropTable, renderMigrationFile, validatorKindToSqlType } from './packem_shared/diffSnapshots-BeDvvNiF.mjs';
14
+ export { default as schemaIrToSnapshot } from './packem_shared/schemaIrToSnapshot-DdsljJT-.mjs';
15
+ export { createRecordingSpawner, defaultSpawner } from './packem_shared/createRecordingSpawner-DxI3mebw.mjs';
16
+ export { default as parseManifest } from './packem_shared/parseManifest--vZf2FY1.mjs';
17
+ export { REQUIRED_COMPATIBILITY_DATE, REQUIRED_FLAG, validateWranglerProject as validateWrangler, validateWranglerConfig } from '@lunora/config';
18
+ export { buildRegistryIndex } from './packem_shared/buildRegistryIndex-BcYe607_.mjs';
19
+ export { r as runAddCommand, a as runBuildIndexCommand, b as runRegistryViewCommand } from './packem_shared/commands-DPKWlqqX.mjs';
@@ -0,0 +1,150 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { findWranglerFile } from '@lunora/config';
3
+ import { join, basename } from '@visulima/path';
4
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
5
+ import { t as tuiText, a as tuiSelect } from '../packem_shared/tui-prompts-DEiPCKV-.mjs';
6
+ import { n as normalizeFeature, E as EMAIL_ITEM, s as sanitizeBucketName, d as deriveBucketName, p as promptBucketName, r as resolveTypedDestination, M as MAIL_DESTINATION_PROMPT, a as sanitizeDatabaseName, b as deriveDatabaseName, c as promptDatabaseName, D as DEFAULT_AUTH_ITEM, e as promptAuthProvider, A as AUTH_PROVIDER_OPTIONS, w as withStorageBucketName, f as withMailDestination, g as withAuthDatabaseName } from '../packem_shared/storage-2RJBhUC4.mjs';
7
+ import { r as runAddCommand } from '../packem_shared/commands-DPKWlqqX.mjs';
8
+
9
+ const providerToItem = (provider) => {
10
+ const value = provider.trim().toLowerCase();
11
+ const match = AUTH_PROVIDER_OPTIONS.find(
12
+ (option) => option.value === value || option.label.toLowerCase() === value || value === "auth0" && option.value === "auth-auth0" || value === "clerk" && option.value === "auth-clerk"
13
+ );
14
+ return match?.value;
15
+ };
16
+ const resolveAuthItem = async (options) => {
17
+ if (options.provider !== void 0 && options.provider !== "") {
18
+ const item = providerToItem(options.provider);
19
+ if (item === void 0) {
20
+ options.logger.warn(`add: unknown --provider "${options.provider}" — using "${DEFAULT_AUTH_ITEM}" (email & password).`);
21
+ return DEFAULT_AUTH_ITEM;
22
+ }
23
+ return item;
24
+ }
25
+ if (options.yes === true) {
26
+ return DEFAULT_AUTH_ITEM;
27
+ }
28
+ const select = options.promptSelect ?? ((message, choices, settings) => tuiSelect(message, choices, settings));
29
+ return promptAuthProvider(select);
30
+ };
31
+ const textPrompt = (options) => options.promptText ?? ((message, settings) => tuiText(message, settings));
32
+ const resolveStorageBucketName = async (options) => {
33
+ const projectName = basename(options.cwd ?? process.cwd());
34
+ if (options.bucket !== void 0 && options.bucket !== "") {
35
+ const sanitized = sanitizeBucketName(options.bucket);
36
+ if (sanitized !== void 0) {
37
+ return sanitized;
38
+ }
39
+ const fallback = deriveBucketName(projectName);
40
+ options.logger.warn(`add: "${options.bucket}" isn't a valid R2 bucket name (lowercase alphanumeric + hyphens, 3–63 chars) — using "${fallback}".`);
41
+ return fallback;
42
+ }
43
+ if (options.yes === true) {
44
+ return deriveBucketName(projectName);
45
+ }
46
+ return promptBucketName(textPrompt(options), projectName);
47
+ };
48
+ const resolveMailDestination = async (options) => {
49
+ const warn = (message) => {
50
+ options.logger.warn(`add: ${message}`);
51
+ };
52
+ if (options.mailTo !== void 0 && options.mailTo !== "") {
53
+ return resolveTypedDestination(options.mailTo, warn);
54
+ }
55
+ if (options.yes === true) {
56
+ return void 0;
57
+ }
58
+ return resolveTypedDestination(await textPrompt(options)(MAIL_DESTINATION_PROMPT, { placeholder: "you@yourdomain.com" }), warn);
59
+ };
60
+ const resolveAuthDatabaseName = async (options) => {
61
+ const projectName = basename(options.cwd ?? process.cwd());
62
+ if (options.db !== void 0 && options.db !== "") {
63
+ const sanitized = sanitizeDatabaseName(options.db);
64
+ if (sanitized !== void 0) {
65
+ return sanitized;
66
+ }
67
+ const fallback = deriveDatabaseName(projectName);
68
+ options.logger.warn(`add: "${options.db}" isn't a usable D1 database name — using "${fallback}".`);
69
+ return fallback;
70
+ }
71
+ if (options.yes === true) {
72
+ return deriveDatabaseName(projectName);
73
+ }
74
+ return promptDatabaseName(textPrompt(options), projectName);
75
+ };
76
+ const resolveFeatureItems = async (feature, options) => {
77
+ if (feature.kind === "auth") {
78
+ return [await resolveAuthItem(options)];
79
+ }
80
+ if (feature.kind === "email") {
81
+ return [EMAIL_ITEM];
82
+ }
83
+ return [feature.item];
84
+ };
85
+ const runAddFeature = async (options) => {
86
+ const cwd = options.cwd ?? process.cwd();
87
+ const feature = options.feature === void 0 ? void 0 : normalizeFeature(options.feature);
88
+ if (feature === void 0) {
89
+ options.logger.error("add requires a feature or registry item. Usage: lunora add <auth|email|storage|crons|presence|…>");
90
+ return { code: 1, items: [] };
91
+ }
92
+ if (!existsSync(join(cwd, "lunora")) || findWranglerFile(cwd) === void 0) {
93
+ options.logger.error("add: not a Lunora project here (need a lunora/ directory and a wrangler.jsonc). Run `lunora init` first.");
94
+ return { code: 1, items: [] };
95
+ }
96
+ const items = await resolveFeatureItems(feature, options);
97
+ const transforms = [];
98
+ if (items.includes("storage")) {
99
+ const bucketName = await resolveStorageBucketName(options);
100
+ transforms.push((manifest) => withStorageBucketName(manifest, bucketName));
101
+ }
102
+ if (items.includes("mail")) {
103
+ const destination = await resolveMailDestination(options);
104
+ if (destination !== void 0) {
105
+ transforms.push((manifest) => withMailDestination(manifest, destination));
106
+ }
107
+ }
108
+ if (items.some((name) => name === "auth" || name.startsWith("auth-"))) {
109
+ const databaseName = await resolveAuthDatabaseName(options);
110
+ transforms.push((manifest) => withAuthDatabaseName(manifest, databaseName));
111
+ }
112
+ const transformManifest = transforms.length > 0 ? (manifest) => {
113
+ let result2 = manifest;
114
+ for (const transform of transforms) {
115
+ result2 = transform(result2);
116
+ }
117
+ return result2;
118
+ } : void 0;
119
+ const result = await runAddCommand({
120
+ allowUnsafeSource: options.allowUnsafeSource,
121
+ cwd,
122
+ from: options.from,
123
+ logger: options.logger,
124
+ names: [...items],
125
+ ref: options.ref,
126
+ source: options.source,
127
+ transformManifest,
128
+ yes: true
129
+ });
130
+ return { code: result.code, items };
131
+ };
132
+ const execute = defineHandler(async ({ argument, cwd, logger, options }) => {
133
+ const result = await runAddFeature({
134
+ allowUnsafeSource: options.allowUnsafeSource === true,
135
+ bucket: options.bucket,
136
+ cwd,
137
+ db: options.db,
138
+ feature: argument[0],
139
+ from: options.from,
140
+ logger,
141
+ mailTo: options.mailTo,
142
+ provider: options.provider,
143
+ ref: options.ref,
144
+ source: options.source,
145
+ yes: options.yes === true
146
+ });
147
+ return { code: result.code };
148
+ });
149
+
150
+ export { execute, runAddFeature };
@@ -0,0 +1,22 @@
1
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
2
+ import { a as resolveProductionWorkerUrl } from '../packem_shared/resolve-target-qbsJ_5sF.mjs';
3
+ import { runImportCommand } from '../packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs';
4
+
5
+ const execute = defineHandler(({ argument, cwd, logger, options }) => {
6
+ const file = argument[0];
7
+ if (!file) {
8
+ logger.error("import requires a file. Usage: lunora import <path> [--table <name>]");
9
+ return { code: 1 };
10
+ }
11
+ return runImportCommand({
12
+ batchSize: options.batchSize,
13
+ file,
14
+ logger,
15
+ prod: options.prod === true,
16
+ table: options.table,
17
+ token: options.token,
18
+ url: resolveProductionWorkerUrl({ cwd, prod: options.prod === true, url: options.url })
19
+ });
20
+ });
21
+
22
+ export { execute };
@@ -0,0 +1,192 @@
1
+ import { readFileSync, existsSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { discoverSchema } from '@lunora/codegen';
4
+ import { readLinkedProject } from '@lunora/config';
5
+ import { parse } from 'jsonc-parser';
6
+ import { Project } from 'ts-morph';
7
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
8
+
9
+ const findWranglerFile = (projectRoot) => {
10
+ for (const candidate of ["wrangler.jsonc", "wrangler.json"]) {
11
+ const fullPath = join(projectRoot, candidate);
12
+ if (existsSync(fullPath)) {
13
+ return fullPath;
14
+ }
15
+ }
16
+ return void 0;
17
+ };
18
+ const stringField = (record, key) => {
19
+ if (record === null || typeof record !== "object") {
20
+ return void 0;
21
+ }
22
+ const value = record[key];
23
+ return typeof value === "string" && value.length > 0 ? value : void 0;
24
+ };
25
+ const arrayField = (record, key) => {
26
+ if (record === null || typeof record !== "object") {
27
+ return [];
28
+ }
29
+ const value = record[key];
30
+ return Array.isArray(value) ? value : [];
31
+ };
32
+ const summariseWrangler = (raw) => {
33
+ const durableObjectBindings = arrayField(raw.durable_objects ?? {}, "bindings");
34
+ const d1 = arrayField(raw, "d1_databases");
35
+ const vectorize = arrayField(raw, "vectorize");
36
+ return {
37
+ bindings: {
38
+ d1: d1.map((entry) => stringField(entry, "binding") ?? "<unnamed>"),
39
+ durableObjects: durableObjectBindings.map((entry) => stringField(entry, "name") ?? "<unnamed>"),
40
+ vectorize: vectorize.map((entry) => stringField(entry, "binding") ?? "<unnamed>")
41
+ },
42
+ compatibilityDate: stringField(raw, "compatibility_date"),
43
+ compatibilityFlags: arrayField(raw, "compatibility_flags").filter((entry) => typeof entry === "string"),
44
+ main: stringField(raw, "main"),
45
+ name: stringField(raw, "name")
46
+ };
47
+ };
48
+ const summariseSchema = (schema) => {
49
+ return {
50
+ tables: schema.tables.map((table) => {
51
+ let shard = "root";
52
+ if (table.shardMode === "global") {
53
+ shard = "global";
54
+ } else if (typeof table.shardMode === "object") {
55
+ shard = `shardBy(${table.shardMode.field})`;
56
+ }
57
+ return {
58
+ indexes: table.indexes.length,
59
+ name: table.name,
60
+ shard
61
+ };
62
+ }),
63
+ vectorIndexes: schema.vectorIndexes.length
64
+ };
65
+ };
66
+ const collectLunoraPackages = (projectRoot) => {
67
+ const pkgPath = join(projectRoot, "package.json");
68
+ if (!existsSync(pkgPath)) {
69
+ return [];
70
+ }
71
+ let pkg;
72
+ try {
73
+ pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
74
+ } catch {
75
+ return [];
76
+ }
77
+ if (pkg === null || typeof pkg !== "object") {
78
+ return [];
79
+ }
80
+ const sections = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
81
+ const seen = /* @__PURE__ */ new Map();
82
+ for (const section of sections) {
83
+ const block = pkg[section];
84
+ if (block === null || typeof block !== "object") {
85
+ continue;
86
+ }
87
+ for (const [name, version] of Object.entries(block)) {
88
+ if (!name.startsWith("@lunora/")) {
89
+ continue;
90
+ }
91
+ if (typeof version === "string" && !seen.has(name)) {
92
+ seen.set(name, version);
93
+ }
94
+ }
95
+ }
96
+ return [...seen.entries()].toSorted(([a], [b]) => a.localeCompare(b)).map(([name, version]) => {
97
+ return { name, version };
98
+ });
99
+ };
100
+ const collectInfo = (projectRoot) => {
101
+ const lunoraPackages = collectLunoraPackages(projectRoot);
102
+ const wranglerPath = findWranglerFile(projectRoot);
103
+ let wrangler;
104
+ if (wranglerPath) {
105
+ try {
106
+ wrangler = summariseWrangler(parse(readFileSync(wranglerPath, "utf8")));
107
+ } catch {
108
+ wrangler = void 0;
109
+ }
110
+ }
111
+ const schemaPath = join(projectRoot, "lunora", "schema.ts");
112
+ let schema;
113
+ let schemaError;
114
+ if (existsSync(schemaPath)) {
115
+ try {
116
+ const project = new Project({ skipAddingFilesFromTsConfig: true, useInMemoryFileSystem: false });
117
+ schema = summariseSchema(discoverSchema(project, schemaPath));
118
+ } catch (error) {
119
+ schemaError = error instanceof Error ? error.message : String(error);
120
+ }
121
+ }
122
+ return {
123
+ link: readLinkedProject(projectRoot),
124
+ lunoraPackages,
125
+ projectRoot,
126
+ schema,
127
+ schemaError,
128
+ wrangler,
129
+ wranglerPath
130
+ };
131
+ };
132
+ const renderText = (snapshot, logger) => {
133
+ logger.info(`project: ${snapshot.projectRoot}`);
134
+ logger.info("");
135
+ logger.info("@lunora/* packages:");
136
+ if (snapshot.lunoraPackages.length === 0) {
137
+ logger.info(" (none found in package.json)");
138
+ } else {
139
+ for (const pkg of snapshot.lunoraPackages) {
140
+ logger.info(` ${pkg.name}@${pkg.version}`);
141
+ }
142
+ }
143
+ logger.info("");
144
+ if (snapshot.wrangler) {
145
+ logger.info(`wrangler: ${snapshot.wranglerPath ?? ""}`);
146
+ logger.info(` name: ${snapshot.wrangler.name ?? "<unset>"}`);
147
+ logger.info(` main: ${snapshot.wrangler.main ?? "<unset>"}`);
148
+ logger.info(` compatibility_date: ${snapshot.wrangler.compatibilityDate ?? "<unset>"}`);
149
+ logger.info(` compatibility_flags: ${snapshot.wrangler.compatibilityFlags.join(", ") || "<none>"}`);
150
+ logger.info(` durable objects: ${snapshot.wrangler.bindings.durableObjects.join(", ") || "<none>"}`);
151
+ logger.info(` d1 databases: ${snapshot.wrangler.bindings.d1.join(", ") || "<none>"}`);
152
+ logger.info(` vectorize indexes: ${snapshot.wrangler.bindings.vectorize.join(", ") || "<none>"}`);
153
+ } else {
154
+ logger.info("wrangler: (not found)");
155
+ }
156
+ logger.info("");
157
+ if (snapshot.link) {
158
+ logger.info(`link: ${snapshot.link.workerName ?? "(unnamed)"} -> ${snapshot.link.workerUrl ?? "<no url>"}`);
159
+ if (snapshot.link.env !== void 0) {
160
+ logger.info(` env: ${snapshot.link.env}`);
161
+ }
162
+ } else {
163
+ logger.info("link: (not linked — run `lunora link --url <https://your-worker>`)");
164
+ }
165
+ logger.info("");
166
+ if (snapshot.schemaError !== void 0) {
167
+ logger.warn(`schema: parse error — ${snapshot.schemaError}`);
168
+ } else if (snapshot.schema) {
169
+ logger.info(`schema: ${String(snapshot.schema.tables.length)} table(s), ${String(snapshot.schema.vectorIndexes)} vector index(es)`);
170
+ for (const table of snapshot.schema.tables) {
171
+ logger.info(` ${table.name} [${table.shard}, ${String(table.indexes)} index(es)]`);
172
+ }
173
+ } else {
174
+ logger.info("schema: (no lunora/schema.ts)");
175
+ }
176
+ };
177
+ const runInfoCommand = (options) => {
178
+ const cwd = options.cwd ?? process.cwd();
179
+ const snapshot = collectInfo(cwd);
180
+ if (options.json) {
181
+ process.stdout.write(`${JSON.stringify(snapshot, void 0, 2)}
182
+ `);
183
+ } else {
184
+ renderText(snapshot, options.logger);
185
+ }
186
+ return { code: 0, snapshot };
187
+ };
188
+ const execute = defineHandler(
189
+ ({ cwd, logger, options }) => runInfoCommand({ cwd, json: options.json === true, logger })
190
+ );
191
+
192
+ export { collectInfo, execute, runInfoCommand };
@@ -0,0 +1,131 @@
1
+ import { r as resolveAdminBaseUrl } from '../packem_shared/admin-url-4UzT-CI4.mjs';
2
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
3
+ import { a as resolveProductionWorkerUrl } from '../packem_shared/resolve-target-qbsJ_5sF.mjs';
4
+
5
+ const GET_FUNCTION_STATS_OP = "__lunora_admin__:getFunctionStats";
6
+ const DEFAULT_LIMIT = 10;
7
+ const TRAILING_SLASH = /\/$/u;
8
+ const toInsightRow = (stat, rate) => {
9
+ return {
10
+ calls: stat.calls,
11
+ conflicts: stat.conflicts ?? 0,
12
+ errors: stat.errors,
13
+ lastErrorMessage: stat.lastErrorMessage,
14
+ maxDurationMs: stat.maxDurationMs,
15
+ meanDurationMs: stat.calls === 0 ? 0 : stat.totalDurationMs / stat.calls,
16
+ path: stat.path,
17
+ rate
18
+ };
19
+ };
20
+ const buildInsightsReport = (functions, limit) => {
21
+ const writeContention = functions.filter((stat) => (stat.conflicts ?? 0) > 0).map((stat) => toInsightRow(stat, stat.calls === 0 ? 0 : (stat.conflicts ?? 0) / stat.calls)).toSorted((a, b) => b.rate - a.rate || b.conflicts - a.conflicts).slice(0, limit);
22
+ const errorHotspots = functions.filter((stat) => stat.errors > 0).map((stat) => toInsightRow(stat, stat.calls === 0 ? 0 : stat.errors / stat.calls)).toSorted((a, b) => b.rate - a.rate || b.errors - a.errors).slice(0, limit);
23
+ const latencyOutliers = functions.map((stat) => toInsightRow(stat, 0)).toSorted((a, b) => b.maxDurationMs - a.maxDurationMs).slice(0, limit);
24
+ return { errorHotspots, latencyOutliers, totalFunctions: functions.length, writeContention };
25
+ };
26
+ const percent = (rate) => `${(rate * 100).toFixed(1)}%`;
27
+ const formatMs = (ms) => ms < 1e3 ? `${Math.round(ms).toString()}ms` : `${(ms / 1e3).toFixed(2)}s`;
28
+ const formatSection = (heading, rows, emptyNote, renderRow) => [
29
+ heading,
30
+ ...rows.length === 0 ? [` ${emptyNote}`] : rows.map((row) => ` ${renderRow(row)}`)
31
+ ];
32
+ const formatInsightsReport = (report) => {
33
+ const errorTail = (row) => row.lastErrorMessage ? ` — ${row.lastErrorMessage}` : "";
34
+ return [
35
+ `Insights over ${report.totalFunctions.toString()} function${report.totalFunctions === 1 ? "" : "s"}`,
36
+ "",
37
+ ...formatSection(
38
+ "Write-conflict hot-spots (OCC contention — candidates for sharding):",
39
+ report.writeContention,
40
+ "none — no write conflicts observed",
41
+ (row) => `${row.path} ${row.conflicts.toString()}/${row.calls.toString()} calls (${percent(row.rate)})`
42
+ ),
43
+ "",
44
+ ...formatSection(
45
+ "Error hot-spots:",
46
+ report.errorHotspots,
47
+ "none — no errors observed",
48
+ (row) => `${row.path} ${row.errors.toString()}/${row.calls.toString()} calls (${percent(row.rate)})${errorTail(row)}`
49
+ ),
50
+ "",
51
+ ...formatSection(
52
+ "Latency outliers (slowest single call):",
53
+ report.latencyOutliers,
54
+ "none — no functions have run",
55
+ (row) => `${row.path} max ${formatMs(row.maxDurationMs)}, mean ${formatMs(row.meanDurationMs)} over ${row.calls.toString()} calls`
56
+ )
57
+ ].join("\n");
58
+ };
59
+ const resolveLimit = (raw) => {
60
+ if (raw === void 0 || !Number.isFinite(raw) || raw <= 0) {
61
+ return DEFAULT_LIMIT;
62
+ }
63
+ return Math.floor(raw);
64
+ };
65
+ const runInsightsCommand = async (options) => {
66
+ if (options.prod && options.url === void 0) {
67
+ options.logger.error("--prod requires an explicit --url (refusing to report from the implicit localhost worker)");
68
+ return { code: 1 };
69
+ }
70
+ const token = options.token ?? process.env.LUNORA_ADMIN_TOKEN;
71
+ if (!token) {
72
+ options.logger.error("admin token required — pass --token or set LUNORA_ADMIN_TOKEN");
73
+ return { code: 1 };
74
+ }
75
+ const baseUrl = resolveAdminBaseUrl(options.url, options.logger);
76
+ if (baseUrl === void 0) {
77
+ return { code: 1 };
78
+ }
79
+ const requestUrl = `${baseUrl.replace(TRAILING_SLASH, "")}/_lunora/rpc`;
80
+ const fetchImpl = globalThis.fetch;
81
+ if (typeof fetchImpl !== "function") {
82
+ throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");
83
+ }
84
+ const payload = { args: {}, functionPath: GET_FUNCTION_STATS_OP };
85
+ if (options.shard !== void 0) {
86
+ payload.shardKey = options.shard;
87
+ }
88
+ options.logger.info(`POST ${requestUrl} -> insights`);
89
+ const response = await fetchImpl(requestUrl, {
90
+ body: JSON.stringify(payload),
91
+ headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
92
+ method: "POST"
93
+ });
94
+ const text = await response.text();
95
+ if (!response.ok) {
96
+ options.logger.error(`insights failed: HTTP ${String(response.status)}: ${text}`);
97
+ return { code: 1 };
98
+ }
99
+ let parsed;
100
+ try {
101
+ parsed = JSON.parse(text);
102
+ } catch {
103
+ options.logger.error(`insights failed: worker returned non-JSON: ${text}`);
104
+ return { code: 1 };
105
+ }
106
+ const result = parsed.result ?? parsed;
107
+ const { functions } = result;
108
+ if (!Array.isArray(functions)) {
109
+ options.logger.error("insights failed: response carried no `functions` array");
110
+ return { code: 1 };
111
+ }
112
+ const report = buildInsightsReport(functions, resolveLimit(options.limit));
113
+ options.logger.info(options.json ? JSON.stringify(report, void 0, 2) : formatInsightsReport(report));
114
+ return { code: 0, report };
115
+ };
116
+ const execute = defineHandler(({ cwd, logger, options }) => {
117
+ const limit = options.limit === void 0 ? void 0 : Number.parseInt(options.limit, 10);
118
+ return runInsightsCommand({
119
+ json: options.json,
120
+ limit,
121
+ logger,
122
+ prod: options.prod,
123
+ shard: options.shard,
124
+ token: options.token,
125
+ // Fall back to the `.lunora/project.json` link when `--prod` is set, so a
126
+ // linked checkout doesn't need --url repeated for prod insights.
127
+ url: resolveProductionWorkerUrl({ cwd, prod: options.prod === true, url: options.url })
128
+ });
129
+ });
130
+
131
+ export { buildInsightsReport, execute, formatInsightsReport, runInsightsCommand };
@@ -0,0 +1,65 @@
1
+ import { existsSync, rmSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { writeLinkedProject, LINKED_PROJECT_FILE } from '@lunora/config';
4
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
5
+ import { r as readWranglerName } from '../packem_shared/wrangler-name-cy4yhm9j.mjs';
6
+
7
+ const isValidWorkerUrl = (value) => {
8
+ try {
9
+ const { protocol } = new URL(value);
10
+ return protocol === "http:" || protocol === "https:";
11
+ } catch {
12
+ return false;
13
+ }
14
+ };
15
+ const runLinkRemove = (cwd, logger) => {
16
+ const path = join(cwd, LINKED_PROJECT_FILE);
17
+ if (!existsSync(path)) {
18
+ logger.warn(`link: no ${LINKED_PROJECT_FILE} to remove`);
19
+ return { code: 0 };
20
+ }
21
+ rmSync(path);
22
+ logger.success(`link: removed ${LINKED_PROJECT_FILE}`);
23
+ return { code: 0 };
24
+ };
25
+ const runLinkCommand = (options) => {
26
+ const cwd = options.cwd ?? process.cwd();
27
+ const { logger } = options;
28
+ if (options.remove) {
29
+ return runLinkRemove(cwd, logger);
30
+ }
31
+ if (options.url === void 0 || options.url === "") {
32
+ logger.error("link requires a deployed Worker URL. Usage: lunora link --url <https://your-worker>");
33
+ return { code: 1 };
34
+ }
35
+ if (!isValidWorkerUrl(options.url)) {
36
+ logger.error(`link: invalid --url "${options.url}" — expected an http(s) URL`);
37
+ return { code: 1 };
38
+ }
39
+ const now = options.now ?? (() => (/* @__PURE__ */ new Date()).toISOString());
40
+ const link = {
41
+ env: options.env,
42
+ linkedAt: now(),
43
+ workerName: options.name ?? readWranglerName(cwd),
44
+ workerUrl: options.url
45
+ };
46
+ const path = writeLinkedProject(cwd, link);
47
+ logger.success(`link: ${link.workerName ?? "(unnamed worker)"} -> ${options.url}`);
48
+ logger.info(`link: wrote ${path}`);
49
+ if (link.env !== void 0) {
50
+ logger.info(`link: environment "${link.env}"`);
51
+ }
52
+ return { code: 0, link };
53
+ };
54
+ const execute = defineHandler(
55
+ ({ cwd, logger, options }) => runLinkCommand({
56
+ cwd,
57
+ env: options.env,
58
+ logger,
59
+ name: options.name,
60
+ remove: options.remove === true,
61
+ url: options.url
62
+ })
63
+ );
64
+
65
+ export { execute, runLinkCommand };
@@ -0,0 +1,58 @@
1
+ import { readLinkedProject } from '@lunora/config';
2
+ import { d as defineHandler } from '../packem_shared/command-BC30oSBW.mjs';
3
+ import { defaultSpawner } from '../packem_shared/createRecordingSpawner-DxI3mebw.mjs';
4
+
5
+ const LOG_FORMATS = /* @__PURE__ */ new Set(["json", "pretty"]);
6
+ const runLogsCommand = async (options) => {
7
+ const cwd = options.cwd ?? process.cwd();
8
+ if (options.format !== void 0 && !LOG_FORMATS.has(options.format)) {
9
+ options.logger.error(`logs: unknown --format "${options.format}" — expected pretty | json`);
10
+ return { code: 1, descriptor: void 0, error: "invalid format" };
11
+ }
12
+ const args = ["exec", "wrangler", "tail"];
13
+ if (options.worker !== void 0) {
14
+ args.push(options.worker);
15
+ }
16
+ const env = options.env ?? readLinkedProject(cwd)?.env;
17
+ if (env !== void 0) {
18
+ args.push("--env", env);
19
+ }
20
+ if (options.format !== void 0) {
21
+ args.push("--format", options.format);
22
+ }
23
+ if (options.status !== void 0) {
24
+ args.push("--status", options.status);
25
+ }
26
+ if (options.search !== void 0) {
27
+ args.push("--search", options.search);
28
+ }
29
+ if (options.temporary) {
30
+ args.push("--temporary");
31
+ }
32
+ const descriptor = {
33
+ args,
34
+ command: "pnpm",
35
+ cwd
36
+ };
37
+ options.logger.info(`tailing logs via ${descriptor.command} ${descriptor.args.join(" ")}`);
38
+ const spawner = options.spawner ?? defaultSpawner;
39
+ const result = await spawner(descriptor);
40
+ return {
41
+ code: result.code,
42
+ descriptor
43
+ };
44
+ };
45
+ const execute = defineHandler(
46
+ ({ argument, cwd, logger, options }) => runLogsCommand({
47
+ cwd,
48
+ env: options.env,
49
+ format: options.format,
50
+ logger,
51
+ search: options.search,
52
+ status: options.status,
53
+ temporary: options.temporary === true,
54
+ worker: argument[0]
55
+ })
56
+ );
57
+
58
+ export { execute, runLogsCommand };