@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.
- package/LICENSE.md +105 -0
- package/README.md +109 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/bin.mjs +11 -0
- package/dist/index.d.mts +852 -0
- package/dist/index.d.ts +852 -0
- package/dist/index.mjs +19 -0
- package/dist/packem_chunks/handler.mjs +76 -0
- package/dist/packem_chunks/handler10.mjs +22 -0
- package/dist/packem_chunks/handler11.mjs +192 -0
- package/dist/packem_chunks/handler12.mjs +131 -0
- package/dist/packem_chunks/handler13.mjs +65 -0
- package/dist/packem_chunks/handler14.mjs +58 -0
- package/dist/packem_chunks/handler15.mjs +79 -0
- package/dist/packem_chunks/handler16.mjs +41 -0
- package/dist/packem_chunks/handler17.mjs +105 -0
- package/dist/packem_chunks/handler18.mjs +172 -0
- package/dist/packem_chunks/handler19.mjs +89 -0
- package/dist/packem_chunks/handler2.mjs +114 -0
- package/dist/packem_chunks/handler20.mjs +94 -0
- package/dist/packem_chunks/handler21.mjs +311 -0
- package/dist/packem_chunks/handler3.mjs +204 -0
- package/dist/packem_chunks/handler4.mjs +33 -0
- package/dist/packem_chunks/handler5.mjs +49 -0
- package/dist/packem_chunks/handler6.mjs +91 -0
- package/dist/packem_chunks/handler7.mjs +42 -0
- package/dist/packem_chunks/handler8.mjs +174 -0
- package/dist/packem_chunks/handler9.mjs +16 -0
- package/dist/packem_chunks/planDevCommand.mjs +543 -0
- package/dist/packem_chunks/runCodegenCommand.mjs +52 -0
- package/dist/packem_chunks/runDeployCommand.mjs +504 -0
- package/dist/packem_chunks/runInitCommand.mjs +652 -0
- package/dist/packem_chunks/runMigrateGenerateCommand.mjs +397 -0
- package/dist/packem_chunks/runResetCommand.mjs +41 -0
- package/dist/packem_chunks/runRpcCommand.mjs +68 -0
- package/dist/packem_shared/COMMANDS-1V_KEx35.mjs +905 -0
- package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +244 -0
- package/dist/packem_shared/admin-url-4UzT-CI4.mjs +19 -0
- package/dist/packem_shared/api-spec-CtA6ilu4.mjs +13 -0
- package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +38 -0
- package/dist/packem_shared/command-BDXcJCCJ.mjs +14 -0
- package/dist/packem_shared/createLogger-CHPNjFw2.mjs +73 -0
- package/dist/packem_shared/defaultSpawner-DxI3mebw.mjs +43 -0
- package/dist/packem_shared/diffSnapshots-RR2ZE8Ya.mjs +161 -0
- package/dist/packem_shared/docker-hMQ97KSQ.mjs +21 -0
- package/dist/packem_shared/features-ocSSpZtS.mjs +24 -0
- package/dist/packem_shared/insertSchemaExtension-BuzF6-t2.mjs +59 -0
- package/dist/packem_shared/open-url-Dfq6fAyT.mjs +41 -0
- package/dist/packem_shared/output-format-7gyGR3h8.mjs +17 -0
- package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +56 -0
- package/dist/packem_shared/parseManifest--vZf2FY1.mjs +94 -0
- package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +16 -0
- package/dist/packem_shared/runAddCommand-BZGkRnBs.mjs +693 -0
- package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +79 -0
- package/dist/packem_shared/schemaIrToSnapshot-aBTo7TM5.mjs +43 -0
- package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +12 -0
- package/package.json +61 -18
- package/skills/README.md +29 -0
- package/skills/lunora/SKILL.md +83 -0
- package/skills/lunora-create-package/SKILL.md +129 -0
- package/skills/lunora-deploy/SKILL.md +150 -0
- package/skills/lunora-functions/SKILL.md +182 -0
- package/skills/lunora-migration-helper/SKILL.md +194 -0
- package/skills/lunora-performance-audit/SKILL.md +143 -0
- package/skills/lunora-quickstart/SKILL.md +240 -0
- package/skills/lunora-realtime/SKILL.md +177 -0
- package/skills/lunora-setup-auth/SKILL.md +170 -0
- package/skills/lunora-setup-hyperdrive/SKILL.md +154 -0
- package/skills/lunora-setup-hyperdrive-global/SKILL.md +171 -0
- package/skills/lunora-setup-mail/SKILL.md +151 -0
- package/skills/lunora-setup-scheduler/SKILL.md +157 -0
- package/skills/lunora-setup-storage/SKILL.md +154 -0
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { detectAgentRules, inferLunoraBindings, packageNamesFromBindings, ensureDevVarsExample, ensureDevVariables, createConfirm, isInteractive, DEV_VARS_FILE, DEV_VARS_EXAMPLE_FILE, claimAgentRulesHint, AGENT_RULES_HINT, materializeRemoteWranglerConfig, formatLunoraEvent, resolveRemoteEnabled, readProjectRemotePreference } from '@lunora/config';
|
|
3
|
+
import { p as parseApiSpec } from '../packem_shared/api-spec-CtA6ilu4.mjs';
|
|
4
|
+
import { existsSync, watch, readFileSync } from 'node:fs';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { runCodegen } from '@lunora/codegen';
|
|
7
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
8
|
+
import { dirname, join as join$1 } from '@visulima/path';
|
|
9
|
+
import { createServer, request } from 'node:http';
|
|
10
|
+
import { connect } from 'node:net';
|
|
11
|
+
import { loadStudioAssets, studioAssetsStamp, renderStudioHtml, resolveAdminToken, SCHEMA_EDIT_ENDPOINT, POLICY_SCAFFOLD_ENDPOINT, SEED_ENDPOINT, serveJsonHandler, handleSchemaEditRequest, handlePolicyScaffoldRequest, handleSeedRequest } from '@lunora/config/studio-host';
|
|
12
|
+
|
|
13
|
+
const DEFAULT_DEBOUNCE_MS = 100;
|
|
14
|
+
const PATH_SEGMENT_SEPARATOR = /[/\\]/u;
|
|
15
|
+
const runOnce = (projectRoot, lunoraDirectory, apiSpec, logger, reason) => {
|
|
16
|
+
try {
|
|
17
|
+
runCodegen({ apiSpec, lunoraDirectory, projectRoot });
|
|
18
|
+
logger.success(`codegen: wrote ${lunoraDirectory}/_generated (${reason})`);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
logger.error(`codegen failed (${reason}): ${error instanceof Error ? error.message : String(error)}`);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const startCodegenWatch = (options) => {
|
|
24
|
+
const lunoraDirectory = options.lunoraDirectory ?? "lunora";
|
|
25
|
+
const watchDirectory = join(options.projectRoot, lunoraDirectory);
|
|
26
|
+
const debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
|
|
27
|
+
const { apiSpec } = options;
|
|
28
|
+
runOnce(options.projectRoot, lunoraDirectory, apiSpec, options.logger, "startup");
|
|
29
|
+
let timer;
|
|
30
|
+
let watcher;
|
|
31
|
+
if (!existsSync(watchDirectory)) {
|
|
32
|
+
options.logger.warn(
|
|
33
|
+
`codegen watch unavailable (no such directory: ${watchDirectory}) — schema edits will NOT auto-regenerate. Run \`lunora codegen\` manually after each edit.`
|
|
34
|
+
);
|
|
35
|
+
return {
|
|
36
|
+
close: () => {
|
|
37
|
+
},
|
|
38
|
+
watchAvailable: false
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
watcher = watch(watchDirectory, { recursive: true }, (_event, filename) => {
|
|
43
|
+
if (typeof filename === "string" && filename.split(PATH_SEGMENT_SEPARATOR).includes("_generated")) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (timer) {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
}
|
|
49
|
+
timer = setTimeout(runOnce, debounceMs, options.projectRoot, lunoraDirectory, apiSpec, options.logger, `change: ${filename ?? "?"}`);
|
|
50
|
+
});
|
|
51
|
+
} catch (error) {
|
|
52
|
+
options.logger.warn(
|
|
53
|
+
`codegen watch unavailable (${error instanceof Error ? error.message : String(error)}) — schema edits will NOT auto-regenerate. Run \`lunora codegen\` manually after each edit.`
|
|
54
|
+
);
|
|
55
|
+
return {
|
|
56
|
+
close: () => {
|
|
57
|
+
},
|
|
58
|
+
watchAvailable: false
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const liveWatcher = watcher;
|
|
62
|
+
return {
|
|
63
|
+
close: () => {
|
|
64
|
+
if (timer) {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
}
|
|
67
|
+
liveWatcher.close();
|
|
68
|
+
},
|
|
69
|
+
watchAvailable: true
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const FALLBACK = "pnpm";
|
|
74
|
+
const KNOWN_MANAGERS = ["pnpm", "yarn", "npm", "bun"];
|
|
75
|
+
const parseDeclaredManager = (declared) => {
|
|
76
|
+
if (typeof declared !== "string") {
|
|
77
|
+
return void 0;
|
|
78
|
+
}
|
|
79
|
+
return KNOWN_MANAGERS.find((manager) => declared.startsWith(`${manager}@`));
|
|
80
|
+
};
|
|
81
|
+
const readDeclaredManager = (directory) => {
|
|
82
|
+
const candidate = join$1(directory, "package.json");
|
|
83
|
+
if (!existsSync(candidate)) {
|
|
84
|
+
return void 0;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const parsed = JSON.parse(readFileSync(candidate, "utf8"));
|
|
88
|
+
return parseDeclaredManager(parsed.packageManager);
|
|
89
|
+
} catch {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const detectPackageManager = (startDirectory) => {
|
|
94
|
+
let directory = startDirectory;
|
|
95
|
+
while (directory && directory !== dirname(directory)) {
|
|
96
|
+
const declared = readDeclaredManager(directory);
|
|
97
|
+
if (declared !== void 0) {
|
|
98
|
+
return declared;
|
|
99
|
+
}
|
|
100
|
+
directory = dirname(directory);
|
|
101
|
+
}
|
|
102
|
+
return FALLBACK;
|
|
103
|
+
};
|
|
104
|
+
const execArgsFor = (manager, command, args) => {
|
|
105
|
+
if (manager === "yarn") {
|
|
106
|
+
return { args: [command, ...args], command: "yarn" };
|
|
107
|
+
}
|
|
108
|
+
if (manager === "bun") {
|
|
109
|
+
return { args: ["x", command, ...args], command: "bun" };
|
|
110
|
+
}
|
|
111
|
+
if (manager === "npm") {
|
|
112
|
+
return { args: ["--", command, ...args], command: "npx" };
|
|
113
|
+
}
|
|
114
|
+
return { args: ["exec", command, ...args], command: "pnpm" };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const PROXY_PREFIX = "/_lunora";
|
|
118
|
+
const pathnameOf = (url) => {
|
|
119
|
+
const queryIndex = url.indexOf("?");
|
|
120
|
+
return queryIndex === -1 ? url : url.slice(0, queryIndex);
|
|
121
|
+
};
|
|
122
|
+
const LOOPBACK_HOST_NAMES = /* @__PURE__ */ new Set(["127.0.0.1", "::1", "[::1]", "localhost"]);
|
|
123
|
+
const isLoopbackHost = (hostHeader) => {
|
|
124
|
+
if (hostHeader === void 0 || hostHeader === "") {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
const withoutPort = hostHeader.startsWith("[") ? hostHeader.slice(0, hostHeader.indexOf("]") + 1) : hostHeader.split(":")[0] ?? hostHeader;
|
|
128
|
+
return LOOPBACK_HOST_NAMES.has(withoutPort);
|
|
129
|
+
};
|
|
130
|
+
const proxyHttp = (request$1, response, worker) => {
|
|
131
|
+
const upstream = request(
|
|
132
|
+
{
|
|
133
|
+
headers: { ...request$1.headers, host: worker.host },
|
|
134
|
+
hostname: worker.hostname,
|
|
135
|
+
method: request$1.method,
|
|
136
|
+
path: request$1.url,
|
|
137
|
+
port: worker.port
|
|
138
|
+
},
|
|
139
|
+
(upstreamResponse) => {
|
|
140
|
+
response.writeHead(upstreamResponse.statusCode ?? 502, upstreamResponse.headers);
|
|
141
|
+
upstreamResponse.pipe(response);
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
upstream.on("error", (error) => {
|
|
145
|
+
response.statusCode = 502;
|
|
146
|
+
response.end(`lunora dev: worker unreachable (${error.message})`);
|
|
147
|
+
});
|
|
148
|
+
request$1.pipe(upstream);
|
|
149
|
+
};
|
|
150
|
+
const UPGRADE_CONNECT_TIMEOUT_MS = 5e3;
|
|
151
|
+
const proxyUpgrade = (request, clientSocket, head, worker) => {
|
|
152
|
+
const upstream = connect({ host: worker.hostname, port: Number(worker.port) }, () => {
|
|
153
|
+
upstream.setTimeout(0);
|
|
154
|
+
const lines = [`${request.method ?? "GET"} ${request.url ?? "/"} HTTP/1.1`];
|
|
155
|
+
for (const [key, value] of Object.entries(request.headers)) {
|
|
156
|
+
if (key === "host") {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
160
|
+
if (item !== void 0) {
|
|
161
|
+
lines.push(`${key}: ${item}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
lines.push(`host: ${worker.host}`, "", "");
|
|
166
|
+
upstream.write(lines.join("\r\n"));
|
|
167
|
+
if (head.length > 0) {
|
|
168
|
+
upstream.write(head);
|
|
169
|
+
}
|
|
170
|
+
upstream.pipe(clientSocket);
|
|
171
|
+
clientSocket.pipe(upstream);
|
|
172
|
+
});
|
|
173
|
+
upstream.setTimeout(UPGRADE_CONNECT_TIMEOUT_MS, () => upstream.destroy());
|
|
174
|
+
upstream.on("error", () => clientSocket.destroy());
|
|
175
|
+
clientSocket.on("error", () => upstream.destroy());
|
|
176
|
+
};
|
|
177
|
+
const startStudioServer = async (options) => {
|
|
178
|
+
const host = options.host ?? "127.0.0.1";
|
|
179
|
+
const isLoopback = host === "127.0.0.1" || host === "localhost" || host === "::1";
|
|
180
|
+
const worker = new URL(options.workerOrigin);
|
|
181
|
+
let assets = loadStudioAssets(options.logger, import.meta.url);
|
|
182
|
+
let assetsStamp = studioAssetsStamp(import.meta.url);
|
|
183
|
+
const html = renderStudioHtml({
|
|
184
|
+
// The admin token grants full read/write over the local worker. Only embed
|
|
185
|
+
// it for a loopback bind: a non-loopback (LAN/0.0.0.0) bind would otherwise
|
|
186
|
+
// hand the token to any network client that GETs `/`.
|
|
187
|
+
adminToken: isLoopback ? resolveAdminToken(options.cwd) : void 0,
|
|
188
|
+
basePath: "/",
|
|
189
|
+
dataEditable: isLoopback,
|
|
190
|
+
rulesInstalled: detectAgentRules(options.cwd).installed,
|
|
191
|
+
runAsIdentity: isLoopback,
|
|
192
|
+
schemaEditable: isLoopback,
|
|
193
|
+
scriptSrc: "/studio.js",
|
|
194
|
+
styleHref: "/styles.css"
|
|
195
|
+
});
|
|
196
|
+
const sendAsset = (response, body, contentType) => {
|
|
197
|
+
response.statusCode = 200;
|
|
198
|
+
response.setHeader("Content-Type", contentType);
|
|
199
|
+
response.end(body);
|
|
200
|
+
};
|
|
201
|
+
const serveLoopbackOnly = (request, response, handle, deniedMessage) => {
|
|
202
|
+
if (isLoopback) {
|
|
203
|
+
serveJsonHandler(request, response, handle, options.cwd);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
response.statusCode = 403;
|
|
207
|
+
response.setHeader("Content-Type", "text/plain");
|
|
208
|
+
response.end(deniedMessage);
|
|
209
|
+
};
|
|
210
|
+
const serveStaticAsset = (pathname, response) => {
|
|
211
|
+
if (pathname !== "/studio.js" && pathname !== "/styles.css") {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
const stamp = studioAssetsStamp(import.meta.url);
|
|
215
|
+
if (stamp !== assetsStamp) {
|
|
216
|
+
assets = loadStudioAssets(options.logger, import.meta.url);
|
|
217
|
+
assetsStamp = stamp;
|
|
218
|
+
}
|
|
219
|
+
if (assets === void 0) {
|
|
220
|
+
response.statusCode = 501;
|
|
221
|
+
response.setHeader("Content-Type", "text/plain");
|
|
222
|
+
response.end("Lunora studio assets not found — install and build @lunora/studio.");
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
const isScript = pathname === "/studio.js";
|
|
226
|
+
sendAsset(response, isScript ? assets.script : assets.styles, isScript ? "text/javascript; charset=utf-8" : "text/css; charset=utf-8");
|
|
227
|
+
return true;
|
|
228
|
+
};
|
|
229
|
+
const document = Buffer.from(html);
|
|
230
|
+
const server = createServer((request, response) => {
|
|
231
|
+
const pathname = pathnameOf(request.url ?? "/");
|
|
232
|
+
if (isLoopback && !isLoopbackHost(request.headers.host)) {
|
|
233
|
+
response.statusCode = 403;
|
|
234
|
+
response.setHeader("Content-Type", "text/plain");
|
|
235
|
+
response.end("Lunora studio: refusing a request whose Host is not a loopback literal (DNS-rebinding guard).");
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (pathname.startsWith(PROXY_PREFIX)) {
|
|
239
|
+
if (!isLoopback) {
|
|
240
|
+
response.statusCode = 403;
|
|
241
|
+
response.setHeader("Content-Type", "text/plain");
|
|
242
|
+
response.end("Lunora studio: the worker admin proxy is only available on a loopback bind.");
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
proxyHttp(request, response, worker);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (pathname === SCHEMA_EDIT_ENDPOINT) {
|
|
249
|
+
serveLoopbackOnly(request, response, handleSchemaEditRequest, "Lunora schema editing is only available on loopback hosts in dev.");
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (pathname === POLICY_SCAFFOLD_ENDPOINT) {
|
|
253
|
+
serveLoopbackOnly(request, response, handlePolicyScaffoldRequest, "Lunora policy scaffolding is only available on loopback hosts in dev.");
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (pathname === SEED_ENDPOINT) {
|
|
257
|
+
serveLoopbackOnly(request, response, handleSeedRequest, "Lunora data seeding is only available on loopback hosts in dev.");
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (serveStaticAsset(pathname, response)) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
sendAsset(response, document, "text/html; charset=utf-8");
|
|
264
|
+
});
|
|
265
|
+
server.on("upgrade", (request, socket, head) => {
|
|
266
|
+
if (!isLoopback || !isLoopbackHost(request.headers.host)) {
|
|
267
|
+
socket.destroy();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (pathnameOf(request.url ?? "").startsWith(PROXY_PREFIX)) {
|
|
271
|
+
proxyUpgrade(request, socket, head, worker);
|
|
272
|
+
} else {
|
|
273
|
+
socket.destroy();
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
await new Promise((resolve, reject) => {
|
|
277
|
+
server.once("error", (error) => {
|
|
278
|
+
if (error.code === "EADDRINUSE") {
|
|
279
|
+
reject(
|
|
280
|
+
new Error(`studio port ${String(options.port)} is already in use — pass a different port with --port, or stop the process using it`, {
|
|
281
|
+
cause: error
|
|
282
|
+
})
|
|
283
|
+
);
|
|
284
|
+
} else {
|
|
285
|
+
reject(error);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
server.listen(options.port, host, () => {
|
|
289
|
+
resolve();
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
return {
|
|
293
|
+
close: () => new Promise((resolve) => {
|
|
294
|
+
server.close(() => {
|
|
295
|
+
resolve();
|
|
296
|
+
});
|
|
297
|
+
}),
|
|
298
|
+
url: `http://${host === "0.0.0.0" || host === "::" ? "localhost" : host}:${String(options.port)}`
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const DEFAULT_STUDIO_PORT = 6173;
|
|
303
|
+
const DEFAULT_WORKER_PORT = 8787;
|
|
304
|
+
const SIGINT_GRACE_MS = 5e3;
|
|
305
|
+
const resolveRemotePlan = (options, cwd) => {
|
|
306
|
+
const noopCleanup = () => {
|
|
307
|
+
};
|
|
308
|
+
if (!options.remote) {
|
|
309
|
+
return { args: [], plan: { bindings: [], cleanup: noopCleanup, enabled: false } };
|
|
310
|
+
}
|
|
311
|
+
const materialize = options.materializeRemote ?? materializeRemoteWranglerConfig;
|
|
312
|
+
const result = materialize({ enabled: true, projectRoot: cwd });
|
|
313
|
+
const bindings = result.remoteBindings.map((binding) => `${binding.binding} (${binding.kind})`);
|
|
314
|
+
const { cleanup } = result;
|
|
315
|
+
if (result.configPath === void 0) {
|
|
316
|
+
return { args: [], plan: { bindings, cleanup, enabled: true, reason: result.reason } };
|
|
317
|
+
}
|
|
318
|
+
return { args: ["--config", result.configPath], plan: { bindings, cleanup, enabled: true } };
|
|
319
|
+
};
|
|
320
|
+
const planDevCommand = (options) => {
|
|
321
|
+
const cwd = options.cwd ?? process.cwd();
|
|
322
|
+
const workerPort = options.workerPort ?? DEFAULT_WORKER_PORT;
|
|
323
|
+
const manager = detectPackageManager(cwd);
|
|
324
|
+
const remote = resolveRemotePlan(options, cwd);
|
|
325
|
+
const exec = execArgsFor(manager, "wrangler", ["dev", "--port", String(workerPort), "--var", "WORKER_ENV:development", ...remote.args]);
|
|
326
|
+
return {
|
|
327
|
+
codegenEnabled: options.codegen !== false,
|
|
328
|
+
remote: remote.plan,
|
|
329
|
+
studioEnabled: options.studio !== false,
|
|
330
|
+
studioPort: options.port ?? DEFAULT_STUDIO_PORT,
|
|
331
|
+
workerOrigin: `http://localhost:${String(workerPort)}`,
|
|
332
|
+
workerPort,
|
|
333
|
+
wrangler: { args: exec.args, command: exec.command, cwd, tag: "wrangler" }
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
const emitChildLine = (line, tag, kind, logger) => {
|
|
337
|
+
if (line.length === 0) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const formatted = formatLunoraEvent(line);
|
|
341
|
+
if (formatted) {
|
|
342
|
+
logger[formatted.level](`[lunora] ${formatted.text}`);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const prefixed = `[${tag}] ${line}`;
|
|
346
|
+
if (kind === "stderr") {
|
|
347
|
+
logger.warn(prefixed);
|
|
348
|
+
} else {
|
|
349
|
+
logger.info(prefixed);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
const pipeChildOutput = (child, tag, logger) => {
|
|
353
|
+
const pumpStream = (stream, kind) => {
|
|
354
|
+
if (!stream) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
let buffer = "";
|
|
358
|
+
stream.on("data", (chunk) => {
|
|
359
|
+
buffer += chunk.toString("utf8");
|
|
360
|
+
const lines = buffer.split("\n");
|
|
361
|
+
buffer = lines.pop() ?? "";
|
|
362
|
+
for (const line of lines) {
|
|
363
|
+
emitChildLine(line.trimEnd(), tag, kind, logger);
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
stream.on("end", () => {
|
|
367
|
+
emitChildLine(buffer.trimEnd(), tag, kind, logger);
|
|
368
|
+
buffer = "";
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
pumpStream(child.stdout, "stdout");
|
|
372
|
+
pumpStream(child.stderr, "stderr");
|
|
373
|
+
};
|
|
374
|
+
const defaultWorkerSpawner = (descriptor, logger) => {
|
|
375
|
+
const child = spawn(descriptor.command, [...descriptor.args], {
|
|
376
|
+
cwd: descriptor.cwd ?? process.cwd(),
|
|
377
|
+
env: process.env,
|
|
378
|
+
stdio: ["inherit", "pipe", "pipe"]
|
|
379
|
+
});
|
|
380
|
+
pipeChildOutput(child, descriptor.tag, logger);
|
|
381
|
+
return {
|
|
382
|
+
exited: new Promise((resolve) => {
|
|
383
|
+
child.on("error", (error) => {
|
|
384
|
+
logger.error(`[${descriptor.tag}] failed to start: ${error.message}`);
|
|
385
|
+
resolve(1);
|
|
386
|
+
});
|
|
387
|
+
child.on("exit", (code) => {
|
|
388
|
+
resolve(code ?? 0);
|
|
389
|
+
});
|
|
390
|
+
}),
|
|
391
|
+
kill: (signal) => {
|
|
392
|
+
try {
|
|
393
|
+
child.kill(signal);
|
|
394
|
+
} catch {
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
const printBanner = (logger, plan, studioUrl) => {
|
|
400
|
+
logger.info("");
|
|
401
|
+
logger.success("Lunora dev");
|
|
402
|
+
logger.info(` ➜ Worker: ${plan.workerOrigin}`);
|
|
403
|
+
if (studioUrl !== void 0) {
|
|
404
|
+
logger.info(` ➜ Studio: ${studioUrl}`);
|
|
405
|
+
}
|
|
406
|
+
if (plan.codegenEnabled) {
|
|
407
|
+
logger.info(" ➜ Codegen: watching lunora/");
|
|
408
|
+
}
|
|
409
|
+
if (plan.remote.enabled) {
|
|
410
|
+
if (plan.remote.bindings.length > 0) {
|
|
411
|
+
logger.info(` ➜ Remote: ${plan.remote.bindings.join(", ")} → deployed worker`);
|
|
412
|
+
} else {
|
|
413
|
+
logger.warn(` ➜ Remote: requested but inactive (${plan.remote.reason ?? "no eligible bindings"}) — running fully local`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
logger.info("");
|
|
417
|
+
};
|
|
418
|
+
const printAgentRulesHint = (logger, cwd) => {
|
|
419
|
+
if (detectAgentRules(cwd).installed || !claimAgentRulesHint()) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
logger.info(` ⓘ ${AGENT_RULES_HINT}`);
|
|
423
|
+
logger.info("");
|
|
424
|
+
};
|
|
425
|
+
const teardown = async (handles) => {
|
|
426
|
+
handles.codegen?.close();
|
|
427
|
+
await handles.studio?.close().catch(() => void 0);
|
|
428
|
+
try {
|
|
429
|
+
handles.remoteCleanup?.();
|
|
430
|
+
} catch {
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
const offerDevVariablesScaffold = async (options, cwd) => {
|
|
434
|
+
try {
|
|
435
|
+
const bindings = await inferLunoraBindings({ projectRoot: cwd });
|
|
436
|
+
const packageNames = packageNamesFromBindings(bindings);
|
|
437
|
+
const addedKeys = (options.ensureExample ?? ensureDevVarsExample)(cwd, packageNames);
|
|
438
|
+
if (addedKeys.length > 0) {
|
|
439
|
+
options.logger.info(`Updated .dev.vars.example with secrets for: ${packageNames.join(", ")} (${addedKeys.join(", ")})`);
|
|
440
|
+
}
|
|
441
|
+
} catch {
|
|
442
|
+
}
|
|
443
|
+
const result = await (options.ensureEnv ?? ensureDevVariables)({
|
|
444
|
+
confirm: createConfirm(),
|
|
445
|
+
cwd,
|
|
446
|
+
info: (message) => {
|
|
447
|
+
options.logger.info(message);
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
if (result.status === "declined" && !isInteractive()) {
|
|
451
|
+
options.logger.info(
|
|
452
|
+
`hint: ${DEV_VARS_FILE} was not scaffolded (non-interactive run). Copy ${DEV_VARS_EXAMPLE_FILE} → ${DEV_VARS_FILE} and fill in secrets, or run \`lunora dev\` in an interactive terminal to scaffold automatically.`
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
const runDevCommand = async (options) => {
|
|
457
|
+
const plan = planDevCommand(options);
|
|
458
|
+
const { logger } = options;
|
|
459
|
+
const cwd = plan.wrangler.cwd ?? process.cwd();
|
|
460
|
+
const handles = { remoteCleanup: plan.remote.cleanup };
|
|
461
|
+
try {
|
|
462
|
+
await offerDevVariablesScaffold(options, cwd);
|
|
463
|
+
logger.info("starting wrangler dev + studio");
|
|
464
|
+
if (plan.codegenEnabled) {
|
|
465
|
+
handles.codegen = (options.startCodegen ?? startCodegenWatch)({ apiSpec: options.apiSpec, logger, projectRoot: cwd });
|
|
466
|
+
}
|
|
467
|
+
let studioUrl;
|
|
468
|
+
if (plan.studioEnabled) {
|
|
469
|
+
try {
|
|
470
|
+
handles.studio = await (options.startStudio ?? startStudioServer)({
|
|
471
|
+
cwd,
|
|
472
|
+
logger: {
|
|
473
|
+
warnOnce: (message) => {
|
|
474
|
+
logger.warn(message);
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
port: plan.studioPort,
|
|
478
|
+
workerOrigin: plan.workerOrigin
|
|
479
|
+
});
|
|
480
|
+
studioUrl = handles.studio.url;
|
|
481
|
+
} catch (error) {
|
|
482
|
+
logger.warn(`studio server failed to start (${error instanceof Error ? error.message : String(error)}) — continuing without it`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const worker = (options.startWorker ?? defaultWorkerSpawner)(plan.wrangler, logger);
|
|
486
|
+
printBanner(logger, plan, studioUrl);
|
|
487
|
+
printAgentRulesHint(logger, cwd);
|
|
488
|
+
let sigintCount = 0;
|
|
489
|
+
let escalationTimer;
|
|
490
|
+
const onSigint = () => {
|
|
491
|
+
sigintCount += 1;
|
|
492
|
+
if (sigintCount === 1) {
|
|
493
|
+
logger.info("received SIGINT — shutting down (press Ctrl-C again to force-kill)");
|
|
494
|
+
worker.kill("SIGTERM");
|
|
495
|
+
escalationTimer = setTimeout(() => {
|
|
496
|
+
worker.kill("SIGKILL");
|
|
497
|
+
}, SIGINT_GRACE_MS);
|
|
498
|
+
escalationTimer.unref();
|
|
499
|
+
} else {
|
|
500
|
+
worker.kill("SIGKILL");
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
const onSigterm = () => {
|
|
504
|
+
worker.kill("SIGTERM");
|
|
505
|
+
};
|
|
506
|
+
process.on("SIGINT", onSigint);
|
|
507
|
+
process.on("SIGTERM", onSigterm);
|
|
508
|
+
const code = await worker.exited;
|
|
509
|
+
if (escalationTimer) {
|
|
510
|
+
clearTimeout(escalationTimer);
|
|
511
|
+
}
|
|
512
|
+
process.off("SIGINT", onSigint);
|
|
513
|
+
process.off("SIGTERM", onSigterm);
|
|
514
|
+
return { code, plan };
|
|
515
|
+
} finally {
|
|
516
|
+
await teardown(handles);
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
const execute = defineHandler(
|
|
520
|
+
({ cwd, logger, options }) => runDevCommand({
|
|
521
|
+
apiSpec: parseApiSpec(options.apiSpec),
|
|
522
|
+
// cerebro parses `--no-codegen`/`--no-studio` as the negation of the
|
|
523
|
+
// `codegen`/`studio` booleans (runtime key drops the `no-` prefix), so a
|
|
524
|
+
// passed flag arrives as `false`, absent as `true` (the option default).
|
|
525
|
+
codegen: options.codegen === false ? false : void 0,
|
|
526
|
+
cwd,
|
|
527
|
+
logger,
|
|
528
|
+
port: options.port,
|
|
529
|
+
// Remote-binding mode obeys a clear precedence: an explicit `--remote`
|
|
530
|
+
// flag wins, then `LUNORA_REMOTE` in the environment, then the `remote`
|
|
531
|
+
// key in the project's `lunora.json` (a project default). See
|
|
532
|
+
// `resolveRemoteEnabled` in @lunora/config.
|
|
533
|
+
remote: resolveRemoteEnabled({
|
|
534
|
+
configPreference: readProjectRemotePreference(cwd),
|
|
535
|
+
envValue: process.env["LUNORA_REMOTE"],
|
|
536
|
+
flag: options.remote
|
|
537
|
+
}),
|
|
538
|
+
studio: options.studio === false ? false : void 0,
|
|
539
|
+
workerPort: options.workerPort
|
|
540
|
+
})
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
export { execute, planDevCommand, resolveRemotePlan, runDevCommand };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { runCodegen } from '@lunora/codegen';
|
|
2
|
+
import { p as parseApiSpec } from '../packem_shared/api-spec-CtA6ilu4.mjs';
|
|
3
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
4
|
+
import { v as validateOutputFormat, p as printJson, l as loggerForFormat, i as isJsonFormat } from '../packem_shared/output-format-7gyGR3h8.mjs';
|
|
5
|
+
|
|
6
|
+
const CRON_TRIGGER_LIMIT = 3;
|
|
7
|
+
const runCodegenCommand = (options) => {
|
|
8
|
+
const projectRoot = options.cwd ?? process.cwd();
|
|
9
|
+
const json = isJsonFormat(options.format);
|
|
10
|
+
const logger = loggerForFormat(options.format, options.logger);
|
|
11
|
+
const formatError = validateOutputFormat("codegen", options.format);
|
|
12
|
+
if (formatError !== void 0) {
|
|
13
|
+
options.logger.error(formatError);
|
|
14
|
+
return { advisories: [], cronTriggers: [], error: formatError, outputDirectory: "" };
|
|
15
|
+
}
|
|
16
|
+
const result = runCodegen({ apiSpec: options.apiSpec, projectRoot });
|
|
17
|
+
const commandResult = {
|
|
18
|
+
advisories: result.advisories.map((advisory) => {
|
|
19
|
+
return {
|
|
20
|
+
detail: advisory.detail,
|
|
21
|
+
level: advisory.level,
|
|
22
|
+
name: advisory.name,
|
|
23
|
+
remediation: advisory.remediation
|
|
24
|
+
};
|
|
25
|
+
}),
|
|
26
|
+
cronTriggers: result.cronTriggers,
|
|
27
|
+
outputDirectory: result.outputDirectory
|
|
28
|
+
};
|
|
29
|
+
logger.success(`codegen wrote dataModel.ts, api.ts, server.ts to ${result.outputDirectory}`);
|
|
30
|
+
if (result.advisories.length > 0) {
|
|
31
|
+
const count = result.advisories.length;
|
|
32
|
+
const lines = result.advisories.map((advisory) => ` [${advisory.level}] ${advisory.name} — ${advisory.detail}
|
|
33
|
+
↳ ${advisory.remediation}`);
|
|
34
|
+
logger.warn(`${count.toString()} schema ${count === 1 ? "advisory" : "advisories"}:
|
|
35
|
+
${lines.join("\n")}`);
|
|
36
|
+
}
|
|
37
|
+
if (result.cronTriggers.length > CRON_TRIGGER_LIMIT) {
|
|
38
|
+
logger.warn(
|
|
39
|
+
`${result.cronTriggers.length.toString()} distinct cron expressions declared — Cloudflare allows at most ${CRON_TRIGGER_LIMIT.toString()} Cron Triggers per Worker. Consolidate schedules (jobs can share one expression) or move finer-grained work to Durable Object alarms via @lunora/scheduler (runAfter/runAt).`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
if (json) {
|
|
43
|
+
printJson(commandResult);
|
|
44
|
+
}
|
|
45
|
+
return commandResult;
|
|
46
|
+
};
|
|
47
|
+
const execute = defineHandler(({ cwd, logger, options }) => {
|
|
48
|
+
const result = runCodegenCommand({ apiSpec: parseApiSpec(options.apiSpec), cwd, format: options.format, logger });
|
|
49
|
+
return { code: result.error === void 0 ? 0 : 1 };
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export { execute, runCodegenCommand };
|