@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,311 @@
|
|
|
1
|
+
import { writeFileSync, existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { DEV_VARS_FILE, DEV_VARS_KEY_PATTERN, DEV_VARS_EXAMPLE_FILE, parseDevVariableEntries, isPlaceholderValue } from '@lunora/config';
|
|
4
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
5
|
+
import { defaultSpawner } from '../packem_shared/defaultSpawner-DxI3mebw.mjs';
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
|
|
8
|
+
const execCode = (error) => {
|
|
9
|
+
if (!error) {
|
|
10
|
+
return 0;
|
|
11
|
+
}
|
|
12
|
+
return typeof error.code === "number" ? error.code : 1;
|
|
13
|
+
};
|
|
14
|
+
const defaultRunner = (command, args, cwd) => new Promise((resolve) => {
|
|
15
|
+
execFile(command, [...args], { cwd }, (error, stdout, stderr) => {
|
|
16
|
+
resolve({ code: execCode(error), stderr, stdout });
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
const parseSecretNames = (stdout) => {
|
|
20
|
+
let parsed;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(stdout);
|
|
23
|
+
} catch {
|
|
24
|
+
return void 0;
|
|
25
|
+
}
|
|
26
|
+
if (!Array.isArray(parsed)) {
|
|
27
|
+
return void 0;
|
|
28
|
+
}
|
|
29
|
+
const names = parsed.map((entry) => entry !== null && typeof entry === "object" ? entry.name : void 0).filter((name) => typeof name === "string" && name.length > 0);
|
|
30
|
+
return [...names].toSorted((a, b) => a.localeCompare(b));
|
|
31
|
+
};
|
|
32
|
+
const listRemoteSecrets = async (inputs) => {
|
|
33
|
+
const args = ["exec", "wrangler", "secret", "list", "--format", "json"];
|
|
34
|
+
if (inputs.env !== void 0) {
|
|
35
|
+
args.push("--env", inputs.env);
|
|
36
|
+
}
|
|
37
|
+
if (inputs.temporary) {
|
|
38
|
+
args.push("--temporary");
|
|
39
|
+
}
|
|
40
|
+
const runner = inputs.runner ?? defaultRunner;
|
|
41
|
+
const result = await runner("pnpm", args, inputs.cwd);
|
|
42
|
+
if (result.code !== 0) {
|
|
43
|
+
return { error: result.stderr.trim() || `wrangler secret list exited ${String(result.code)}`, names: [], ok: false };
|
|
44
|
+
}
|
|
45
|
+
const names = parseSecretNames(result.stdout);
|
|
46
|
+
if (names === void 0) {
|
|
47
|
+
return { error: "could not parse `wrangler secret list --format json` output", names: [], ok: false };
|
|
48
|
+
}
|
|
49
|
+
return { names, ok: true };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const NEWLINE_PRESENT = /[\r\n]/u;
|
|
53
|
+
const UNREPRESENTABLE_PRESENT = /["\\]/u;
|
|
54
|
+
const parseDevVariables = (content) => {
|
|
55
|
+
const map = /* @__PURE__ */ new Map();
|
|
56
|
+
for (const entry of parseDevVariableEntries(content)) {
|
|
57
|
+
map.set(entry.key, entry);
|
|
58
|
+
}
|
|
59
|
+
return map;
|
|
60
|
+
};
|
|
61
|
+
const serializeDevVariables = (map) => {
|
|
62
|
+
const lines = [];
|
|
63
|
+
for (const entry of map.values()) {
|
|
64
|
+
lines.push(`${entry.key}="${entry.value}"`);
|
|
65
|
+
}
|
|
66
|
+
return `${lines.join("\n")}
|
|
67
|
+
`;
|
|
68
|
+
};
|
|
69
|
+
const redact = (value) => {
|
|
70
|
+
if (value.length <= 4) {
|
|
71
|
+
return "****";
|
|
72
|
+
}
|
|
73
|
+
return `${value.slice(0, 4)}${"*".repeat(Math.min(8, value.length - 4))}`;
|
|
74
|
+
};
|
|
75
|
+
const loadDevVariables = (devVariablesPath) => {
|
|
76
|
+
if (!existsSync(devVariablesPath)) {
|
|
77
|
+
return /* @__PURE__ */ new Map();
|
|
78
|
+
}
|
|
79
|
+
return parseDevVariables(readFileSync(devVariablesPath, "utf8"));
|
|
80
|
+
};
|
|
81
|
+
const runEnvList = (context) => {
|
|
82
|
+
const map = loadDevVariables(context.devVariablesPath);
|
|
83
|
+
if (map.size === 0) {
|
|
84
|
+
context.logger.info(`${DEV_VARS_FILE}: (empty)`);
|
|
85
|
+
return { code: 0, descriptors: [] };
|
|
86
|
+
}
|
|
87
|
+
for (const entry of map.values()) {
|
|
88
|
+
context.logger.info(`${entry.key}=${redact(entry.value)}`);
|
|
89
|
+
}
|
|
90
|
+
return { code: 0, descriptors: [] };
|
|
91
|
+
};
|
|
92
|
+
const runEnvGet = (context) => {
|
|
93
|
+
const { devVariablesPath, logger, options } = context;
|
|
94
|
+
if (!options.key) {
|
|
95
|
+
logger.error("env get requires a key. Usage: lunora env get <KEY>");
|
|
96
|
+
return { code: 1, descriptors: [] };
|
|
97
|
+
}
|
|
98
|
+
const entry = loadDevVariables(devVariablesPath).get(options.key);
|
|
99
|
+
if (!entry) {
|
|
100
|
+
logger.error(`env: ${options.key} is not set in ${DEV_VARS_FILE}`);
|
|
101
|
+
return { code: 1, descriptors: [] };
|
|
102
|
+
}
|
|
103
|
+
process.stdout.write(`${entry.value}
|
|
104
|
+
`);
|
|
105
|
+
return { code: 0, descriptors: [] };
|
|
106
|
+
};
|
|
107
|
+
const runEnvSet = (context) => {
|
|
108
|
+
const { devVariablesPath, logger, options } = context;
|
|
109
|
+
if (!options.key) {
|
|
110
|
+
logger.error("env set requires a key. Usage: lunora env set <KEY> <VALUE>");
|
|
111
|
+
return { code: 1, descriptors: [] };
|
|
112
|
+
}
|
|
113
|
+
if (!DEV_VARS_KEY_PATTERN.test(options.key)) {
|
|
114
|
+
logger.error(`env: invalid key "${options.key}" — must match [A-Za-z_][A-Za-z0-9_]*`);
|
|
115
|
+
return { code: 1, descriptors: [] };
|
|
116
|
+
}
|
|
117
|
+
if (options.value === void 0) {
|
|
118
|
+
logger.error("env set requires a value. Usage: lunora env set <KEY> <VALUE>");
|
|
119
|
+
return { code: 1, descriptors: [] };
|
|
120
|
+
}
|
|
121
|
+
if (NEWLINE_PRESENT.test(options.value)) {
|
|
122
|
+
logger.error(`env: value for "${options.key}" contains a newline, which .dev.vars cannot represent`);
|
|
123
|
+
return { code: 1, descriptors: [] };
|
|
124
|
+
}
|
|
125
|
+
if (UNREPRESENTABLE_PRESENT.test(options.value)) {
|
|
126
|
+
logger.error(`env: value for "${options.key}" contains a double-quote or backslash, which .dev.vars cannot round-trip`);
|
|
127
|
+
return { code: 1, descriptors: [] };
|
|
128
|
+
}
|
|
129
|
+
const map = loadDevVariables(devVariablesPath);
|
|
130
|
+
map.set(options.key, { key: options.key, value: options.value });
|
|
131
|
+
writeFileSync(devVariablesPath, serializeDevVariables(map), "utf8");
|
|
132
|
+
logger.success(`env: set ${options.key} (${redact(options.value)}) in ${DEV_VARS_FILE}`);
|
|
133
|
+
return { code: 0, descriptors: [] };
|
|
134
|
+
};
|
|
135
|
+
const runEnvUnset = (context) => {
|
|
136
|
+
const { devVariablesPath, logger, options } = context;
|
|
137
|
+
if (!options.key) {
|
|
138
|
+
logger.error("env unset requires a key. Usage: lunora env unset <KEY>");
|
|
139
|
+
return { code: 1, descriptors: [] };
|
|
140
|
+
}
|
|
141
|
+
const map = loadDevVariables(devVariablesPath);
|
|
142
|
+
if (!map.delete(options.key)) {
|
|
143
|
+
logger.warn(`env: ${options.key} was not set in ${DEV_VARS_FILE}`);
|
|
144
|
+
return { code: 0, descriptors: [] };
|
|
145
|
+
}
|
|
146
|
+
writeFileSync(devVariablesPath, serializeDevVariables(map), "utf8");
|
|
147
|
+
logger.success(`env: unset ${options.key} in ${DEV_VARS_FILE}`);
|
|
148
|
+
return { code: 0, descriptors: [] };
|
|
149
|
+
};
|
|
150
|
+
const runEnvPush = async (context) => {
|
|
151
|
+
const { devVariablesPath, logger, options } = context;
|
|
152
|
+
if (!options.yes) {
|
|
153
|
+
logger.error("env push uploads secrets to Cloudflare. Re-run with --yes to confirm.");
|
|
154
|
+
return { code: 1, descriptors: [] };
|
|
155
|
+
}
|
|
156
|
+
const map = loadDevVariables(devVariablesPath);
|
|
157
|
+
if (map.size === 0) {
|
|
158
|
+
logger.warn(`${DEV_VARS_FILE}: nothing to push (empty)`);
|
|
159
|
+
return { code: 0, descriptors: [] };
|
|
160
|
+
}
|
|
161
|
+
const spawner = options.spawner ?? defaultSpawner;
|
|
162
|
+
const descriptors = [];
|
|
163
|
+
for (const entry of map.values()) {
|
|
164
|
+
const args = ["exec", "wrangler", "secret", "put", entry.key];
|
|
165
|
+
if (options.prod) {
|
|
166
|
+
args.push("--env", "production");
|
|
167
|
+
}
|
|
168
|
+
if (options.temporary) {
|
|
169
|
+
args.push("--temporary");
|
|
170
|
+
}
|
|
171
|
+
const descriptor = {
|
|
172
|
+
args,
|
|
173
|
+
command: "pnpm",
|
|
174
|
+
cwd: options.cwd ?? process.cwd(),
|
|
175
|
+
// `wrangler secret put <name>` reads the value from stdin. We
|
|
176
|
+
// pipe it through the spawner's `input` channel so the secret
|
|
177
|
+
// never lands on the command line, in env, or in shell history.
|
|
178
|
+
input: entry.value
|
|
179
|
+
};
|
|
180
|
+
descriptors.push(descriptor);
|
|
181
|
+
logger.info(`pushing ${entry.key} -> wrangler secret${options.prod ? " (production)" : ""}`);
|
|
182
|
+
const result = await spawner(descriptor);
|
|
183
|
+
if (result.code !== 0) {
|
|
184
|
+
logger.error(`env push: failed at ${entry.key} (exit ${String(result.code)})`);
|
|
185
|
+
return { code: result.code, descriptors };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
logger.success(`env: pushed ${String(map.size)} secret(s)`);
|
|
189
|
+
return { code: 0, descriptors };
|
|
190
|
+
};
|
|
191
|
+
const fetchRemoteSecretNames = (context) => {
|
|
192
|
+
const lister = context.options.secretLister ?? listRemoteSecrets;
|
|
193
|
+
return lister({
|
|
194
|
+
cwd: context.cwd,
|
|
195
|
+
env: context.options.prod ? "production" : void 0,
|
|
196
|
+
temporary: context.options.temporary
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
const runEnvDiff = async (context) => {
|
|
200
|
+
const { devVariablesPath, logger } = context;
|
|
201
|
+
const remote = await fetchRemoteSecretNames(context);
|
|
202
|
+
if (!remote.ok) {
|
|
203
|
+
logger.error(`env diff: ${remote.error ?? "failed to list remote secrets"}`);
|
|
204
|
+
return { code: 1, descriptors: [] };
|
|
205
|
+
}
|
|
206
|
+
const localKeys = new Set(loadDevVariables(devVariablesPath).keys());
|
|
207
|
+
const remoteKeys = new Set(remote.names);
|
|
208
|
+
const localOnly = [...localKeys].filter((key) => !remoteKeys.has(key)).toSorted((a, b) => a.localeCompare(b));
|
|
209
|
+
const remoteOnly = remote.names.filter((name) => !localKeys.has(name));
|
|
210
|
+
const both = [...localKeys].filter((key) => remoteKeys.has(key)).toSorted((a, b) => a.localeCompare(b));
|
|
211
|
+
for (const key of localOnly) {
|
|
212
|
+
logger.info(`local only (run \`lunora env push\`): ${key}`);
|
|
213
|
+
}
|
|
214
|
+
for (const key of remoteOnly) {
|
|
215
|
+
logger.info(`remote only (in Cloudflare, not ${DEV_VARS_FILE}): ${key}`);
|
|
216
|
+
}
|
|
217
|
+
logger.info(`in both: ${String(both.length)} secret(s)`);
|
|
218
|
+
if (localOnly.length === 0 && remoteOnly.length === 0) {
|
|
219
|
+
logger.success("env diff: local and remote secret names match");
|
|
220
|
+
}
|
|
221
|
+
return { code: 0, descriptors: [] };
|
|
222
|
+
};
|
|
223
|
+
const runEnvDoctor = (context) => {
|
|
224
|
+
const { cwd, devVariablesPath, logger } = context;
|
|
225
|
+
const examplePath = join(cwd, DEV_VARS_EXAMPLE_FILE);
|
|
226
|
+
if (!existsSync(examplePath)) {
|
|
227
|
+
logger.info(`env doctor: no ${DEV_VARS_EXAMPLE_FILE} to check against — nothing to validate.`);
|
|
228
|
+
return { code: 0, descriptors: [] };
|
|
229
|
+
}
|
|
230
|
+
const exampleKeys = parseDevVariableEntries(readFileSync(examplePath, "utf8")).map((entry) => entry.key);
|
|
231
|
+
const current = loadDevVariables(devVariablesPath);
|
|
232
|
+
if (!existsSync(devVariablesPath)) {
|
|
233
|
+
logger.error(`env doctor: ${DEV_VARS_FILE} is missing. Run \`lunora dev\` to scaffold it, or \`lunora env set <KEY> <VALUE>\`.`);
|
|
234
|
+
logger.info(`expected (from ${DEV_VARS_EXAMPLE_FILE}): ${exampleKeys.join(", ")}`);
|
|
235
|
+
return { code: 1, descriptors: [] };
|
|
236
|
+
}
|
|
237
|
+
const missing = exampleKeys.filter((key) => !current.has(key));
|
|
238
|
+
const placeholders = [...current.values()].filter((entry) => isPlaceholderValue(entry.value)).map((entry) => entry.key);
|
|
239
|
+
const exampleKeySet = new Set(exampleKeys);
|
|
240
|
+
const extra = [...current.keys()].filter((key) => !exampleKeySet.has(key));
|
|
241
|
+
for (const key of missing) {
|
|
242
|
+
logger.error(`missing: ${key} is in ${DEV_VARS_EXAMPLE_FILE} but not ${DEV_VARS_FILE}`);
|
|
243
|
+
}
|
|
244
|
+
for (const key of placeholders) {
|
|
245
|
+
logger.error(`unset: ${key} still has a placeholder value`);
|
|
246
|
+
}
|
|
247
|
+
for (const key of extra) {
|
|
248
|
+
logger.info(`extra: ${key} is set locally but not listed in ${DEV_VARS_EXAMPLE_FILE}`);
|
|
249
|
+
}
|
|
250
|
+
if (missing.length === 0 && placeholders.length === 0) {
|
|
251
|
+
logger.success(`env doctor: ${DEV_VARS_FILE} looks good (${String(current.size)} var(s)).`);
|
|
252
|
+
return { code: 0, descriptors: [] };
|
|
253
|
+
}
|
|
254
|
+
return { code: 1, descriptors: [] };
|
|
255
|
+
};
|
|
256
|
+
const runEnvCommand = async (options) => {
|
|
257
|
+
const cwd = options.cwd ?? process.cwd();
|
|
258
|
+
const context = {
|
|
259
|
+
cwd,
|
|
260
|
+
devVariablesPath: join(cwd, DEV_VARS_FILE),
|
|
261
|
+
logger: options.logger,
|
|
262
|
+
options
|
|
263
|
+
};
|
|
264
|
+
switch (options.subcommand) {
|
|
265
|
+
case "diff": {
|
|
266
|
+
return runEnvDiff(context);
|
|
267
|
+
}
|
|
268
|
+
case "doctor": {
|
|
269
|
+
return runEnvDoctor(context);
|
|
270
|
+
}
|
|
271
|
+
case "get": {
|
|
272
|
+
return runEnvGet(context);
|
|
273
|
+
}
|
|
274
|
+
case "list": {
|
|
275
|
+
return runEnvList(context);
|
|
276
|
+
}
|
|
277
|
+
case "push": {
|
|
278
|
+
return runEnvPush(context);
|
|
279
|
+
}
|
|
280
|
+
case "set": {
|
|
281
|
+
return runEnvSet(context);
|
|
282
|
+
}
|
|
283
|
+
case "unset": {
|
|
284
|
+
return runEnvUnset(context);
|
|
285
|
+
}
|
|
286
|
+
default: {
|
|
287
|
+
options.logger.error(`env: unknown subcommand "${options.subcommand}"`);
|
|
288
|
+
return { code: 1, descriptors: [] };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
const isEnvSubcommand = (value) => value === "list" || value === "get" || value === "set" || value === "unset" || value === "push" || value === "diff" || value === "doctor";
|
|
293
|
+
const execute = defineHandler(({ argument, cwd, logger, options }) => {
|
|
294
|
+
const sub = argument[0];
|
|
295
|
+
if (!isEnvSubcommand(sub)) {
|
|
296
|
+
logger.error(`env: unknown subcommand "${sub ?? ""}" — expected list | get | set | unset | push | diff | doctor`);
|
|
297
|
+
return { code: 1 };
|
|
298
|
+
}
|
|
299
|
+
return runEnvCommand({
|
|
300
|
+
cwd,
|
|
301
|
+
key: argument[1],
|
|
302
|
+
logger,
|
|
303
|
+
prod: options.prod === true,
|
|
304
|
+
subcommand: sub,
|
|
305
|
+
temporary: options.temporary === true,
|
|
306
|
+
value: argument[2],
|
|
307
|
+
yes: options.yes === true
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
export { execute, runEnvCommand };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { r as resolveAdminBaseUrl } from '../packem_shared/admin-url-4UzT-CI4.mjs';
|
|
5
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
6
|
+
import { a as resolveProductionWorkerUrl } from '../packem_shared/resolve-target-qbsJ_5sF.mjs';
|
|
7
|
+
import { runExportCommand, runImportCommand } from '../packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs';
|
|
8
|
+
|
|
9
|
+
const DEFAULT_BACKUP_DIR = ".lunora-backups";
|
|
10
|
+
const MANIFEST_FILE = "manifest.json";
|
|
11
|
+
const PITR_ENDPOINT_PATH = "/_lunora/admin/pitr";
|
|
12
|
+
const GET_PITR_BOOKMARK_OP = "__lunora_admin__:getPitrBookmark";
|
|
13
|
+
const PITR_RESTORE_OP = "__lunora_admin__:pitrRestore";
|
|
14
|
+
const isManifestEntry = (value) => typeof value === "object" && value !== null && typeof value.id === "string" && typeof value.file === "string";
|
|
15
|
+
const readManifest = async (directory) => {
|
|
16
|
+
const path = join(directory, MANIFEST_FILE);
|
|
17
|
+
if (!existsSync(path)) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
let parsed;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(await readFile(path, "utf8"));
|
|
23
|
+
} catch (error) {
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
+
throw new Error(`backup: ${path} exists but is not valid JSON (${message}) — refusing to overwrite it; fix or remove it manually`, { cause: error });
|
|
26
|
+
}
|
|
27
|
+
if (!Array.isArray(parsed)) {
|
|
28
|
+
throw new TypeError(`backup: ${path} exists but is not a JSON array — refusing to overwrite it; fix or remove it manually`);
|
|
29
|
+
}
|
|
30
|
+
return parsed.filter(isManifestEntry);
|
|
31
|
+
};
|
|
32
|
+
const writeManifest = async (directory, entries) => {
|
|
33
|
+
await writeFile(join(directory, MANIFEST_FILE), `${JSON.stringify(entries, void 0, 2)}
|
|
34
|
+
`, "utf8");
|
|
35
|
+
};
|
|
36
|
+
const runBackupCreate = async (options, directory) => {
|
|
37
|
+
await mkdir(directory, { recursive: true });
|
|
38
|
+
const timestamp = (options.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
|
|
39
|
+
const file = `lunora-backup-${timestamp.replaceAll(/[.:]/gu, "-")}.ndjson`;
|
|
40
|
+
const result = await runExportCommand({
|
|
41
|
+
fetchImpl: options.fetchImpl,
|
|
42
|
+
logger: options.logger,
|
|
43
|
+
out: join(directory, file),
|
|
44
|
+
prod: options.prod,
|
|
45
|
+
tables: options.tables,
|
|
46
|
+
token: options.token,
|
|
47
|
+
url: options.url
|
|
48
|
+
});
|
|
49
|
+
if (result.code !== 0) {
|
|
50
|
+
return { code: result.code };
|
|
51
|
+
}
|
|
52
|
+
const entry = { bytes: result.bytes, createdAt: timestamp, file, id: timestamp, rows: result.rows, tables: options.tables };
|
|
53
|
+
const manifest = await readManifest(directory);
|
|
54
|
+
manifest.push(entry);
|
|
55
|
+
await writeManifest(directory, manifest);
|
|
56
|
+
options.logger.success(`backup created: ${file} (${result.rows.toString()} rows, ${result.bytes.toString()} bytes)`);
|
|
57
|
+
return { code: 0, entry };
|
|
58
|
+
};
|
|
59
|
+
const runBackupList = async (options, directory) => {
|
|
60
|
+
const manifest = await readManifest(directory);
|
|
61
|
+
if (manifest.length === 0) {
|
|
62
|
+
options.logger.info(`no backups found in ${directory}`);
|
|
63
|
+
return { code: 0 };
|
|
64
|
+
}
|
|
65
|
+
for (const entry of manifest) {
|
|
66
|
+
options.logger.info(`${entry.id} ${entry.rows.toString()} rows ${entry.bytes.toString()} bytes ${entry.file}`);
|
|
67
|
+
}
|
|
68
|
+
return { code: 0 };
|
|
69
|
+
};
|
|
70
|
+
const runBackupRestore = async (options, directory) => {
|
|
71
|
+
const { target } = options;
|
|
72
|
+
if (target === void 0 || target.length === 0) {
|
|
73
|
+
options.logger.error("restore requires a backup id or file path. Usage: lunora backup restore <id|file>");
|
|
74
|
+
return { code: 1 };
|
|
75
|
+
}
|
|
76
|
+
const manifest = await readManifest(directory);
|
|
77
|
+
const matched = manifest.find((entry) => entry.id === target);
|
|
78
|
+
const file = matched ? join(directory, matched.file) : target;
|
|
79
|
+
if (!existsSync(file)) {
|
|
80
|
+
options.logger.error(`backup not found: ${target}`);
|
|
81
|
+
return { code: 1 };
|
|
82
|
+
}
|
|
83
|
+
const result = await runImportCommand({
|
|
84
|
+
fetchImpl: options.fetchImpl,
|
|
85
|
+
file,
|
|
86
|
+
logger: options.logger,
|
|
87
|
+
prod: options.prod,
|
|
88
|
+
token: options.token,
|
|
89
|
+
url: options.url
|
|
90
|
+
});
|
|
91
|
+
return { code: result.code };
|
|
92
|
+
};
|
|
93
|
+
const resolvePitrRequest = (options) => {
|
|
94
|
+
const token = options.token ?? process.env.LUNORA_ADMIN_TOKEN;
|
|
95
|
+
if (!token) {
|
|
96
|
+
options.logger.error("admin token required — pass --token or set LUNORA_ADMIN_TOKEN");
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
99
|
+
if (options.prod && options.url === void 0) {
|
|
100
|
+
options.logger.error("--prod requires an explicit --url (refusing to target the implicit localhost worker)");
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
if (options.restore === true && options.at === void 0 && options.bookmark === void 0) {
|
|
104
|
+
options.logger.error("pitr --restore requires --at <time> or --bookmark <bookmark>");
|
|
105
|
+
return void 0;
|
|
106
|
+
}
|
|
107
|
+
if (options.restore === true && options.prod === true && options.yes !== true) {
|
|
108
|
+
options.logger.error("pitr --restore --prod restores production data in place. Re-run with --yes to confirm.");
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
const baseUrl = resolveAdminBaseUrl(options.url, options.logger);
|
|
112
|
+
if (baseUrl === void 0) {
|
|
113
|
+
return void 0;
|
|
114
|
+
}
|
|
115
|
+
const fetchImpl = options.pitrFetch ?? globalThis.fetch;
|
|
116
|
+
if (typeof fetchImpl !== "function") {
|
|
117
|
+
throw new TypeError("no fetch implementation available — pass pitrFetch or run on Node >= 18");
|
|
118
|
+
}
|
|
119
|
+
return { fetchImpl, requestUrl: `${baseUrl}${PITR_ENDPOINT_PATH}`, token };
|
|
120
|
+
};
|
|
121
|
+
const buildPitrArgs = (options, isRestore) => {
|
|
122
|
+
const args = {};
|
|
123
|
+
if (options.at !== void 0) {
|
|
124
|
+
args.time = options.at;
|
|
125
|
+
}
|
|
126
|
+
if (isRestore && options.bookmark !== void 0) {
|
|
127
|
+
args.bookmark = options.bookmark;
|
|
128
|
+
}
|
|
129
|
+
if (isRestore && options.restart === true) {
|
|
130
|
+
args.restart = true;
|
|
131
|
+
}
|
|
132
|
+
return args;
|
|
133
|
+
};
|
|
134
|
+
const runBackupPitr = async (options) => {
|
|
135
|
+
const request = resolvePitrRequest(options);
|
|
136
|
+
if (request === void 0) {
|
|
137
|
+
return { code: 1 };
|
|
138
|
+
}
|
|
139
|
+
const isRestore = options.restore === true;
|
|
140
|
+
const functionPath = isRestore ? PITR_RESTORE_OP : GET_PITR_BOOKMARK_OP;
|
|
141
|
+
const args = buildPitrArgs(options, isRestore);
|
|
142
|
+
const action = isRestore ? "restore" : "bookmark";
|
|
143
|
+
options.logger.info(`POST ${request.requestUrl} -> pitr ${action}${options.shard === void 0 ? "" : ` (shard "${options.shard}")`}`);
|
|
144
|
+
const response = await request.fetchImpl(request.requestUrl, {
|
|
145
|
+
body: JSON.stringify({ args, functionPath, shardKey: options.shard }),
|
|
146
|
+
headers: { authorization: `Bearer ${request.token}`, "content-type": "application/json" },
|
|
147
|
+
method: "POST"
|
|
148
|
+
});
|
|
149
|
+
const text = await response.text();
|
|
150
|
+
let body;
|
|
151
|
+
try {
|
|
152
|
+
body = JSON.parse(text);
|
|
153
|
+
} catch {
|
|
154
|
+
body = text;
|
|
155
|
+
}
|
|
156
|
+
options.logger.info(JSON.stringify(body, void 0, 2));
|
|
157
|
+
return { code: response.ok ? 0 : 1 };
|
|
158
|
+
};
|
|
159
|
+
const runBackupCommand = async (options) => {
|
|
160
|
+
const cwd = options.cwd ?? process.cwd();
|
|
161
|
+
const directory = join(cwd, options.dir ?? DEFAULT_BACKUP_DIR);
|
|
162
|
+
try {
|
|
163
|
+
if (options.subcommand === "create") {
|
|
164
|
+
return await runBackupCreate(options, directory);
|
|
165
|
+
}
|
|
166
|
+
if (options.subcommand === "list") {
|
|
167
|
+
return await runBackupList(options, directory);
|
|
168
|
+
}
|
|
169
|
+
if (options.subcommand === "pitr") {
|
|
170
|
+
return await runBackupPitr(options);
|
|
171
|
+
}
|
|
172
|
+
return await runBackupRestore(options, directory);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
options.logger.error(error instanceof Error ? error.message : String(error));
|
|
175
|
+
return { code: 1 };
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const isBackupSubcommand = (value) => value === "create" || value === "list" || value === "pitr" || value === "restore";
|
|
179
|
+
const execute = defineHandler(({ argument, cwd, logger, options }) => {
|
|
180
|
+
const sub = argument[0];
|
|
181
|
+
if (!isBackupSubcommand(sub)) {
|
|
182
|
+
logger.error(`backup: unknown subcommand "${sub ?? ""}" — expected create | list | restore | pitr`);
|
|
183
|
+
return { code: 1 };
|
|
184
|
+
}
|
|
185
|
+
return runBackupCommand({
|
|
186
|
+
at: options.at,
|
|
187
|
+
bookmark: options.bookmark,
|
|
188
|
+
cwd,
|
|
189
|
+
dir: options.dir,
|
|
190
|
+
logger,
|
|
191
|
+
prod: options.prod === true,
|
|
192
|
+
restart: options.restart === true,
|
|
193
|
+
restore: options.restore === true,
|
|
194
|
+
shard: options.shard,
|
|
195
|
+
subcommand: sub,
|
|
196
|
+
tables: options.tables,
|
|
197
|
+
target: argument[1],
|
|
198
|
+
token: options.token,
|
|
199
|
+
url: resolveProductionWorkerUrl({ cwd, prod: options.prod === true, url: options.url }),
|
|
200
|
+
yes: options.yes === true
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
export { execute, runBackupCommand };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { p as parseApiSpec } from '../packem_shared/api-spec-CtA6ilu4.mjs';
|
|
2
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
3
|
+
import { runDeployCommand } from './runDeployCommand.mjs';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_OUT_DIR = ".lunora/build";
|
|
6
|
+
const runBuildCommand = async (options) => {
|
|
7
|
+
const outDirectory = options.outDir ?? DEFAULT_OUT_DIR;
|
|
8
|
+
const result = await runDeployCommand({
|
|
9
|
+
apiSpec: options.apiSpec,
|
|
10
|
+
cwd: options.cwd,
|
|
11
|
+
dryRun: true,
|
|
12
|
+
format: options.format,
|
|
13
|
+
logger: options.logger,
|
|
14
|
+
outDir: outDirectory,
|
|
15
|
+
spawner: options.spawner
|
|
16
|
+
});
|
|
17
|
+
if (result.code === 0) {
|
|
18
|
+
options.logger.success(`build complete — bundle written to ${outDirectory}`);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
const execute = defineHandler(async ({ cwd, logger, options }) => {
|
|
23
|
+
const result = await runBuildCommand({
|
|
24
|
+
apiSpec: parseApiSpec(options.apiSpec),
|
|
25
|
+
cwd,
|
|
26
|
+
format: options.format,
|
|
27
|
+
logger,
|
|
28
|
+
outDir: options.outDir
|
|
29
|
+
});
|
|
30
|
+
return { code: result.code };
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export { execute, runBuildCommand };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
2
|
+
import { i as isDockerAvailable } from '../packem_shared/docker-hMQ97KSQ.mjs';
|
|
3
|
+
import { defaultSpawner } from '../packem_shared/defaultSpawner-DxI3mebw.mjs';
|
|
4
|
+
|
|
5
|
+
const SUBCOMMANDS = /* @__PURE__ */ new Set(["build", "delete", "images", "info", "list", "push"]);
|
|
6
|
+
const NEEDS_DOCKER = /* @__PURE__ */ new Set(["build", "push"]);
|
|
7
|
+
const runContainersCommand = async (options) => {
|
|
8
|
+
const [subcommand, ...rest] = options.argument;
|
|
9
|
+
if (subcommand === void 0 || !SUBCOMMANDS.has(subcommand)) {
|
|
10
|
+
options.logger.error(
|
|
11
|
+
`lunora containers requires a subcommand: ${[...SUBCOMMANDS].toSorted((a, b) => a.localeCompare(b)).join(" | ")}. Example: lunora containers build ./containers/app --tag app:v1 --push`
|
|
12
|
+
);
|
|
13
|
+
return { code: 1 };
|
|
14
|
+
}
|
|
15
|
+
if (NEEDS_DOCKER.has(subcommand) && !(options.dockerAvailable ?? isDockerAvailable)()) {
|
|
16
|
+
options.logger.error(
|
|
17
|
+
`containers ${subcommand} needs a running Docker-compatible engine (it builds/pushes images locally). Start Docker or Colima and retry. Note: container images must target linux/amd64.`
|
|
18
|
+
);
|
|
19
|
+
return { code: 1 };
|
|
20
|
+
}
|
|
21
|
+
const args = ["exec", "wrangler", "containers", subcommand, ...rest];
|
|
22
|
+
if (options.tag !== void 0) {
|
|
23
|
+
args.push("--tag", options.tag);
|
|
24
|
+
}
|
|
25
|
+
if (options.push === true) {
|
|
26
|
+
args.push("--push");
|
|
27
|
+
}
|
|
28
|
+
if (options.env !== void 0) {
|
|
29
|
+
args.push("--env", options.env);
|
|
30
|
+
}
|
|
31
|
+
const descriptor = { args, command: "pnpm", cwd: options.cwd ?? process.cwd() };
|
|
32
|
+
options.logger.info(`running ${descriptor.command} ${descriptor.args.join(" ")}`);
|
|
33
|
+
const spawner = options.spawner ?? defaultSpawner;
|
|
34
|
+
const result = await spawner(descriptor);
|
|
35
|
+
return { code: result.code, descriptor };
|
|
36
|
+
};
|
|
37
|
+
const execute = defineHandler(async ({ argument, cwd, logger, options }) => {
|
|
38
|
+
const result = await runContainersCommand({
|
|
39
|
+
argument,
|
|
40
|
+
cwd,
|
|
41
|
+
env: options.env,
|
|
42
|
+
logger,
|
|
43
|
+
push: options.push === true,
|
|
44
|
+
tag: options.tag
|
|
45
|
+
});
|
|
46
|
+
return { code: result.code };
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export { execute, runContainersCommand };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { d as defineHandler } from '../packem_shared/command-BDXcJCCJ.mjs';
|
|
2
|
+
import { defaultSpawner } from '../packem_shared/defaultSpawner-DxI3mebw.mjs';
|
|
3
|
+
|
|
4
|
+
const withEnv = (args, env) => {
|
|
5
|
+
if (env !== void 0) {
|
|
6
|
+
args.push("--env", env);
|
|
7
|
+
}
|
|
8
|
+
return args;
|
|
9
|
+
};
|
|
10
|
+
const buildListArgs = (options) => {
|
|
11
|
+
const args = withEnv(["exec", "wrangler", "deployments", "list"], options.env);
|
|
12
|
+
if (options.json) {
|
|
13
|
+
args.push("--json");
|
|
14
|
+
}
|
|
15
|
+
return args;
|
|
16
|
+
};
|
|
17
|
+
const buildArgs = (options) => {
|
|
18
|
+
switch (options.subcommand) {
|
|
19
|
+
case "inspect": {
|
|
20
|
+
if (options.versionId === void 0) {
|
|
21
|
+
return { error: "deployments inspect requires a version id. Usage: lunora deployments inspect <version-id>" };
|
|
22
|
+
}
|
|
23
|
+
return { args: withEnv(["exec", "wrangler", "versions", "view", options.versionId], options.env) };
|
|
24
|
+
}
|
|
25
|
+
case "list": {
|
|
26
|
+
return { args: buildListArgs(options) };
|
|
27
|
+
}
|
|
28
|
+
case "promote": {
|
|
29
|
+
if (options.versionId === void 0) {
|
|
30
|
+
return { error: "deployments promote requires a version id. Usage: lunora deployments promote <version-id> --yes" };
|
|
31
|
+
}
|
|
32
|
+
if (!options.yes) {
|
|
33
|
+
return { error: "deployments promote shifts 100% of live traffic. Re-run with --yes to confirm." };
|
|
34
|
+
}
|
|
35
|
+
const args = withEnv(["exec", "wrangler", "versions", "deploy", `${options.versionId}@100%`, "--yes"], options.env);
|
|
36
|
+
if (options.message !== void 0) {
|
|
37
|
+
args.push("--message", options.message);
|
|
38
|
+
}
|
|
39
|
+
return { args };
|
|
40
|
+
}
|
|
41
|
+
case "rollback": {
|
|
42
|
+
if (!options.yes) {
|
|
43
|
+
return { error: "deployments rollback changes the live version. Re-run with --yes to confirm." };
|
|
44
|
+
}
|
|
45
|
+
const args = withEnv(["exec", "wrangler", "rollback"], options.env);
|
|
46
|
+
if (options.versionId !== void 0) {
|
|
47
|
+
args.push(options.versionId);
|
|
48
|
+
}
|
|
49
|
+
args.push("--yes");
|
|
50
|
+
if (options.message !== void 0) {
|
|
51
|
+
args.push("--message", options.message);
|
|
52
|
+
}
|
|
53
|
+
return { args };
|
|
54
|
+
}
|
|
55
|
+
default: {
|
|
56
|
+
return { error: `deployments: unknown subcommand "${options.subcommand}"` };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const runDeploymentsCommand = async (options) => {
|
|
61
|
+
const { args, error } = buildArgs(options);
|
|
62
|
+
if (error !== void 0 || args === void 0) {
|
|
63
|
+
options.logger.error(error ?? "deployments: nothing to run");
|
|
64
|
+
return { code: 1, descriptor: void 0, error };
|
|
65
|
+
}
|
|
66
|
+
const descriptor = { args, command: "pnpm", cwd: options.cwd ?? process.cwd() };
|
|
67
|
+
options.logger.info(`${descriptor.command} ${descriptor.args.join(" ")}`);
|
|
68
|
+
const spawner = options.spawner ?? defaultSpawner;
|
|
69
|
+
const result = await spawner(descriptor);
|
|
70
|
+
return { code: result.code, descriptor };
|
|
71
|
+
};
|
|
72
|
+
const isDeploymentsSubcommand = (value) => value === "list" || value === "inspect" || value === "rollback" || value === "promote";
|
|
73
|
+
const execute = defineHandler(({ argument, cwd, logger, options }) => {
|
|
74
|
+
const sub = argument[0];
|
|
75
|
+
if (!isDeploymentsSubcommand(sub)) {
|
|
76
|
+
logger.error(`deployments: unknown subcommand "${sub ?? ""}" — expected list | inspect | rollback | promote`);
|
|
77
|
+
return { code: 1 };
|
|
78
|
+
}
|
|
79
|
+
return runDeploymentsCommand({
|
|
80
|
+
cwd,
|
|
81
|
+
env: options.env,
|
|
82
|
+
json: options.json === true,
|
|
83
|
+
logger,
|
|
84
|
+
message: options.message,
|
|
85
|
+
subcommand: sub,
|
|
86
|
+
versionId: argument[1],
|
|
87
|
+
yes: options.yes === true
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
export { execute, runDeploymentsCommand };
|