mpb-localkit 1.3.7 → 1.4.1
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/dist/cli/index.js +303 -92
- package/dist/cli/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from 'commander';
|
|
3
3
|
import { createRequire } from 'module';
|
|
4
|
-
import { existsSync,
|
|
5
|
-
import { resolve, join } from 'path';
|
|
6
|
-
import {
|
|
4
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
|
|
5
|
+
import { resolve, join, dirname } from 'path';
|
|
6
|
+
import { execSync, spawnSync } from 'child_process';
|
|
7
|
+
import { randomBytes } from 'crypto';
|
|
7
8
|
|
|
8
9
|
var colors = {
|
|
9
10
|
green: (s) => `\x1B[32m${s}\x1B[0m`,
|
|
@@ -21,23 +22,6 @@ var log = {
|
|
|
21
22
|
bold: (msg) => console.log(colors.bold(msg)),
|
|
22
23
|
dim: (msg) => console.log(colors.dim(msg))
|
|
23
24
|
};
|
|
24
|
-
var CONFIG_CANDIDATES = [
|
|
25
|
-
"offlinekit.config.ts",
|
|
26
|
-
"offlinekit.config.js",
|
|
27
|
-
"offlinekit.schema.ts",
|
|
28
|
-
"offlinekit.schema.js"
|
|
29
|
-
];
|
|
30
|
-
function findSchemaFile(cwd = process.cwd()) {
|
|
31
|
-
for (const candidate of CONFIG_CANDIDATES) {
|
|
32
|
-
const p = resolve(cwd, candidate);
|
|
33
|
-
if (existsSync(p)) return p;
|
|
34
|
-
}
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
async function loadSchema(filePath) {
|
|
38
|
-
const mod = await import(filePath);
|
|
39
|
-
return mod.default ?? mod;
|
|
40
|
-
}
|
|
41
25
|
|
|
42
26
|
// src/cli/commands/dev.ts
|
|
43
27
|
function registerDev(program2) {
|
|
@@ -114,7 +98,7 @@ export async function authMiddleware(c: any, next: () => Promise<void>) {
|
|
|
114
98
|
const token = header.startsWith('Bearer ') ? header.slice(7) : null
|
|
115
99
|
if (!token) return c.json({ error: 'Unauthorized' }, 401)
|
|
116
100
|
try {
|
|
117
|
-
const payload = await verify(token, c.env.JWT_SECRET) as { sub: string }
|
|
101
|
+
const payload = await verify(token, c.env.JWT_SECRET, 'HS256') as { sub: string }
|
|
118
102
|
c.set('userId', payload.sub)
|
|
119
103
|
} catch {
|
|
120
104
|
return c.json({ error: 'Invalid token' }, 401)
|
|
@@ -133,7 +117,7 @@ auth.post('/signup', async (c) => {
|
|
|
133
117
|
const user: StoredUser = { userId, email, passwordHash }
|
|
134
118
|
await c.env.KV.put(AUTH_KEY(email), JSON.stringify(user))
|
|
135
119
|
|
|
136
|
-
const token = await sign({ sub: userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET)
|
|
120
|
+
const token = await sign({ sub: userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET, 'HS256')
|
|
137
121
|
return c.json({ user: { id: userId, email }, token }, 201)
|
|
138
122
|
})
|
|
139
123
|
|
|
@@ -147,7 +131,7 @@ auth.post('/signin', async (c) => {
|
|
|
147
131
|
const user = JSON.parse(raw) as StoredUser
|
|
148
132
|
if (!timingSafeEqual(user.passwordHash, passwordHash)) return c.json({ error: 'Invalid credentials' }, 401)
|
|
149
133
|
|
|
150
|
-
const token = await sign({ sub: user.userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET)
|
|
134
|
+
const token = await sign({ sub: user.userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET, 'HS256')
|
|
151
135
|
return c.json({ user: { id: user.userId, email }, token })
|
|
152
136
|
})
|
|
153
137
|
|
|
@@ -299,7 +283,7 @@ export class WsSessions {
|
|
|
299
283
|
private async handle(ws: WebSocket, session: { userId: string | null }, msg: WsMsg): Promise<void> {
|
|
300
284
|
if (msg.type === 'auth') {
|
|
301
285
|
try {
|
|
302
|
-
const payload = await verify(msg.token ?? '', this.env.JWT_SECRET) as { sub?: string }
|
|
286
|
+
const payload = await verify(msg.token ?? '', this.env.JWT_SECRET, 'HS256') as { sub?: string }
|
|
303
287
|
if (!payload.sub) throw new Error('Missing sub')
|
|
304
288
|
session.userId = payload.sub
|
|
305
289
|
ws.send(JSON.stringify({ type: 'auth_ack', id: msg.id }))
|
|
@@ -440,7 +424,7 @@ export interface StoredSession {
|
|
|
440
424
|
}
|
|
441
425
|
|
|
442
426
|
// src/cli/generator/templates/wrangler.ts
|
|
443
|
-
function wranglerTemplate(appName) {
|
|
427
|
+
function wranglerTemplate(appName, kvNamespaceId) {
|
|
444
428
|
const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, "-");
|
|
445
429
|
return `name = "${name}-worker"
|
|
446
430
|
main = "src/index.ts"
|
|
@@ -453,7 +437,7 @@ bucket_name = "${name}-storage"
|
|
|
453
437
|
|
|
454
438
|
[[kv_namespaces]]
|
|
455
439
|
binding = "KV"
|
|
456
|
-
id = "REPLACE_WITH_KV_NAMESPACE_ID"
|
|
440
|
+
id = "${kvNamespaceId ?? "REPLACE_WITH_KV_NAMESPACE_ID"}"
|
|
457
441
|
|
|
458
442
|
[[durable_objects.bindings]]
|
|
459
443
|
name = "WS_SESSIONS"
|
|
@@ -461,7 +445,7 @@ class_name = "WsSessions"
|
|
|
461
445
|
|
|
462
446
|
[[migrations]]
|
|
463
447
|
tag = "v1"
|
|
464
|
-
|
|
448
|
+
new_sqlite_classes = ["WsSessions"]
|
|
465
449
|
|
|
466
450
|
# Security: Set secrets via Cloudflare dashboard or CLI:
|
|
467
451
|
# wrangler secret put JWT_SECRET
|
|
@@ -470,7 +454,7 @@ new_classes = ["WsSessions"]
|
|
|
470
454
|
|
|
471
455
|
// src/cli/generator/index.ts
|
|
472
456
|
function generateWorker(options) {
|
|
473
|
-
const { appName, outDir } = options;
|
|
457
|
+
const { appName, outDir, kvNamespaceId } = options;
|
|
474
458
|
const dirs = [
|
|
475
459
|
outDir,
|
|
476
460
|
join(outDir, "src"),
|
|
@@ -479,7 +463,7 @@ function generateWorker(options) {
|
|
|
479
463
|
];
|
|
480
464
|
for (const dir of dirs) mkdirSync(dir, { recursive: true });
|
|
481
465
|
const files = [
|
|
482
|
-
[join(outDir, "wrangler.toml"), wranglerTemplate(appName)],
|
|
466
|
+
[join(outDir, "wrangler.toml"), wranglerTemplate(appName, kvNamespaceId)],
|
|
483
467
|
[join(outDir, "src", "index.ts"), workerIndexTemplate()],
|
|
484
468
|
[join(outDir, "src", "routes", "auth.ts"), authTemplate()],
|
|
485
469
|
[join(outDir, "src", "routes", "sync.ts"), syncTemplate()],
|
|
@@ -494,8 +478,9 @@ function generateWorker(options) {
|
|
|
494
478
|
for (const [path, content] of files) {
|
|
495
479
|
writeFileSync(path, content, "utf8");
|
|
496
480
|
}
|
|
481
|
+
const kvWarning = kvNamespaceId ? "" : " - KV namespace id \u2192 set your actual KV namespace ID\n";
|
|
497
482
|
console.warn(
|
|
498
|
-
'\n\u26A0 Remember to replace placeholder values in wrangler.toml before deploying:\n - JWT_SECRET = "REPLACE_WITH_SECRET" \u2192 set a strong random secret\n
|
|
483
|
+
'\n\u26A0 Remember to replace placeholder values in wrangler.toml before deploying:\n - JWT_SECRET = "REPLACE_WITH_SECRET" \u2192 set a strong random secret\n' + kvWarning
|
|
499
484
|
);
|
|
500
485
|
}
|
|
501
486
|
function workerPackageJson(appName) {
|
|
@@ -545,8 +530,8 @@ function workerTsConfig() {
|
|
|
545
530
|
// src/cli/targets/cloudflare.ts
|
|
546
531
|
var cloudflareTarget = {
|
|
547
532
|
name: "cloudflare",
|
|
548
|
-
generate({ appName, outDir }) {
|
|
549
|
-
generateWorker({ appName, outDir });
|
|
533
|
+
generate({ appName, outDir, kvNamespaceId }) {
|
|
534
|
+
generateWorker({ appName, outDir, kvNamespaceId });
|
|
550
535
|
}
|
|
551
536
|
};
|
|
552
537
|
var nodeTarget = {
|
|
@@ -700,19 +685,6 @@ function registerBuild(program2) {
|
|
|
700
685
|
log.error(`Unknown target: ${opts.target}. Valid targets: ${Object.keys(targets).join(", ")}`);
|
|
701
686
|
process.exit(1);
|
|
702
687
|
}
|
|
703
|
-
const schemaFile = findSchemaFile();
|
|
704
|
-
if (!schemaFile) {
|
|
705
|
-
log.error("No schema file found. Create offlinekit.config.ts in your project root.");
|
|
706
|
-
process.exit(1);
|
|
707
|
-
}
|
|
708
|
-
log.info(`Found schema: ${schemaFile}`);
|
|
709
|
-
try {
|
|
710
|
-
await loadSchema(schemaFile);
|
|
711
|
-
log.success("Schema loaded successfully");
|
|
712
|
-
} catch (err) {
|
|
713
|
-
log.error(`Schema load failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
714
|
-
process.exit(1);
|
|
715
|
-
}
|
|
716
688
|
const outDir = resolve(process.cwd(), opts.out);
|
|
717
689
|
log.info(`Generating ${target.name} backend to: ${outDir}`);
|
|
718
690
|
try {
|
|
@@ -729,66 +701,305 @@ function registerBuild(program2) {
|
|
|
729
701
|
}
|
|
730
702
|
});
|
|
731
703
|
}
|
|
704
|
+
function runWrangler(args, options) {
|
|
705
|
+
const result = spawnSync("wrangler", args, {
|
|
706
|
+
encoding: "utf8",
|
|
707
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
708
|
+
...options
|
|
709
|
+
});
|
|
710
|
+
return {
|
|
711
|
+
stdout: result.stdout ?? "",
|
|
712
|
+
stderr: result.stderr ?? "",
|
|
713
|
+
status: result.status
|
|
714
|
+
};
|
|
715
|
+
}
|
|
732
716
|
function checkWrangler() {
|
|
733
717
|
try {
|
|
734
|
-
const result = execSync("wrangler --version", {
|
|
718
|
+
const result = execSync("wrangler --version", {
|
|
719
|
+
encoding: "utf8",
|
|
720
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
721
|
+
});
|
|
735
722
|
return result.trim();
|
|
736
723
|
} catch {
|
|
737
724
|
return null;
|
|
738
725
|
}
|
|
739
726
|
}
|
|
740
|
-
function
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
if (
|
|
749
|
-
log.
|
|
750
|
-
|
|
751
|
-
process.exit(1);
|
|
752
|
-
}
|
|
753
|
-
const version = checkWrangler();
|
|
754
|
-
if (!version) {
|
|
755
|
-
log.error("wrangler is not installed or not in PATH.");
|
|
756
|
-
log.dim("Install it with: npm install -g wrangler");
|
|
757
|
-
process.exit(1);
|
|
727
|
+
function checkAuth() {
|
|
728
|
+
const result = runWrangler(["whoami"]);
|
|
729
|
+
return result.status === 0;
|
|
730
|
+
}
|
|
731
|
+
function createR2Bucket(name) {
|
|
732
|
+
log.info(`Creating R2 bucket: ${name}`);
|
|
733
|
+
const result = runWrangler(["r2", "bucket", "create", name]);
|
|
734
|
+
if (result.status !== 0) {
|
|
735
|
+
if (result.stderr.includes("already exists")) {
|
|
736
|
+
log.info(`R2 bucket "${name}" already exists, continuing`);
|
|
737
|
+
return;
|
|
758
738
|
}
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
process.exit(1);
|
|
739
|
+
if (result.stderr.includes("R2") || result.stderr.includes("not allowed") || result.stderr.includes("not enabled")) {
|
|
740
|
+
throw new Error(
|
|
741
|
+
`Failed to create R2 bucket: ${result.stderr}
|
|
742
|
+
Hint: Enable R2 in your Cloudflare dashboard at https://dash.cloudflare.com \u2192 R2 Object Storage before deploying.`
|
|
743
|
+
);
|
|
765
744
|
}
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
745
|
+
throw new Error(`Failed to create R2 bucket: ${result.stderr}`);
|
|
746
|
+
}
|
|
747
|
+
log.success(`R2 bucket "${name}" created`);
|
|
748
|
+
}
|
|
749
|
+
function kvNamespaceExists(id) {
|
|
750
|
+
const result = runWrangler(["kv", "namespace", "list", "--json"]);
|
|
751
|
+
if (result.status !== 0) return false;
|
|
752
|
+
try {
|
|
753
|
+
const namespaces = JSON.parse(result.stdout);
|
|
754
|
+
return namespaces.some((ns) => ns.id === id);
|
|
755
|
+
} catch {
|
|
756
|
+
return false;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
function createKvNamespace(name) {
|
|
760
|
+
log.info(`Creating KV namespace: ${name}`);
|
|
761
|
+
const result = runWrangler(["kv", "namespace", "create", name]);
|
|
762
|
+
if (result.status === 0) {
|
|
763
|
+
const match = result.stdout.match(/id\s*=\s*"([a-f0-9]+)"/);
|
|
764
|
+
if (match) {
|
|
765
|
+
log.success(`KV namespace "${name}" created with ID: ${match[1]}`);
|
|
766
|
+
return match[1];
|
|
771
767
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
if (opts.dryRun) {
|
|
777
|
-
log.dim(`[dry-run] wrangler ${args.join(" ")}`);
|
|
778
|
-
log.dim(`[dry-run] cwd: ${outDir}`);
|
|
779
|
-
return;
|
|
768
|
+
try {
|
|
769
|
+
const parsed = JSON.parse(result.stdout);
|
|
770
|
+
if (parsed.id) return parsed.id;
|
|
771
|
+
} catch {
|
|
780
772
|
}
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
773
|
+
}
|
|
774
|
+
if (result.stderr.includes("already exists") || result.status !== 0) {
|
|
775
|
+
log.info(`KV namespace "${name}" may already exist, looking up ID...`);
|
|
776
|
+
const listResult = runWrangler(["kv", "namespace", "list", "--json"]);
|
|
777
|
+
if (listResult.status === 0) {
|
|
778
|
+
try {
|
|
779
|
+
const namespaces = JSON.parse(listResult.stdout);
|
|
780
|
+
const found = namespaces.find((ns) => ns.title === name);
|
|
781
|
+
if (found) {
|
|
782
|
+
log.success(`Found existing KV namespace "${name}" with ID: ${found.id}`);
|
|
783
|
+
return found.id;
|
|
784
|
+
}
|
|
785
|
+
} catch {
|
|
786
|
+
log.warn("Failed to parse KV namespace list JSON, falling back to regex");
|
|
787
|
+
}
|
|
788
|
+
const idMatch = listResult.stdout.match(/id\s*=\s*"([a-f0-9]+)"/);
|
|
789
|
+
if (idMatch) return idMatch[1];
|
|
789
790
|
}
|
|
790
|
-
|
|
791
|
+
}
|
|
792
|
+
throw new Error(
|
|
793
|
+
`Failed to create or find KV namespace "${name}". stdout: ${result.stdout}, stderr: ${result.stderr}`
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
function setSecret(name, value, cwd) {
|
|
797
|
+
log.info(`Setting secret: ${name}`);
|
|
798
|
+
const result = runWrangler(["secret", "put", name], { cwd, input: value });
|
|
799
|
+
if (result.status !== 0) {
|
|
800
|
+
throw new Error(`Failed to set secret ${name}: ${result.stderr}`);
|
|
801
|
+
}
|
|
802
|
+
log.success(`Secret "${name}" set`);
|
|
803
|
+
}
|
|
804
|
+
function readDeployState(outDir) {
|
|
805
|
+
const statePath = resolve(outDir, "..", "deploy-state.json");
|
|
806
|
+
if (!existsSync(statePath)) return null;
|
|
807
|
+
try {
|
|
808
|
+
return JSON.parse(readFileSync(statePath, "utf8"));
|
|
809
|
+
} catch {
|
|
810
|
+
return null;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
function writeDeployState(outDir, state) {
|
|
814
|
+
const statePath = resolve(outDir, "..", "deploy-state.json");
|
|
815
|
+
mkdirSync(dirname(statePath), { recursive: true });
|
|
816
|
+
writeFileSync(statePath, JSON.stringify(state, null, 2), "utf8");
|
|
817
|
+
}
|
|
818
|
+
function provisionCloudflare(appName, _outDir, dryRun) {
|
|
819
|
+
const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, "-");
|
|
820
|
+
const r2BucketName = `${name}-storage`;
|
|
821
|
+
const kvTitle = `${name}-kv`;
|
|
822
|
+
const workerName = `${name}-worker`;
|
|
823
|
+
if (dryRun) {
|
|
824
|
+
log.dim("[dry-run] Would check wrangler auth (wrangler whoami)");
|
|
825
|
+
log.dim(`[dry-run] Would create R2 bucket: ${r2BucketName}`);
|
|
826
|
+
log.dim(`[dry-run] Would create KV namespace: ${kvTitle}`);
|
|
827
|
+
log.dim("[dry-run] Would set JWT_SECRET");
|
|
828
|
+
return { kvNamespaceId: "DRY_RUN_PLACEHOLDER", r2BucketName, workerName };
|
|
829
|
+
}
|
|
830
|
+
if (!checkAuth()) {
|
|
831
|
+
log.error("Not authenticated with Cloudflare.");
|
|
832
|
+
log.dim("Run `wrangler login` to authenticate, then try again.");
|
|
833
|
+
process.exit(1);
|
|
834
|
+
}
|
|
835
|
+
log.success("Authenticated with Cloudflare");
|
|
836
|
+
createR2Bucket(r2BucketName);
|
|
837
|
+
const kvNamespaceId = createKvNamespace(kvTitle);
|
|
838
|
+
return { kvNamespaceId, r2BucketName, workerName };
|
|
839
|
+
}
|
|
840
|
+
function generateAndDeploy(appName, outDir, kvNamespaceId, env, dryRun) {
|
|
841
|
+
if (dryRun) {
|
|
842
|
+
log.dim(`[dry-run] Would generate worker code to: ${outDir}`);
|
|
843
|
+
log.dim(`[dry-run] Would run: npm install (in ${outDir})`);
|
|
844
|
+
log.dim(`[dry-run] Would run: wrangler deploy${env ? ` --env ${env}` : ""}`);
|
|
845
|
+
return void 0;
|
|
846
|
+
}
|
|
847
|
+
log.info("Generating worker code...");
|
|
848
|
+
cloudflareTarget.generate({ appName, outDir, kvNamespaceId });
|
|
849
|
+
log.success("Worker code generated");
|
|
850
|
+
log.info("Installing dependencies...");
|
|
851
|
+
const installResult = spawnSync("npm", ["install"], {
|
|
852
|
+
cwd: outDir,
|
|
853
|
+
encoding: "utf8",
|
|
854
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
855
|
+
});
|
|
856
|
+
if (installResult.status !== 0) {
|
|
857
|
+
throw new Error(`npm install failed: ${installResult.stderr}`);
|
|
858
|
+
}
|
|
859
|
+
log.success("Dependencies installed");
|
|
860
|
+
log.info("Deploying to Cloudflare...");
|
|
861
|
+
const deployArgs = ["deploy"];
|
|
862
|
+
if (env) deployArgs.push("--env", env);
|
|
863
|
+
const deployResult = spawnSync("wrangler", deployArgs, {
|
|
864
|
+
cwd: outDir,
|
|
865
|
+
encoding: "utf8",
|
|
866
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
791
867
|
});
|
|
868
|
+
if (deployResult.status !== 0) {
|
|
869
|
+
throw new Error(
|
|
870
|
+
`wrangler deploy failed (exit ${deployResult.status}): ${deployResult.stderr}`
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
const urlMatch = deployResult.stdout.match(/https:\/\/[^\s]+\.workers\.dev/);
|
|
874
|
+
const workerUrl = urlMatch ? urlMatch[0] : void 0;
|
|
875
|
+
if (workerUrl) {
|
|
876
|
+
log.success(`Deployed to: ${workerUrl}`);
|
|
877
|
+
} else {
|
|
878
|
+
log.success("Deployed successfully!");
|
|
879
|
+
}
|
|
880
|
+
return workerUrl;
|
|
881
|
+
}
|
|
882
|
+
function registerDeploy(program2) {
|
|
883
|
+
program2.command("deploy").description("Deploy the LocalKit Worker to Cloudflare via Wrangler").option("-d, --dir <dir>", "Worker output directory", ".offlinekit/worker").option("-n, --name <name>", "App name for the worker", "localkit-app").option("-e, --env <env>", "Wrangler environment (e.g. production, staging)").option("-t, --target <target>", "Deploy target (default: cloudflare)", "cloudflare").option("--dry-run", "Show what would happen without executing").action(
|
|
884
|
+
(opts) => {
|
|
885
|
+
log.bold("LocalKit Deploy");
|
|
886
|
+
const dryRun = opts.dryRun ?? false;
|
|
887
|
+
const serverDir = resolve(process.cwd(), "server");
|
|
888
|
+
if (existsSync(serverDir)) {
|
|
889
|
+
log.info(
|
|
890
|
+
"Warning: This project has been ejected (./server/ exists)."
|
|
891
|
+
);
|
|
892
|
+
log.dim(
|
|
893
|
+
"You can deploy the ejected code directly with `cd server && wrangler deploy`."
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
if (opts.target !== "cloudflare") {
|
|
897
|
+
log.error(
|
|
898
|
+
`Deploy target "${opts.target}" is not supported yet. Only "cloudflare" is deployable.`
|
|
899
|
+
);
|
|
900
|
+
log.dim(
|
|
901
|
+
"For Node.js targets, run `npx mpb-localkit build --target node` then deploy the output manually."
|
|
902
|
+
);
|
|
903
|
+
process.exit(1);
|
|
904
|
+
}
|
|
905
|
+
const version = checkWrangler();
|
|
906
|
+
if (!version) {
|
|
907
|
+
log.error("wrangler is not installed or not in PATH.");
|
|
908
|
+
log.dim("Install it with: npm install -g wrangler");
|
|
909
|
+
process.exit(1);
|
|
910
|
+
}
|
|
911
|
+
log.success(`wrangler ${version}`);
|
|
912
|
+
const outDir = resolve(process.cwd(), opts.dir);
|
|
913
|
+
const appName = opts.name;
|
|
914
|
+
const existingState = readDeployState(outDir);
|
|
915
|
+
let kvNamespaceId;
|
|
916
|
+
let r2BucketName;
|
|
917
|
+
let workerName;
|
|
918
|
+
if (existingState?.kvNamespaceId && existingState?.r2BucketName) {
|
|
919
|
+
log.info("Found existing deploy state, validating resources...");
|
|
920
|
+
r2BucketName = existingState.r2BucketName;
|
|
921
|
+
workerName = existingState.workerName;
|
|
922
|
+
if (dryRun) {
|
|
923
|
+
log.dim("[dry-run] Would validate KV namespace exists");
|
|
924
|
+
kvNamespaceId = existingState.kvNamespaceId;
|
|
925
|
+
} else if (kvNamespaceExists(existingState.kvNamespaceId)) {
|
|
926
|
+
log.success("KV namespace validated");
|
|
927
|
+
kvNamespaceId = existingState.kvNamespaceId;
|
|
928
|
+
} else {
|
|
929
|
+
log.warn("KV namespace no longer exists, re-creating...");
|
|
930
|
+
const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, "-");
|
|
931
|
+
kvNamespaceId = createKvNamespace(`${name}-kv`);
|
|
932
|
+
writeDeployState(outDir, {
|
|
933
|
+
...existingState,
|
|
934
|
+
kvNamespaceId
|
|
935
|
+
});
|
|
936
|
+
log.success("Deploy state updated with new KV namespace ID");
|
|
937
|
+
}
|
|
938
|
+
} else {
|
|
939
|
+
const provisioned = provisionCloudflare(appName, outDir, dryRun);
|
|
940
|
+
kvNamespaceId = provisioned.kvNamespaceId;
|
|
941
|
+
r2BucketName = provisioned.r2BucketName;
|
|
942
|
+
workerName = provisioned.workerName;
|
|
943
|
+
if (!dryRun) {
|
|
944
|
+
writeDeployState(outDir, {
|
|
945
|
+
kvNamespaceId,
|
|
946
|
+
r2BucketName,
|
|
947
|
+
workerName,
|
|
948
|
+
jwtSecretSet: false
|
|
949
|
+
});
|
|
950
|
+
log.success("Deploy state saved to .offlinekit/deploy-state.json");
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
if (kvNamespaceId === "REPLACE_WITH_KV_NAMESPACE_ID" || !kvNamespaceId) {
|
|
954
|
+
log.error(
|
|
955
|
+
"KV namespace ID is missing or still a placeholder. Run `mpb-localkit deploy` (not `wrangler deploy` directly) to auto-provision resources, or create one manually: wrangler kv namespace create KV"
|
|
956
|
+
);
|
|
957
|
+
process.exit(1);
|
|
958
|
+
}
|
|
959
|
+
const alreadyHasSecret = existingState?.jwtSecretSet ?? false;
|
|
960
|
+
let workerUrl;
|
|
961
|
+
try {
|
|
962
|
+
workerUrl = generateAndDeploy(appName, outDir, kvNamespaceId, opts.env, dryRun);
|
|
963
|
+
} catch (err) {
|
|
964
|
+
log.error(`Deploy failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
965
|
+
process.exit(1);
|
|
966
|
+
}
|
|
967
|
+
if (dryRun) {
|
|
968
|
+
if (!alreadyHasSecret) {
|
|
969
|
+
log.dim("[dry-run] Would set JWT_SECRET (first deploy only)");
|
|
970
|
+
}
|
|
971
|
+
log.bold("Dry run complete. No changes were made.");
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
let jwtSecretSet = alreadyHasSecret;
|
|
975
|
+
if (!jwtSecretSet) {
|
|
976
|
+
try {
|
|
977
|
+
setSecret("JWT_SECRET", randomBytes(32).toString("hex"), outDir);
|
|
978
|
+
log.success("JWT_SECRET set");
|
|
979
|
+
jwtSecretSet = true;
|
|
980
|
+
} catch (err) {
|
|
981
|
+
log.warn(`Could not set JWT_SECRET: ${err instanceof Error ? err.message : String(err)}`);
|
|
982
|
+
log.dim("Set it manually: wrangler secret put JWT_SECRET");
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
writeDeployState(outDir, { kvNamespaceId, r2BucketName, workerName, workerUrl, jwtSecretSet });
|
|
986
|
+
if (workerUrl) {
|
|
987
|
+
const envPath = resolve(process.cwd(), ".env");
|
|
988
|
+
if (existsSync(envPath)) {
|
|
989
|
+
let envContent = readFileSync(envPath, "utf8");
|
|
990
|
+
if (envContent.includes("VITE_SYNC_URL=")) {
|
|
991
|
+
envContent = envContent.replace(/VITE_SYNC_URL=.*/, `VITE_SYNC_URL=${workerUrl}`);
|
|
992
|
+
} else {
|
|
993
|
+
envContent += `
|
|
994
|
+
VITE_SYNC_URL=${workerUrl}
|
|
995
|
+
`;
|
|
996
|
+
}
|
|
997
|
+
writeFileSync(envPath, envContent, "utf8");
|
|
998
|
+
log.info(`Updated .env with VITE_SYNC_URL=${workerUrl}`);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
);
|
|
792
1003
|
}
|
|
793
1004
|
var targets2 = {
|
|
794
1005
|
cloudflare: cloudflareTarget,
|
package/dist/cli/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/utils.ts","../../src/cli/commands/dev.ts","../../src/cli/generator/templates/index.ts","../../src/cli/generator/templates/auth.ts","../../src/cli/generator/templates/sync.ts","../../src/cli/generator/templates/ws.ts","../../src/cli/generator/templates/errors.ts","../../src/cli/generator/templates/health.ts","../../src/cli/generator/templates/r2-storage.ts","../../src/cli/generator/templates/kv-storage.ts","../../src/cli/generator/templates/wrangler.ts","../../src/cli/generator/index.ts","../../src/cli/targets/cloudflare.ts","../../src/cli/targets/node.ts","../../src/cli/commands/build.ts","../../src/cli/commands/deploy.ts","../../src/cli/commands/eject.ts","../../src/cli/index.ts"],"names":["program","join","mkdirSync","writeFileSync","resolve","existsSync","targets","require"],"mappings":";;;;;;;AAIA,IAAM,MAAA,GAAS;AAAA,EACb,KAAA,EAAO,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EAClC,MAAA,EAAQ,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EACnC,GAAA,EAAK,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EAChC,IAAA,EAAM,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EACjC,IAAA,EAAM,CAAC,CAAA,KAAc,CAAA,OAAA,EAAU,CAAC,CAAA,OAAA,CAAA;AAAA,EAChC,GAAA,EAAK,CAAC,CAAA,KAAc,CAAA,OAAA,EAAU,CAAC,CAAA,OAAA;AACjC,CAAA;AAEO,IAAM,GAAA,GAAM;AAAA,EACjB,IAAA,EAAM,CAAC,GAAA,KAAgB,OAAA,CAAQ,GAAA,CAAI,CAAA,EAAG,MAAA,CAAO,IAAA,CAAK,QAAG,CAAC,CAAA,CAAA,EAAI,GAAG,CAAA,CAAE,CAAA;AAAA,EAC/D,OAAA,EAAS,CAAC,GAAA,KAAgB,OAAA,CAAQ,GAAA,CAAI,CAAA,EAAG,MAAA,CAAO,KAAA,CAAM,QAAG,CAAC,CAAA,CAAA,EAAI,GAAG,CAAA,CAAE,CAAA;AAAA,EACnE,IAAA,EAAM,CAAC,GAAA,KAAgB,OAAA,CAAQ,IAAA,CAAK,CAAA,EAAG,MAAA,CAAO,MAAA,CAAO,QAAG,CAAC,CAAA,CAAA,EAAI,GAAG,CAAA,CAAE,CAAA;AAAA,EAClE,KAAA,EAAO,CAAC,GAAA,KAAgB,OAAA,CAA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,OAAA,CAAQ,KAAA,CAAM,QAAQ,IAAI,CAAA","file":"index.js","sourcesContent":["import { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\n/** Color helpers for terminal output (no dependencies) */\nconst colors = {\n green: (s: string) => `\\x1b[32m${s}\\x1b[0m`,\n yellow: (s: string) => `\\x1b[33m${s}\\x1b[0m`,\n red: (s: string) => `\\x1b[31m${s}\\x1b[0m`,\n cyan: (s: string) => `\\x1b[36m${s}\\x1b[0m`,\n bold: (s: string) => `\\x1b[1m${s}\\x1b[0m`,\n dim: (s: string) => `\\x1b[2m${s}\\x1b[0m`,\n}\n\nexport const log = {\n info: (msg: string) => console.log(`${colors.cyan('ℹ')} ${msg}`),\n success: (msg: string) => console.log(`${colors.green('✓')} ${msg}`),\n warn: (msg: string) => console.warn(`${colors.yellow('⚠')} ${msg}`),\n error: (msg: string) => console.error(`${colors.red('✗')} ${msg}`),\n bold: (msg: string) => console.log(colors.bold(msg)),\n dim: (msg: string) => console.log(colors.dim(msg)),\n}\n\nconst CONFIG_CANDIDATES = [\n 'offlinekit.config.ts',\n 'offlinekit.config.js',\n 'offlinekit.schema.ts',\n 'offlinekit.schema.js',\n]\n\n/** Finds the OfflineKit config/schema file in the current working directory */\nexport function findSchemaFile(cwd = process.cwd()): string | null {\n for (const candidate of CONFIG_CANDIDATES) {\n const p = resolve(cwd, candidate)\n if (existsSync(p)) return p\n }\n return null\n}\n\n/** Dynamically imports and returns the default export from the schema file */\nexport async function loadSchema(filePath: string): Promise<unknown> {\n const mod = await import(filePath) as { default?: unknown }\n return mod.default ?? mod\n}\n","import type { Command } from 'commander'\nimport { log } from '../utils.js'\n\nexport function registerDev(program: Command): void {\n program\n .command('dev')\n .description('Start LocalKit in local-only mode (no cloud sync)')\n .action(() => {\n process.env['OFFLINEKIT_MODE'] = 'local'\n log.bold('LocalKit Dev Mode')\n log.info('Running in local-only mode (no cloud sync)')\n log.dim('All data is stored locally. Sync is disabled.')\n })\n}\n","/** Main Hono app entry point template */\nexport function workerIndexTemplate(_appName: string): string {\n return `\nimport { Hono } from 'hono'\nimport { cors } from 'hono/cors'\nimport { auth } from './routes/auth.js'\nimport { sync } from './routes/sync.js'\nimport { ws, WsSessions } from './routes/ws.js'\nimport { errors } from './routes/errors.js'\nimport { health } from './routes/health.js'\n\nexport interface Env {\n STORAGE: R2Bucket\n KV: KVNamespace\n JWT_SECRET: string\n WS_SESSIONS: DurableObjectNamespace\n}\n\nexport { WsSessions }\n\nconst app = new Hono<{ Bindings: Env }>()\n\napp.use('/*', cors({\n origin: '*',\n allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],\n allowHeaders: ['Content-Type', 'Authorization'],\n}))\n\napp.route('/auth', auth)\napp.route('/sync', sync)\napp.route('/ws', ws)\napp.route('/errors', errors)\napp.route('/health', health)\n\nexport default app\n`.trimStart()\n}\n","/** Auth routes: POST /auth/signup, /auth/signin, /auth/signout */\nexport function authTemplate(): string {\n return `\nimport { Hono } from 'hono'\nimport { sign, verify } from 'hono/jwt'\nimport type { Env } from './index.js'\nimport { AUTH_KEY, SESSION_KEY, type StoredUser } from '../storage/kv.js'\n\nexport const auth = new Hono<{ Bindings: Env }>()\n\n/** Constant-time string comparison to prevent timing side-channels */\nfunction timingSafeEqual(a: string, b: string): boolean {\n if (a.length !== b.length) return false\n const encoder = new TextEncoder()\n const bufA = encoder.encode(a)\n const bufB = encoder.encode(b)\n let result = 0\n for (let i = 0; i < bufA.length; i++) result |= bufA[i] ^ bufB[i]\n return result === 0\n}\n\n/** Extract userId from Bearer token — throws on invalid/expired */\nexport async function authMiddleware(c: any, next: () => Promise<void>) {\n const header = c.req.header('Authorization') ?? ''\n const token = header.startsWith('Bearer ') ? header.slice(7) : null\n if (!token) return c.json({ error: 'Unauthorized' }, 401)\n try {\n const payload = await verify(token, c.env.JWT_SECRET) as { sub: string }\n c.set('userId', payload.sub)\n } catch {\n return c.json({ error: 'Invalid token' }, 401)\n }\n return next()\n}\n\nauth.post('/signup', async (c) => {\n const { email, passwordHash } = await c.req.json<{ email: string; passwordHash: string }>()\n if (!email || !passwordHash) return c.json({ error: 'email and passwordHash required' }, 400)\n\n const existing = await c.env.KV.get(AUTH_KEY(email))\n if (existing) return c.json({ error: 'Email already registered' }, 409)\n\n const userId = crypto.randomUUID()\n const user: StoredUser = { userId, email, passwordHash }\n await c.env.KV.put(AUTH_KEY(email), JSON.stringify(user))\n\n const token = await sign({ sub: userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET)\n return c.json({ user: { id: userId, email }, token }, 201)\n})\n\nauth.post('/signin', async (c) => {\n const { email, passwordHash } = await c.req.json<{ email: string; passwordHash: string }>()\n if (!email || !passwordHash) return c.json({ error: 'email and passwordHash required' }, 400)\n\n const raw = await c.env.KV.get(AUTH_KEY(email))\n if (!raw) return c.json({ error: 'Invalid credentials' }, 401)\n\n const user = JSON.parse(raw) as StoredUser\n if (!timingSafeEqual(user.passwordHash, passwordHash)) return c.json({ error: 'Invalid credentials' }, 401)\n\n const token = await sign({ sub: user.userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET)\n return c.json({ user: { id: user.userId, email }, token })\n})\n\nauth.post('/signout', async (c) => {\n const header = c.req.header('Authorization') ?? ''\n const token = header.startsWith('Bearer ') ? header.slice(7) : null\n if (token) await c.env.KV.delete(SESSION_KEY(token))\n return c.json({ ok: true })\n})\n`.trimStart()\n}\n","/** Sync routes: POST /sync/push, GET /sync/pull?since= */\nexport function syncTemplate(): string {\n return `\nimport { Hono } from 'hono'\nimport type { Env } from './index.js'\nimport { authMiddleware } from './auth.js'\nimport { docKey, collectionPrefix } from '../storage/r2.js'\n\ninterface SyncDoc {\n _id: string\n _collection: string\n _updatedAt: number\n _deleted: boolean\n [key: string]: unknown\n}\n\nexport const sync = new Hono<{ Bindings: Env }>()\n\nsync.use('/*', authMiddleware)\n\nsync.post('/push', async (c) => {\n const userId = c.get('userId') as string\n const { changes } = await c.req.json<{ changes: SyncDoc[] }>()\n\n await Promise.all(\n changes.map(async (doc) => {\n const key = docKey(userId, doc._collection, doc._id)\n const existing = await c.env.STORAGE.get(key, 'json') as SyncDoc | null\n\n // LWW — only overwrite if incoming is newer\n if (!existing || doc._updatedAt >= existing._updatedAt) {\n await c.env.STORAGE.put(key, JSON.stringify(doc), {\n customMetadata: { updatedAt: String(doc._updatedAt) },\n })\n }\n }),\n )\n\n return c.json({ ok: true, received: changes.length })\n})\n\nsync.get('/pull', async (c) => {\n const userId = c.get('userId') as string\n const since = parseInt(c.req.query('since') ?? '0', 10)\n\n // Scan all collections for this user — list all objects under {userId}/\n const prefix = \\`\\${userId}/\\`\n const listed = await c.env.STORAGE.list({ prefix })\n\n const changes = await Promise.all(\n listed.objects\n .filter((obj) => {\n const ts = parseInt(obj.customMetadata?.updatedAt ?? '0', 10)\n return ts > since\n })\n .map(async (obj) => {\n const blob = await c.env.STORAGE.get(obj.key, 'json')\n return blob as SyncDoc\n }),\n )\n\n return c.json({ changes: changes.filter(Boolean) })\n})\n`.trimStart()\n}\n","/** WebSocket Durable Object handler for real-time sync */\nexport function wsTemplate(): string {\n return `import { Hono } from 'hono'\nimport { verify } from 'hono/jwt'\nimport type { Env } from './index.js'\nimport { authMiddleware } from './auth.js'\nimport { docKey } from '../storage/r2.js'\n\nexport const ws = new Hono<{ Bindings: Env }>()\n\n// Proxy WS upgrade requests to the user-scoped Durable Object\nws.get('/', authMiddleware, async (c) => {\n const userId = c.get('userId') as string\n const id = c.env.WS_SESSIONS.idFromName(userId)\n return c.env.WS_SESSIONS.get(id).fetch(c.req.raw)\n})\n\n// ---- Durable Object: one instance per userId, owns all WebSocket sessions ----\n\ninterface SyncDoc {\n _id: string\n _collection: string\n _updatedAt: number\n _deleted: boolean\n [key: string]: unknown\n}\n\ninterface WsMsg {\n type: string\n id?: string\n payload?: unknown\n since?: number\n token?: string\n}\n\nexport class WsSessions {\n private readonly sessions = new Map<WebSocket, { userId: string | null }>()\n\n constructor(\n private readonly state: DurableObjectState,\n private readonly env: Env,\n ) {}\n\n async fetch(request: Request): Promise<Response> {\n if (request.headers.get('Upgrade') !== 'websocket') {\n return new Response('Expected WebSocket upgrade', { status: 426 })\n }\n\n const pair = new WebSocketPair()\n const [client, server] = Object.values(pair) as [WebSocket, WebSocket]\n server.accept()\n\n const session = { userId: null as string | null }\n this.sessions.set(server, session)\n\n server.addEventListener('message', async (event) => {\n let msg: WsMsg\n try { msg = JSON.parse(event.data as string) as WsMsg } catch { return }\n try {\n await this.handle(server, session, msg)\n } catch (err) {\n server.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: String(err) } }))\n }\n })\n\n server.addEventListener('close', () => { this.sessions.delete(server) })\n\n return new Response(null, { status: 101, webSocket: client })\n }\n\n private async handle(ws: WebSocket, session: { userId: string | null }, msg: WsMsg): Promise<void> {\n if (msg.type === 'auth') {\n try {\n const payload = await verify(msg.token ?? '', this.env.JWT_SECRET) as { sub?: string }\n if (!payload.sub) throw new Error('Missing sub')\n session.userId = payload.sub\n ws.send(JSON.stringify({ type: 'auth_ack', id: msg.id }))\n } catch {\n ws.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: 'Unauthorized' } }))\n ws.close(1008, 'Unauthorized')\n }\n return\n }\n\n if (!session.userId) {\n ws.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: 'Not authenticated' } }))\n return\n }\n\n if (msg.type === 'push') {\n const { changes } = msg.payload as { changes: SyncDoc[] }\n await Promise.all(changes.map(async (doc) => {\n const key = docKey(session.userId!, doc._collection, doc._id)\n const existing = await this.env.STORAGE.get(key, 'json') as SyncDoc | null\n if (!existing || doc._updatedAt >= existing._updatedAt) {\n await this.env.STORAGE.put(key, JSON.stringify(doc), {\n customMetadata: { updatedAt: String(doc._updatedAt) },\n })\n }\n }))\n ws.send(JSON.stringify({ type: 'push_ack', id: msg.id, payload: { ok: true } }))\n this.broadcast(session.userId, ws, { type: 'remote_changes', changes })\n return\n }\n\n if (msg.type === 'pull') {\n const since = typeof msg.since === 'number' ? msg.since : 0\n const listed = await this.env.STORAGE.list({ prefix: \\`\\${session.userId}/\\` })\n const changes = (await Promise.all(\n listed.objects\n .filter((o) => parseInt(o.customMetadata?.updatedAt ?? '0', 10) > since)\n .map((o) => this.env.STORAGE.get(o.key, 'json') as Promise<SyncDoc>),\n )).filter(Boolean)\n ws.send(JSON.stringify({ type: 'pull_response', id: msg.id, payload: { changes } }))\n return\n }\n\n ws.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: \\`Unknown type: \\${msg.type}\\` } }))\n }\n\n private broadcast(userId: string, sender: WebSocket, msg: object): void {\n const data = JSON.stringify(msg)\n for (const [ws, s] of this.sessions) {\n if (ws !== sender && s.userId === userId) {\n try { ws.send(data) } catch { /* stale connection */ }\n }\n }\n }\n}\n\n// JWT verification handled by hono/jwt verify() — no hand-rolled crypto needed\n`.trimStart()\n}\n","/** GET /errors — list error blobs for authenticated user */\nexport function errorsTemplate(): string {\n return `\nimport { Hono } from 'hono'\nimport type { Env } from './index.js'\nimport { authMiddleware } from './auth.js'\n\nexport const errors = new Hono<{ Bindings: Env }>()\n\nerrors.use('/*', authMiddleware)\n\nerrors.get('/', async (c) => {\n const userId = c.get('userId') as string\n const prefix = \\`\\${userId}/_errors/\\`\n\n const listed = await c.env.STORAGE.list({ prefix })\n const results = await Promise.all(\n listed.objects.map(async (obj) => {\n const blob = await c.env.STORAGE.get(obj.key)\n if (!blob) return null\n return blob.json()\n }),\n )\n\n return c.json({ errors: results.filter(Boolean) })\n})\n`.trimStart()\n}\n","/** GET /health — liveness check */\nexport function healthTemplate(): string {\n return `\nimport { Hono } from 'hono'\n\nexport const health = new Hono()\n\nhealth.get('/', (c) => {\n return c.json({ status: 'ok', timestamp: Date.now() })\n})\n`.trimStart()\n}\n","/** R2 document key helpers — {userId}/{collection}/{docId}.json */\nexport function r2StorageTemplate(): string {\n return `\nexport const docKey = (userId: string, collection: string, docId: string) =>\n \\`\\${userId}/\\${collection}/\\${docId}.json\\`\n\nexport const collectionPrefix = (userId: string, collection: string) =>\n \\`\\${userId}/\\${collection}/\\`\n`.trimStart()\n}\n","/** KV storage helpers — sessions, auth credentials, metadata */\nexport function kvStorageTemplate(): string {\n return `\n/** Key: auth:{email} Value: { userId, email, passwordHash } */\nexport const AUTH_KEY = (email: string) => \\`auth:\\${email}\\`\n\n/** Key: session:{token} Value: { userId, email, exp } */\nexport const SESSION_KEY = (token: string) => \\`session:\\${token}\\`\n\n/** Key: meta:{userId} Value: arbitrary metadata object */\nexport const META_KEY = (userId: string) => \\`meta:\\${userId}\\`\n\nexport interface StoredUser {\n userId: string\n email: string\n passwordHash: string\n}\n\nexport interface StoredSession {\n userId: string\n email: string\n exp: number\n}\n`.trimStart()\n}\n","/** Generate wrangler.toml for a Cloudflare Worker with R2 + KV bindings */\nexport function wranglerTemplate(appName: string): string {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n return `name = \"${name}-worker\"\nmain = \"src/index.ts\"\ncompatibility_date = \"2024-01-01\"\ncompatibility_flags = [\"nodejs_compat\"]\n\n[[r2_buckets]]\nbinding = \"STORAGE\"\nbucket_name = \"${name}-storage\"\n\n[[kv_namespaces]]\nbinding = \"KV\"\nid = \"REPLACE_WITH_KV_NAMESPACE_ID\"\n\n[[durable_objects.bindings]]\nname = \"WS_SESSIONS\"\nclass_name = \"WsSessions\"\n\n[[migrations]]\ntag = \"v1\"\nnew_classes = [\"WsSessions\"]\n\n# Security: Set secrets via Cloudflare dashboard or CLI:\n# wrangler secret put JWT_SECRET\n`\n}\n","import { mkdirSync, writeFileSync } from 'node:fs'\nimport { join } from 'node:path'\nimport { workerIndexTemplate } from './templates/index.js'\nimport { authTemplate } from './templates/auth.js'\nimport { syncTemplate } from './templates/sync.js'\nimport { wsTemplate } from './templates/ws.js'\nimport { errorsTemplate } from './templates/errors.js'\nimport { healthTemplate } from './templates/health.js'\nimport { r2StorageTemplate } from './templates/r2-storage.js'\nimport { kvStorageTemplate } from './templates/kv-storage.js'\nimport { wranglerTemplate } from './templates/wrangler.js'\n\ninterface GeneratorOptions {\n appName: string\n outDir: string\n}\n\n/**\n * Generates a complete Hono Cloudflare Worker from the app schema.\n * Writes all source files to outDir.\n */\nexport function generateWorker(options: GeneratorOptions): void {\n const { appName, outDir } = options\n\n // Create directory tree\n const dirs = [\n outDir,\n join(outDir, 'src'),\n join(outDir, 'src', 'routes'),\n join(outDir, 'src', 'storage'),\n ]\n for (const dir of dirs) mkdirSync(dir, { recursive: true })\n\n const files: [string, string][] = [\n [join(outDir, 'wrangler.toml'), wranglerTemplate(appName)],\n [join(outDir, 'src', 'index.ts'), workerIndexTemplate(appName)],\n [join(outDir, 'src', 'routes', 'auth.ts'), authTemplate()],\n [join(outDir, 'src', 'routes', 'sync.ts'), syncTemplate()],\n [join(outDir, 'src', 'routes', 'ws.ts'), wsTemplate()],\n [join(outDir, 'src', 'routes', 'errors.ts'), errorsTemplate()],\n [join(outDir, 'src', 'routes', 'health.ts'), healthTemplate()],\n [join(outDir, 'src', 'storage', 'r2.ts'), r2StorageTemplate()],\n [join(outDir, 'src', 'storage', 'kv.ts'), kvStorageTemplate()],\n [join(outDir, 'package.json'), workerPackageJson(appName)],\n [join(outDir, 'tsconfig.json'), workerTsConfig()],\n ]\n\n for (const [path, content] of files) {\n writeFileSync(path, content, 'utf8')\n }\n\n console.warn(\n '\\n⚠ Remember to replace placeholder values in wrangler.toml before deploying:\\n' +\n ' - JWT_SECRET = \"REPLACE_WITH_SECRET\" → set a strong random secret\\n' +\n ' - KV namespace id → set your actual KV namespace ID\\n'\n )\n}\n\nfunction workerPackageJson(appName: string): string {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n return JSON.stringify(\n {\n name: `${name}-worker`,\n version: '1.0.0',\n type: 'module',\n scripts: {\n dev: 'wrangler dev',\n deploy: 'wrangler deploy',\n 'type-check': 'tsc --noEmit',\n },\n dependencies: {\n hono: '^4.6.0',\n },\n devDependencies: {\n '@cloudflare/workers-types': '^4.0.0',\n typescript: '^5.6.0',\n wrangler: '^3.0.0',\n },\n },\n null,\n 2,\n )\n}\n\nfunction workerTsConfig(): string {\n return JSON.stringify(\n {\n compilerOptions: {\n target: 'ES2022',\n module: 'ESNext',\n moduleResolution: 'bundler',\n lib: ['ES2022'],\n types: ['@cloudflare/workers-types'],\n strict: true,\n skipLibCheck: true,\n },\n include: ['src/**/*'],\n },\n null,\n 2,\n )\n}\n","import { generateWorker } from '../generator/index.js'\nimport type { DeployTarget } from './types.js'\n\nexport const cloudflareTarget: DeployTarget = {\n name: 'cloudflare',\n generate({ appName, outDir }: { appName: string; outDir: string }): void {\n generateWorker({ appName, outDir })\n },\n}\n","import { mkdirSync, writeFileSync } from 'node:fs'\nimport { join } from 'node:path'\nimport { authTemplate } from '../generator/templates/auth.js'\nimport { syncTemplate } from '../generator/templates/sync.js'\nimport { wsTemplate } from '../generator/templates/ws.js'\nimport { errorsTemplate } from '../generator/templates/errors.js'\nimport { healthTemplate } from '../generator/templates/health.js'\nimport { r2StorageTemplate } from '../generator/templates/r2-storage.js'\nimport { kvStorageTemplate } from '../generator/templates/kv-storage.js'\nimport type { DeployTarget } from './types.js'\n\nexport const nodeTarget: DeployTarget = {\n name: 'node',\n generate({ appName, outDir }: { appName: string; outDir: string }): void {\n const dirs = [\n outDir,\n join(outDir, 'src'),\n join(outDir, 'src', 'routes'),\n join(outDir, 'src', 'storage'),\n ]\n for (const dir of dirs) mkdirSync(dir, { recursive: true })\n\n const serverEntry = nodeServerEntry(appName)\n\n const files: [string, string][] = [\n [join(outDir, 'src', 'index.ts'), serverEntry],\n [join(outDir, 'src', 'routes', 'auth.ts'), authTemplate()],\n [join(outDir, 'src', 'routes', 'sync.ts'), syncTemplate()],\n [join(outDir, 'src', 'routes', 'ws.ts'), wsTemplate()],\n [join(outDir, 'src', 'routes', 'errors.ts'), errorsTemplate()],\n [join(outDir, 'src', 'routes', 'health.ts'), healthTemplate()],\n [join(outDir, 'src', 'storage', 'r2.ts'), r2StorageTemplate()],\n [join(outDir, 'src', 'storage', 'kv.ts'), kvStorageTemplate()],\n [join(outDir, 'package.json'), nodePackageJson(appName)],\n [join(outDir, 'tsconfig.json'), nodeTsConfig()],\n ]\n\n for (const [path, content] of files) {\n writeFileSync(path, content, 'utf8')\n }\n },\n}\n\nfunction nodeServerEntry(appName: string): string {\n return `import { Hono } from 'hono'\nimport { serve } from '@hono/node-server'\nimport { createNodeWebSocket } from '@hono/node-ws'\nimport { verify } from 'hono/jwt'\nimport { auth } from './routes/auth.js'\nimport { sync } from './routes/sync.js'\nimport { health } from './routes/health.js'\n\nconst app = new Hono()\nconst { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app })\n\n// In-memory session store for WebSocket connections (mirrors DO sessions map)\nconst sessions = new Map<WebSocket, { userId: string }>()\n\napp.route('/auth', auth)\napp.route('/sync', sync)\napp.route('/health', health)\n\napp.get('/ws', upgradeWebSocket(() => ({\n onOpen(_event, ws) {\n // session added on auth message\n },\n async onMessage(event, ws) {\n let msg: { type: string; [k: string]: unknown }\n try {\n msg = JSON.parse(event.data.toString())\n } catch {\n ws.send(JSON.stringify({ type: 'error', message: 'Invalid JSON' }))\n return\n }\n\n const session = sessions.get(ws.raw as WebSocket)\n\n if (msg.type === 'auth') {\n try {\n const jwtSecret = process.env.JWT_SECRET\n if (!jwtSecret) throw new Error('JWT_SECRET not configured')\n const payload = await verify(msg.token as string, jwtSecret) as { sub: string }\n if (!payload.sub) throw new Error('Missing sub claim')\n sessions.set(ws.raw as WebSocket, { userId: payload.sub })\n ws.send(JSON.stringify({ type: 'auth', ok: true }))\n } catch {\n ws.send(JSON.stringify({ type: 'error', message: 'Invalid token' }))\n ws.close()\n }\n } else if (!session) {\n ws.send(JSON.stringify({ type: 'error', message: 'Not authenticated' }))\n } else if (msg.type === 'push') {\n ws.send(JSON.stringify({ type: 'push', ok: true }))\n } else if (msg.type === 'pull') {\n ws.send(JSON.stringify({ type: 'pull', changes: [] }))\n }\n },\n onClose(_event, ws) {\n sessions.delete(ws.raw as WebSocket)\n },\n})))\n\nconst port = parseInt(process.env.PORT ?? '3000', 10)\nconsole.log(\\`${appName} server running on http://localhost:\\${port}\\`)\n\nconst server = serve({ fetch: app.fetch, port })\ninjectWebSocket(server)\n`\n}\n\nfunction nodePackageJson(appName: string): string {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n return JSON.stringify(\n {\n name: `${name}-server`,\n version: '1.0.0',\n type: 'module',\n scripts: {\n dev: 'tsx watch src/index.ts',\n start: 'node dist/index.js',\n build: 'tsc',\n },\n dependencies: {\n hono: '^4.6.0',\n '@hono/node-server': '^1.13.0',\n '@hono/node-ws': '^1.0.0',\n },\n devDependencies: {\n typescript: '^5.6.0',\n tsx: '^4.0.0',\n '@types/node': '^22.0.0',\n },\n },\n null,\n 2,\n )\n}\n\nfunction nodeTsConfig(): string {\n return JSON.stringify(\n {\n compilerOptions: {\n target: 'ES2022',\n module: 'ESNext',\n moduleResolution: 'bundler',\n lib: ['ES2022'],\n types: ['node'],\n strict: true,\n skipLibCheck: true,\n outDir: 'dist',\n },\n include: ['src/**/*'],\n },\n null,\n 2,\n )\n}\n","import type { Command } from 'commander'\nimport { resolve } from 'node:path'\nimport { cloudflareTarget } from '../targets/cloudflare.js'\nimport { nodeTarget } from '../targets/node.js'\nimport { findSchemaFile, loadSchema, log } from '../utils.js'\n\nconst targets = { cloudflare: cloudflareTarget, node: nodeTarget }\n\nexport function registerBuild(program: Command): void {\n program\n .command('build')\n .description('Build the LocalKit backend bundle')\n .option('-o, --out <dir>', 'Output directory', '.offlinekit/worker')\n .option('-n, --name <name>', 'App name for the worker', 'localkit-app')\n .option('-t, --target <target>', 'Deploy target: cloudflare or node', 'cloudflare')\n .action(async (opts: { out: string; name: string; target: string }) => {\n log.bold('LocalKit Build')\n\n const target = targets[opts.target as keyof typeof targets]\n if (!target) {\n log.error(`Unknown target: ${opts.target}. Valid targets: ${Object.keys(targets).join(', ')}`)\n process.exit(1)\n }\n\n const schemaFile = findSchemaFile()\n if (!schemaFile) {\n log.error('No schema file found. Create offlinekit.config.ts in your project root.')\n process.exit(1)\n }\n\n log.info(`Found schema: ${schemaFile}`)\n\n try {\n await loadSchema(schemaFile)\n log.success('Schema loaded successfully')\n } catch (err) {\n log.error(`Schema load failed: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n\n const outDir = resolve(process.cwd(), opts.out)\n log.info(`Generating ${target.name} backend to: ${outDir}`)\n\n try {\n target.generate({ appName: opts.name, outDir })\n log.success(`Backend generated at ${outDir}`)\n if (opts.target === 'node') {\n log.dim(`Run: cd ${opts.out} && npm install && npm run dev`)\n } else {\n log.dim(`Run: cd ${opts.out} && npm install && wrangler deploy`)\n }\n } catch (err) {\n log.error(`Build failed: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n })\n}\n","import type { Command } from 'commander'\nimport { execSync, spawnSync } from 'node:child_process'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { log } from '../utils.js'\n\nfunction checkWrangler(): string | null {\n try {\n const result = execSync('wrangler --version', { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] })\n return result.trim()\n } catch {\n return null\n }\n}\n\nexport function registerDeploy(program: Command): void {\n program\n .command('deploy')\n .description('Deploy the LocalKit Worker to Cloudflare via Wrangler')\n .option('-d, --dir <dir>', 'Worker output directory', '.offlinekit/worker')\n .option('-e, --env <env>', 'Wrangler environment (e.g. production, staging)')\n .option('-t, --target <target>', 'Deploy target (default: cloudflare)', 'cloudflare')\n .option('--dry-run', 'Print the wrangler command without running it')\n .action((opts: { dir: string; env?: string; target: string; dryRun?: boolean }) => {\n log.bold('LocalKit Deploy')\n\n // 0. Check if project has been ejected\n const serverDir = resolve(process.cwd(), 'server')\n if (existsSync(serverDir)) {\n log.info('Warning: This project has been ejected (./server/ exists).')\n log.dim('You can deploy the ejected code directly with `cd server && wrangler deploy`.')\n }\n\n // 0b. Validate target\n if (opts.target !== 'cloudflare') {\n log.error(`Deploy target \"${opts.target}\" is not supported yet. Only \"cloudflare\" is deployable.`)\n log.dim('For Node.js targets, run `npx mpb-localkit build --target node` then deploy the output manually.')\n process.exit(1)\n }\n\n // 1. Check wrangler\n const version = checkWrangler()\n if (!version) {\n log.error('wrangler is not installed or not in PATH.')\n log.dim('Install it with: npm install -g wrangler')\n process.exit(1)\n }\n log.success(`wrangler ${version}`)\n\n // 2. Check output directory exists\n const outDir = resolve(process.cwd(), opts.dir)\n if (!existsSync(outDir)) {\n log.error(`Worker directory not found: ${outDir}`)\n log.dim('Run `npx mpb-localkit build` first to generate the worker.')\n process.exit(1)\n }\n\n // 3. Check wrangler.toml is present\n const tomlPath = resolve(outDir, 'wrangler.toml')\n if (!existsSync(tomlPath)) {\n log.error(`wrangler.toml not found in ${outDir}`)\n log.dim('Run `npx mpb-localkit build` to regenerate the worker.')\n process.exit(1)\n }\n\n // 4. Build wrangler deploy command\n const args = ['deploy']\n if (opts.env) args.push('--env', opts.env)\n\n log.info(`Deploying from: ${outDir}`)\n if (opts.env) log.info(`Environment: ${opts.env}`)\n\n if (opts.dryRun) {\n log.dim(`[dry-run] wrangler ${args.join(' ')}`)\n log.dim(`[dry-run] cwd: ${outDir}`)\n return\n }\n\n // 5. Run wrangler deploy\n const result = spawnSync('wrangler', args, {\n cwd: outDir,\n stdio: 'inherit',\n encoding: 'utf8',\n })\n\n if (result.status !== 0) {\n log.error(`wrangler deploy failed (exit ${result.status ?? 'unknown'})`)\n process.exit(result.status ?? 1)\n }\n\n log.success('Deployed successfully!')\n })\n}\n","import type { Command } from 'commander'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { cloudflareTarget } from '../targets/cloudflare.js'\nimport { nodeTarget } from '../targets/node.js'\nimport type { DeployTarget } from '../targets/types.js'\nimport { log } from '../utils.js'\n\nconst targets: Record<string, DeployTarget> = {\n cloudflare: cloudflareTarget,\n node: nodeTarget,\n}\n\nexport function registerEject(program: Command): void {\n program\n .command('eject')\n .description('Eject generated Worker code into ./server/ for manual customization')\n .option('-o, --out <dir>', 'Output directory for ejected code', 'server')\n .option('-n, --name <name>', 'App name', 'localkit-app')\n .option('-t, --target <target>', 'Deploy target (cloudflare, node)', 'cloudflare')\n .action((opts: { out: string; name: string; target: string }) => {\n log.bold('LocalKit Eject')\n\n const target = targets[opts.target]\n if (!target) {\n log.error(`Unknown target: \"${opts.target}\"`)\n log.dim(`Available targets: ${Object.keys(targets).join(', ')}`)\n process.exit(1)\n }\n\n const outDir = resolve(process.cwd(), opts.out)\n\n if (existsSync(outDir)) {\n log.error(`Directory already exists: ${outDir}`)\n log.dim('Remove it or choose a different output directory with --out.')\n process.exit(1)\n }\n\n log.info(`Ejecting ${opts.target} code to: ${outDir}`)\n\n try {\n target.generate({ appName: opts.name, outDir })\n log.success(`Ejected to ./${opts.out}/`)\n\n if (opts.target === 'node') {\n log.dim('You can now customize and deploy your Node.js server.')\n log.dim('')\n log.dim('Next steps:')\n log.dim(` cd ${opts.out}`)\n log.dim(' npm install')\n log.dim(' npm run dev')\n log.dim('')\n log.dim('To run in production: npm start')\n } else {\n log.dim('You can now customize and deploy with `wrangler deploy`.')\n log.dim('')\n log.dim('Next steps:')\n log.dim(` cd ${opts.out}`)\n log.dim(' npm install')\n log.dim(' wrangler deploy')\n log.dim('')\n log.dim('Note: Running `npx mpb-localkit deploy` will warn that code has been ejected.')\n }\n } catch (err) {\n log.error(`Eject failed: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n })\n}\n","#!/usr/bin/env node\nimport { Command } from 'commander'\nimport { createRequire } from 'node:module'\nimport { registerDev } from './commands/dev.js'\nimport { registerBuild } from './commands/build.js'\nimport { registerDeploy } from './commands/deploy.js'\nimport { registerEject } from './commands/eject.js'\n\nconst require = createRequire(import.meta.url)\nconst pkg = require('../../package.json') as { version: string; description: string }\n\nconst program = new Command()\n\nprogram\n .name('mpb-localkit')\n .description(pkg.description)\n .version(pkg.version, '-v, --version', 'Print the current version')\n\nregisterDev(program)\nregisterBuild(program)\nregisterDeploy(program)\nregisterEject(program)\n\nprogram.parse(process.argv)\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/utils.ts","../../src/cli/commands/dev.ts","../../src/cli/generator/templates/index.ts","../../src/cli/generator/templates/auth.ts","../../src/cli/generator/templates/sync.ts","../../src/cli/generator/templates/ws.ts","../../src/cli/generator/templates/errors.ts","../../src/cli/generator/templates/health.ts","../../src/cli/generator/templates/r2-storage.ts","../../src/cli/generator/templates/kv-storage.ts","../../src/cli/generator/templates/wrangler.ts","../../src/cli/generator/index.ts","../../src/cli/targets/cloudflare.ts","../../src/cli/targets/node.ts","../../src/cli/commands/build.ts","../../src/cli/commands/deploy.ts","../../src/cli/commands/eject.ts","../../src/cli/index.ts"],"names":["program","join","mkdirSync","writeFileSync","resolve","existsSync","targets","require"],"mappings":";;;;;;;;AAIA,IAAM,MAAA,GAAS;AAAA,EACb,KAAA,EAAO,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EAClC,MAAA,EAAQ,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EACnC,GAAA,EAAK,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EAChC,IAAA,EAAM,CAAC,CAAA,KAAc,CAAA,QAAA,EAAW,CAAC,CAAA,OAAA,CAAA;AAAA,EACjC,IAAA,EAAM,CAAC,CAAA,KAAc,CAAA,OAAA,EAAU,CAAC,CAAA,OAAA,CAAA;AAAA,EAChC,GAAA,EAAK,CAAC,CAAA,KAAc,CAAA,OAAA,EAAU,CAAC,CAAA,OAAA;AACjC,CAAA;AAEO,IAAM,GAAA,GAAM;AAAA,EACjB,IAAA,EAAM,CAAC,GAAA,KAAgB,OAAA,CAAQ,GAAA,CAAI,CAAA,EAAG,MAAA,CAAO,IAAA,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AA;AACV,QAAA,GAAA,CAAI,IAAI,aAAa,CAAA;AACrB,QAAA,GAAA,CAAI,GAAA,CAAI,CAAA,KAAA,EAAQ,IAAA,CAAK,GAAG,CAAA,CAAE,CAAA;AAC1B,QAAA,GAAA,CAAI,IAAI,eAAe,CAAA;AACvB,QAAA,GAAA,CAAI,IAAI,mBAAmB,CAAA;AAC3B,QAAA,GAAA,CAAI,IAAI,EAAE,CAAA;AACV,QAAA,GAAA,CAAI,IAAI,+EAA+E,CAAA;AAAA,MACzF;AAAA,IACF,SAAS,GAAA,EAAK;AACZ,MAAA,GAAA,CAAI,KAAA,CAAM,iBAAiB,GAAA,YAAe,KAAA,GAAQ,IAAI,OAAA,GAAU,MAAA,CAAO,GAAG,CAAC,CAAA,CAAE,CAAA;AAC7E,MAAA,OAAA,CAAQ,KAAK,CAAC,CAAA;AAAA,IAChB;AAAA,EACF,CAAC,CAAA;AACL;;;AC5DA,IAAME,QAAAA,GAAU,aAAA,CAAc,MAAA,CAAA,IAAA,CAAY,GAAG,CAAA;AAC7C,IAAM,GAAA,GAAMA,SAAQ,oBAAoB,CAAA;AAExC,IAAM,OAAA,GAAU,IAAI,OAAA,EAAQ;AAE5B,OAAA,CACG,IAAA,CAAK,cAAc,CAAA,CACnB,WAAA,CAAY,GAAA,CAAI,WAAW,CAAA,CAC3B,OAAA,CAAQ,GAAA,CAAI,OAAA,EAAS,eAAA,EAAiB,2BAA2B,CAAA;AAEpE,WAAA,CAAY,OAAO,CAAA;AACnB,aAAA,CAAc,OAAO,CAAA;AACrB,cAAA,CAAe,OAAO,CAAA;AACtB,aAAA,CAAc,OAAO,CAAA;AAErB,OAAA,CAAQ,KAAA,CAAM,QAAQ,IAAI,CAAA","file":"index.js","sourcesContent":["import { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\n/** Color helpers for terminal output (no dependencies) */\nconst colors = {\n green: (s: string) => `\\x1b[32m${s}\\x1b[0m`,\n yellow: (s: string) => `\\x1b[33m${s}\\x1b[0m`,\n red: (s: string) => `\\x1b[31m${s}\\x1b[0m`,\n cyan: (s: string) => `\\x1b[36m${s}\\x1b[0m`,\n bold: (s: string) => `\\x1b[1m${s}\\x1b[0m`,\n dim: (s: string) => `\\x1b[2m${s}\\x1b[0m`,\n}\n\nexport const log = {\n info: (msg: string) => console.log(`${colors.cyan('ℹ')} ${msg}`),\n success: (msg: string) => console.log(`${colors.green('✓')} ${msg}`),\n warn: (msg: string) => console.warn(`${colors.yellow('⚠')} ${msg}`),\n error: (msg: string) => console.error(`${colors.red('✗')} ${msg}`),\n bold: (msg: string) => console.log(colors.bold(msg)),\n dim: (msg: string) => console.log(colors.dim(msg)),\n}\n\nconst CONFIG_CANDIDATES = [\n 'offlinekit.config.ts',\n 'offlinekit.config.js',\n 'offlinekit.schema.ts',\n 'offlinekit.schema.js',\n]\n\n/** Finds the OfflineKit config/schema file in the current working directory */\nexport function findSchemaFile(cwd = process.cwd()): string | null {\n for (const candidate of CONFIG_CANDIDATES) {\n const p = resolve(cwd, candidate)\n if (existsSync(p)) return p\n }\n return null\n}\n\n/** Dynamically imports and returns the default export from the schema file */\nexport async function loadSchema(filePath: string): Promise<unknown> {\n const mod = await import(filePath) as { default?: unknown }\n return mod.default ?? mod\n}\n","import type { Command } from 'commander'\nimport { log } from '../utils.js'\n\nexport function registerDev(program: Command): void {\n program\n .command('dev')\n .description('Start LocalKit in local-only mode (no cloud sync)')\n .action(() => {\n process.env['OFFLINEKIT_MODE'] = 'local'\n log.bold('LocalKit Dev Mode')\n log.info('Running in local-only mode (no cloud sync)')\n log.dim('All data is stored locally. Sync is disabled.')\n })\n}\n","/** Main Hono app entry point template */\nexport function workerIndexTemplate(_appName: string): string {\n return `\nimport { Hono } from 'hono'\nimport { cors } from 'hono/cors'\nimport { auth } from './routes/auth.js'\nimport { sync } from './routes/sync.js'\nimport { ws, WsSessions } from './routes/ws.js'\nimport { errors } from './routes/errors.js'\nimport { health } from './routes/health.js'\n\nexport interface Env {\n STORAGE: R2Bucket\n KV: KVNamespace\n JWT_SECRET: string\n WS_SESSIONS: DurableObjectNamespace\n}\n\nexport { WsSessions }\n\nconst app = new Hono<{ Bindings: Env }>()\n\napp.use('/*', cors({\n origin: '*',\n allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],\n allowHeaders: ['Content-Type', 'Authorization'],\n}))\n\napp.route('/auth', auth)\napp.route('/sync', sync)\napp.route('/ws', ws)\napp.route('/errors', errors)\napp.route('/health', health)\n\nexport default app\n`.trimStart()\n}\n","/** Auth routes: POST /auth/signup, /auth/signin, /auth/signout */\nexport function authTemplate(): string {\n return `\nimport { Hono } from 'hono'\nimport { sign, verify } from 'hono/jwt'\nimport type { Env } from './index.js'\nimport { AUTH_KEY, SESSION_KEY, type StoredUser } from '../storage/kv.js'\n\nexport const auth = new Hono<{ Bindings: Env }>()\n\n/** Constant-time string comparison to prevent timing side-channels */\nfunction timingSafeEqual(a: string, b: string): boolean {\n if (a.length !== b.length) return false\n const encoder = new TextEncoder()\n const bufA = encoder.encode(a)\n const bufB = encoder.encode(b)\n let result = 0\n for (let i = 0; i < bufA.length; i++) result |= bufA[i] ^ bufB[i]\n return result === 0\n}\n\n/** Extract userId from Bearer token — throws on invalid/expired */\nexport async function authMiddleware(c: any, next: () => Promise<void>) {\n const header = c.req.header('Authorization') ?? ''\n const token = header.startsWith('Bearer ') ? header.slice(7) : null\n if (!token) return c.json({ error: 'Unauthorized' }, 401)\n try {\n const payload = await verify(token, c.env.JWT_SECRET, 'HS256') as { sub: string }\n c.set('userId', payload.sub)\n } catch {\n return c.json({ error: 'Invalid token' }, 401)\n }\n return next()\n}\n\nauth.post('/signup', async (c) => {\n const { email, passwordHash } = await c.req.json<{ email: string; passwordHash: string }>()\n if (!email || !passwordHash) return c.json({ error: 'email and passwordHash required' }, 400)\n\n const existing = await c.env.KV.get(AUTH_KEY(email))\n if (existing) return c.json({ error: 'Email already registered' }, 409)\n\n const userId = crypto.randomUUID()\n const user: StoredUser = { userId, email, passwordHash }\n await c.env.KV.put(AUTH_KEY(email), JSON.stringify(user))\n\n const token = await sign({ sub: userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET, 'HS256')\n return c.json({ user: { id: userId, email }, token }, 201)\n})\n\nauth.post('/signin', async (c) => {\n const { email, passwordHash } = await c.req.json<{ email: string; passwordHash: string }>()\n if (!email || !passwordHash) return c.json({ error: 'email and passwordHash required' }, 400)\n\n const raw = await c.env.KV.get(AUTH_KEY(email))\n if (!raw) return c.json({ error: 'Invalid credentials' }, 401)\n\n const user = JSON.parse(raw) as StoredUser\n if (!timingSafeEqual(user.passwordHash, passwordHash)) return c.json({ error: 'Invalid credentials' }, 401)\n\n const token = await sign({ sub: user.userId, email, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30 }, c.env.JWT_SECRET, 'HS256')\n return c.json({ user: { id: user.userId, email }, token })\n})\n\nauth.post('/signout', async (c) => {\n const header = c.req.header('Authorization') ?? ''\n const token = header.startsWith('Bearer ') ? header.slice(7) : null\n if (token) await c.env.KV.delete(SESSION_KEY(token))\n return c.json({ ok: true })\n})\n`.trimStart()\n}\n","/** Sync routes: POST /sync/push, GET /sync/pull?since= */\nexport function syncTemplate(): string {\n return `\nimport { Hono } from 'hono'\nimport type { Env } from './index.js'\nimport { authMiddleware } from './auth.js'\nimport { docKey, collectionPrefix } from '../storage/r2.js'\n\ninterface SyncDoc {\n _id: string\n _collection: string\n _updatedAt: number\n _deleted: boolean\n [key: string]: unknown\n}\n\nexport const sync = new Hono<{ Bindings: Env }>()\n\nsync.use('/*', authMiddleware)\n\nsync.post('/push', async (c) => {\n const userId = c.get('userId') as string\n const { changes } = await c.req.json<{ changes: SyncDoc[] }>()\n\n await Promise.all(\n changes.map(async (doc) => {\n const key = docKey(userId, doc._collection, doc._id)\n const existing = await c.env.STORAGE.get(key, 'json') as SyncDoc | null\n\n // LWW — only overwrite if incoming is newer\n if (!existing || doc._updatedAt >= existing._updatedAt) {\n await c.env.STORAGE.put(key, JSON.stringify(doc), {\n customMetadata: { updatedAt: String(doc._updatedAt) },\n })\n }\n }),\n )\n\n return c.json({ ok: true, received: changes.length })\n})\n\nsync.get('/pull', async (c) => {\n const userId = c.get('userId') as string\n const since = parseInt(c.req.query('since') ?? '0', 10)\n\n // Scan all collections for this user — list all objects under {userId}/\n const prefix = \\`\\${userId}/\\`\n const listed = await c.env.STORAGE.list({ prefix })\n\n const changes = await Promise.all(\n listed.objects\n .filter((obj) => {\n const ts = parseInt(obj.customMetadata?.updatedAt ?? '0', 10)\n return ts > since\n })\n .map(async (obj) => {\n const blob = await c.env.STORAGE.get(obj.key, 'json')\n return blob as SyncDoc\n }),\n )\n\n return c.json({ changes: changes.filter(Boolean) })\n})\n`.trimStart()\n}\n","/** WebSocket Durable Object handler for real-time sync */\nexport function wsTemplate(): string {\n return `import { Hono } from 'hono'\nimport { verify } from 'hono/jwt'\nimport type { Env } from './index.js'\nimport { authMiddleware } from './auth.js'\nimport { docKey } from '../storage/r2.js'\n\nexport const ws = new Hono<{ Bindings: Env }>()\n\n// Proxy WS upgrade requests to the user-scoped Durable Object\nws.get('/', authMiddleware, async (c) => {\n const userId = c.get('userId') as string\n const id = c.env.WS_SESSIONS.idFromName(userId)\n return c.env.WS_SESSIONS.get(id).fetch(c.req.raw)\n})\n\n// ---- Durable Object: one instance per userId, owns all WebSocket sessions ----\n\ninterface SyncDoc {\n _id: string\n _collection: string\n _updatedAt: number\n _deleted: boolean\n [key: string]: unknown\n}\n\ninterface WsMsg {\n type: string\n id?: string\n payload?: unknown\n since?: number\n token?: string\n}\n\nexport class WsSessions {\n private readonly sessions = new Map<WebSocket, { userId: string | null }>()\n\n constructor(\n private readonly state: DurableObjectState,\n private readonly env: Env,\n ) {}\n\n async fetch(request: Request): Promise<Response> {\n if (request.headers.get('Upgrade') !== 'websocket') {\n return new Response('Expected WebSocket upgrade', { status: 426 })\n }\n\n const pair = new WebSocketPair()\n const [client, server] = Object.values(pair) as [WebSocket, WebSocket]\n server.accept()\n\n const session = { userId: null as string | null }\n this.sessions.set(server, session)\n\n server.addEventListener('message', async (event) => {\n let msg: WsMsg\n try { msg = JSON.parse(event.data as string) as WsMsg } catch { return }\n try {\n await this.handle(server, session, msg)\n } catch (err) {\n server.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: String(err) } }))\n }\n })\n\n server.addEventListener('close', () => { this.sessions.delete(server) })\n\n return new Response(null, { status: 101, webSocket: client })\n }\n\n private async handle(ws: WebSocket, session: { userId: string | null }, msg: WsMsg): Promise<void> {\n if (msg.type === 'auth') {\n try {\n const payload = await verify(msg.token ?? '', this.env.JWT_SECRET, 'HS256') as { sub?: string }\n if (!payload.sub) throw new Error('Missing sub')\n session.userId = payload.sub\n ws.send(JSON.stringify({ type: 'auth_ack', id: msg.id }))\n } catch {\n ws.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: 'Unauthorized' } }))\n ws.close(1008, 'Unauthorized')\n }\n return\n }\n\n if (!session.userId) {\n ws.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: 'Not authenticated' } }))\n return\n }\n\n if (msg.type === 'push') {\n const { changes } = msg.payload as { changes: SyncDoc[] }\n await Promise.all(changes.map(async (doc) => {\n const key = docKey(session.userId!, doc._collection, doc._id)\n const existing = await this.env.STORAGE.get(key, 'json') as SyncDoc | null\n if (!existing || doc._updatedAt >= existing._updatedAt) {\n await this.env.STORAGE.put(key, JSON.stringify(doc), {\n customMetadata: { updatedAt: String(doc._updatedAt) },\n })\n }\n }))\n ws.send(JSON.stringify({ type: 'push_ack', id: msg.id, payload: { ok: true } }))\n this.broadcast(session.userId, ws, { type: 'remote_changes', changes })\n return\n }\n\n if (msg.type === 'pull') {\n const since = typeof msg.since === 'number' ? msg.since : 0\n const listed = await this.env.STORAGE.list({ prefix: \\`\\${session.userId}/\\` })\n const changes = (await Promise.all(\n listed.objects\n .filter((o) => parseInt(o.customMetadata?.updatedAt ?? '0', 10) > since)\n .map((o) => this.env.STORAGE.get(o.key, 'json') as Promise<SyncDoc>),\n )).filter(Boolean)\n ws.send(JSON.stringify({ type: 'pull_response', id: msg.id, payload: { changes } }))\n return\n }\n\n ws.send(JSON.stringify({ type: 'error', id: msg.id, payload: { message: \\`Unknown type: \\${msg.type}\\` } }))\n }\n\n private broadcast(userId: string, sender: WebSocket, msg: object): void {\n const data = JSON.stringify(msg)\n for (const [ws, s] of this.sessions) {\n if (ws !== sender && s.userId === userId) {\n try { ws.send(data) } catch { /* stale connection */ }\n }\n }\n }\n}\n\n// JWT verification handled by hono/jwt verify() — no hand-rolled crypto needed\n`.trimStart()\n}\n","/** GET /errors — list error blobs for authenticated user */\nexport function errorsTemplate(): string {\n return `\nimport { Hono } from 'hono'\nimport type { Env } from './index.js'\nimport { authMiddleware } from './auth.js'\n\nexport const errors = new Hono<{ Bindings: Env }>()\n\nerrors.use('/*', authMiddleware)\n\nerrors.get('/', async (c) => {\n const userId = c.get('userId') as string\n const prefix = \\`\\${userId}/_errors/\\`\n\n const listed = await c.env.STORAGE.list({ prefix })\n const results = await Promise.all(\n listed.objects.map(async (obj) => {\n const blob = await c.env.STORAGE.get(obj.key)\n if (!blob) return null\n return blob.json()\n }),\n )\n\n return c.json({ errors: results.filter(Boolean) })\n})\n`.trimStart()\n}\n","/** GET /health — liveness check */\nexport function healthTemplate(): string {\n return `\nimport { Hono } from 'hono'\n\nexport const health = new Hono()\n\nhealth.get('/', (c) => {\n return c.json({ status: 'ok', timestamp: Date.now() })\n})\n`.trimStart()\n}\n","/** R2 document key helpers — {userId}/{collection}/{docId}.json */\nexport function r2StorageTemplate(): string {\n return `\nexport const docKey = (userId: string, collection: string, docId: string) =>\n \\`\\${userId}/\\${collection}/\\${docId}.json\\`\n\nexport const collectionPrefix = (userId: string, collection: string) =>\n \\`\\${userId}/\\${collection}/\\`\n`.trimStart()\n}\n","/** KV storage helpers — sessions, auth credentials, metadata */\nexport function kvStorageTemplate(): string {\n return `\n/** Key: auth:{email} Value: { userId, email, passwordHash } */\nexport const AUTH_KEY = (email: string) => \\`auth:\\${email}\\`\n\n/** Key: session:{token} Value: { userId, email, exp } */\nexport const SESSION_KEY = (token: string) => \\`session:\\${token}\\`\n\n/** Key: meta:{userId} Value: arbitrary metadata object */\nexport const META_KEY = (userId: string) => \\`meta:\\${userId}\\`\n\nexport interface StoredUser {\n userId: string\n email: string\n passwordHash: string\n}\n\nexport interface StoredSession {\n userId: string\n email: string\n exp: number\n}\n`.trimStart()\n}\n","/** Generate wrangler.toml for a Cloudflare Worker with R2 + KV bindings */\nexport function wranglerTemplate(appName: string, kvNamespaceId?: string): string {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n return `name = \"${name}-worker\"\nmain = \"src/index.ts\"\ncompatibility_date = \"2024-01-01\"\ncompatibility_flags = [\"nodejs_compat\"]\n\n[[r2_buckets]]\nbinding = \"STORAGE\"\nbucket_name = \"${name}-storage\"\n\n[[kv_namespaces]]\nbinding = \"KV\"\nid = \"${kvNamespaceId ?? 'REPLACE_WITH_KV_NAMESPACE_ID'}\"\n\n[[durable_objects.bindings]]\nname = \"WS_SESSIONS\"\nclass_name = \"WsSessions\"\n\n[[migrations]]\ntag = \"v1\"\nnew_sqlite_classes = [\"WsSessions\"]\n\n# Security: Set secrets via Cloudflare dashboard or CLI:\n# wrangler secret put JWT_SECRET\n`\n}\n","import { mkdirSync, writeFileSync } from 'node:fs'\nimport { join } from 'node:path'\nimport { workerIndexTemplate } from './templates/index.js'\nimport { authTemplate } from './templates/auth.js'\nimport { syncTemplate } from './templates/sync.js'\nimport { wsTemplate } from './templates/ws.js'\nimport { errorsTemplate } from './templates/errors.js'\nimport { healthTemplate } from './templates/health.js'\nimport { r2StorageTemplate } from './templates/r2-storage.js'\nimport { kvStorageTemplate } from './templates/kv-storage.js'\nimport { wranglerTemplate } from './templates/wrangler.js'\n\ninterface GeneratorOptions {\n appName: string\n outDir: string\n kvNamespaceId?: string\n}\n\n/**\n * Generates a complete Hono Cloudflare Worker from the app schema.\n * Writes all source files to outDir.\n */\nexport function generateWorker(options: GeneratorOptions): void {\n const { appName, outDir, kvNamespaceId } = options\n\n // Create directory tree\n const dirs = [\n outDir,\n join(outDir, 'src'),\n join(outDir, 'src', 'routes'),\n join(outDir, 'src', 'storage'),\n ]\n for (const dir of dirs) mkdirSync(dir, { recursive: true })\n\n const files: [string, string][] = [\n [join(outDir, 'wrangler.toml'), wranglerTemplate(appName, kvNamespaceId)],\n [join(outDir, 'src', 'index.ts'), workerIndexTemplate(appName)],\n [join(outDir, 'src', 'routes', 'auth.ts'), authTemplate()],\n [join(outDir, 'src', 'routes', 'sync.ts'), syncTemplate()],\n [join(outDir, 'src', 'routes', 'ws.ts'), wsTemplate()],\n [join(outDir, 'src', 'routes', 'errors.ts'), errorsTemplate()],\n [join(outDir, 'src', 'routes', 'health.ts'), healthTemplate()],\n [join(outDir, 'src', 'storage', 'r2.ts'), r2StorageTemplate()],\n [join(outDir, 'src', 'storage', 'kv.ts'), kvStorageTemplate()],\n [join(outDir, 'package.json'), workerPackageJson(appName)],\n [join(outDir, 'tsconfig.json'), workerTsConfig()],\n ]\n\n for (const [path, content] of files) {\n writeFileSync(path, content, 'utf8')\n }\n\n const kvWarning = kvNamespaceId\n ? ''\n : ' - KV namespace id → set your actual KV namespace ID\\n'\n console.warn(\n '\\n⚠ Remember to replace placeholder values in wrangler.toml before deploying:\\n' +\n ' - JWT_SECRET = \"REPLACE_WITH_SECRET\" → set a strong random secret\\n' +\n kvWarning\n )\n}\n\nfunction workerPackageJson(appName: string): string {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n return JSON.stringify(\n {\n name: `${name}-worker`,\n version: '1.0.0',\n type: 'module',\n scripts: {\n dev: 'wrangler dev',\n deploy: 'wrangler deploy',\n 'type-check': 'tsc --noEmit',\n },\n dependencies: {\n hono: '^4.6.0',\n },\n devDependencies: {\n '@cloudflare/workers-types': '^4.0.0',\n typescript: '^5.6.0',\n wrangler: '^3.0.0',\n },\n },\n null,\n 2,\n )\n}\n\nfunction workerTsConfig(): string {\n return JSON.stringify(\n {\n compilerOptions: {\n target: 'ES2022',\n module: 'ESNext',\n moduleResolution: 'bundler',\n lib: ['ES2022'],\n types: ['@cloudflare/workers-types'],\n strict: true,\n skipLibCheck: true,\n },\n include: ['src/**/*'],\n },\n null,\n 2,\n )\n}\n","import { generateWorker } from '../generator/index.js'\nimport type { DeployTarget } from './types.js'\n\nexport const cloudflareTarget: DeployTarget = {\n name: 'cloudflare',\n generate({ appName, outDir, kvNamespaceId }: { appName: string; outDir: string; kvNamespaceId?: string }): void {\n generateWorker({ appName, outDir, kvNamespaceId })\n },\n}\n","import { mkdirSync, writeFileSync } from 'node:fs'\nimport { join } from 'node:path'\nimport { authTemplate } from '../generator/templates/auth.js'\nimport { syncTemplate } from '../generator/templates/sync.js'\nimport { wsTemplate } from '../generator/templates/ws.js'\nimport { errorsTemplate } from '../generator/templates/errors.js'\nimport { healthTemplate } from '../generator/templates/health.js'\nimport { r2StorageTemplate } from '../generator/templates/r2-storage.js'\nimport { kvStorageTemplate } from '../generator/templates/kv-storage.js'\nimport type { DeployTarget } from './types.js'\n\nexport const nodeTarget: DeployTarget = {\n name: 'node',\n generate({ appName, outDir }: { appName: string; outDir: string }): void {\n const dirs = [\n outDir,\n join(outDir, 'src'),\n join(outDir, 'src', 'routes'),\n join(outDir, 'src', 'storage'),\n ]\n for (const dir of dirs) mkdirSync(dir, { recursive: true })\n\n const serverEntry = nodeServerEntry(appName)\n\n const files: [string, string][] = [\n [join(outDir, 'src', 'index.ts'), serverEntry],\n [join(outDir, 'src', 'routes', 'auth.ts'), authTemplate()],\n [join(outDir, 'src', 'routes', 'sync.ts'), syncTemplate()],\n [join(outDir, 'src', 'routes', 'ws.ts'), wsTemplate()],\n [join(outDir, 'src', 'routes', 'errors.ts'), errorsTemplate()],\n [join(outDir, 'src', 'routes', 'health.ts'), healthTemplate()],\n [join(outDir, 'src', 'storage', 'r2.ts'), r2StorageTemplate()],\n [join(outDir, 'src', 'storage', 'kv.ts'), kvStorageTemplate()],\n [join(outDir, 'package.json'), nodePackageJson(appName)],\n [join(outDir, 'tsconfig.json'), nodeTsConfig()],\n ]\n\n for (const [path, content] of files) {\n writeFileSync(path, content, 'utf8')\n }\n },\n}\n\nfunction nodeServerEntry(appName: string): string {\n return `import { Hono } from 'hono'\nimport { serve } from '@hono/node-server'\nimport { createNodeWebSocket } from '@hono/node-ws'\nimport { verify } from 'hono/jwt'\nimport { auth } from './routes/auth.js'\nimport { sync } from './routes/sync.js'\nimport { health } from './routes/health.js'\n\nconst app = new Hono()\nconst { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app })\n\n// In-memory session store for WebSocket connections (mirrors DO sessions map)\nconst sessions = new Map<WebSocket, { userId: string }>()\n\napp.route('/auth', auth)\napp.route('/sync', sync)\napp.route('/health', health)\n\napp.get('/ws', upgradeWebSocket(() => ({\n onOpen(_event, ws) {\n // session added on auth message\n },\n async onMessage(event, ws) {\n let msg: { type: string; [k: string]: unknown }\n try {\n msg = JSON.parse(event.data.toString())\n } catch {\n ws.send(JSON.stringify({ type: 'error', message: 'Invalid JSON' }))\n return\n }\n\n const session = sessions.get(ws.raw as WebSocket)\n\n if (msg.type === 'auth') {\n try {\n const jwtSecret = process.env.JWT_SECRET\n if (!jwtSecret) throw new Error('JWT_SECRET not configured')\n const payload = await verify(msg.token as string, jwtSecret) as { sub: string }\n if (!payload.sub) throw new Error('Missing sub claim')\n sessions.set(ws.raw as WebSocket, { userId: payload.sub })\n ws.send(JSON.stringify({ type: 'auth', ok: true }))\n } catch {\n ws.send(JSON.stringify({ type: 'error', message: 'Invalid token' }))\n ws.close()\n }\n } else if (!session) {\n ws.send(JSON.stringify({ type: 'error', message: 'Not authenticated' }))\n } else if (msg.type === 'push') {\n ws.send(JSON.stringify({ type: 'push', ok: true }))\n } else if (msg.type === 'pull') {\n ws.send(JSON.stringify({ type: 'pull', changes: [] }))\n }\n },\n onClose(_event, ws) {\n sessions.delete(ws.raw as WebSocket)\n },\n})))\n\nconst port = parseInt(process.env.PORT ?? '3000', 10)\nconsole.log(\\`${appName} server running on http://localhost:\\${port}\\`)\n\nconst server = serve({ fetch: app.fetch, port })\ninjectWebSocket(server)\n`\n}\n\nfunction nodePackageJson(appName: string): string {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n return JSON.stringify(\n {\n name: `${name}-server`,\n version: '1.0.0',\n type: 'module',\n scripts: {\n dev: 'tsx watch src/index.ts',\n start: 'node dist/index.js',\n build: 'tsc',\n },\n dependencies: {\n hono: '^4.6.0',\n '@hono/node-server': '^1.13.0',\n '@hono/node-ws': '^1.0.0',\n },\n devDependencies: {\n typescript: '^5.6.0',\n tsx: '^4.0.0',\n '@types/node': '^22.0.0',\n },\n },\n null,\n 2,\n )\n}\n\nfunction nodeTsConfig(): string {\n return JSON.stringify(\n {\n compilerOptions: {\n target: 'ES2022',\n module: 'ESNext',\n moduleResolution: 'bundler',\n lib: ['ES2022'],\n types: ['node'],\n strict: true,\n skipLibCheck: true,\n outDir: 'dist',\n },\n include: ['src/**/*'],\n },\n null,\n 2,\n )\n}\n","import type { Command } from 'commander'\nimport { resolve } from 'node:path'\nimport { cloudflareTarget } from '../targets/cloudflare.js'\nimport { nodeTarget } from '../targets/node.js'\nimport { log } from '../utils.js'\n\nconst targets = { cloudflare: cloudflareTarget, node: nodeTarget }\n\nexport function registerBuild(program: Command): void {\n program\n .command('build')\n .description('Build the LocalKit backend bundle')\n .option('-o, --out <dir>', 'Output directory', '.offlinekit/worker')\n .option('-n, --name <name>', 'App name for the worker', 'localkit-app')\n .option('-t, --target <target>', 'Deploy target: cloudflare or node', 'cloudflare')\n .action(async (opts: { out: string; name: string; target: string }) => {\n log.bold('LocalKit Build')\n\n const target = targets[opts.target as keyof typeof targets]\n if (!target) {\n log.error(`Unknown target: ${opts.target}. Valid targets: ${Object.keys(targets).join(', ')}`)\n process.exit(1)\n }\n\n const outDir = resolve(process.cwd(), opts.out)\n log.info(`Generating ${target.name} backend to: ${outDir}`)\n\n try {\n target.generate({ appName: opts.name, outDir })\n log.success(`Backend generated at ${outDir}`)\n if (opts.target === 'node') {\n log.dim(`Run: cd ${opts.out} && npm install && npm run dev`)\n } else {\n log.dim(`Run: cd ${opts.out} && npm install && wrangler deploy`)\n }\n } catch (err) {\n log.error(`Build failed: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n })\n}\n","import type { Command } from 'commander'\nimport { execSync, spawnSync } from 'node:child_process'\nimport { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs'\nimport { randomBytes } from 'node:crypto'\nimport { resolve, dirname } from 'node:path'\nimport { cloudflareTarget } from '../targets/cloudflare.js'\nimport { log } from '../utils.js'\n\n// --- Types ---\n\nexport interface DeployState {\n kvNamespaceId: string\n r2BucketName: string\n workerName: string\n workerUrl?: string\n jwtSecretSet: boolean\n}\n\ninterface WranglerResult {\n stdout: string\n stderr: string\n status: number | null\n}\n\n// --- Helpers ---\n\nexport function runWrangler(\n args: string[],\n options?: { cwd?: string; input?: string },\n): WranglerResult {\n const result = spawnSync('wrangler', args, {\n encoding: 'utf8',\n stdio: ['pipe', 'pipe', 'pipe'],\n ...options,\n })\n return {\n stdout: result.stdout ?? '',\n stderr: result.stderr ?? '',\n status: result.status,\n }\n}\n\nfunction checkWrangler(): string | null {\n try {\n const result = execSync('wrangler --version', {\n encoding: 'utf8',\n stdio: ['ignore', 'pipe', 'ignore'],\n })\n return result.trim()\n } catch {\n return null\n }\n}\n\nexport function checkAuth(): boolean {\n const result = runWrangler(['whoami'])\n return result.status === 0\n}\n\nexport function createR2Bucket(name: string): void {\n log.info(`Creating R2 bucket: ${name}`)\n const result = runWrangler(['r2', 'bucket', 'create', name])\n if (result.status !== 0) {\n if (result.stderr.includes('already exists')) {\n log.info(`R2 bucket \"${name}\" already exists, continuing`)\n return\n }\n if (result.stderr.includes('R2') || result.stderr.includes('not allowed') || result.stderr.includes('not enabled')) {\n throw new Error(\n `Failed to create R2 bucket: ${result.stderr}\\n` +\n 'Hint: Enable R2 in your Cloudflare dashboard at https://dash.cloudflare.com → R2 Object Storage before deploying.',\n )\n }\n throw new Error(`Failed to create R2 bucket: ${result.stderr}`)\n }\n log.success(`R2 bucket \"${name}\" created`)\n}\n\nexport function kvNamespaceExists(id: string): boolean {\n const result = runWrangler(['kv', 'namespace', 'list', '--json'])\n if (result.status !== 0) return false\n try {\n const namespaces = JSON.parse(result.stdout) as Array<{ id: string }>\n return namespaces.some((ns) => ns.id === id)\n } catch {\n return false\n }\n}\n\nexport function createKvNamespace(name: string): string {\n log.info(`Creating KV namespace: ${name}`)\n const result = runWrangler(['kv', 'namespace', 'create', name])\n\n if (result.status === 0) {\n // Primary: parse ID from create stdout\n const match = result.stdout.match(/id\\s*=\\s*\"([a-f0-9]+)\"/)\n if (match) {\n log.success(`KV namespace \"${name}\" created with ID: ${match[1]}`)\n return match[1]\n }\n // Try JSON parsing if --json was supported\n try {\n const parsed = JSON.parse(result.stdout)\n if (parsed.id) return parsed.id\n } catch {\n // not JSON, continue\n }\n }\n\n // Fallback: namespace already exists, list and find by title\n if (result.stderr.includes('already exists') || result.status !== 0) {\n log.info(`KV namespace \"${name}\" may already exist, looking up ID...`)\n const listResult = runWrangler(['kv', 'namespace', 'list', '--json'])\n if (listResult.status === 0) {\n try {\n const namespaces = JSON.parse(listResult.stdout) as Array<{\n id: string\n title: string\n }>\n const found = namespaces.find((ns) => ns.title === name)\n if (found) {\n log.success(`Found existing KV namespace \"${name}\" with ID: ${found.id}`)\n return found.id\n }\n } catch {\n log.warn('Failed to parse KV namespace list JSON, falling back to regex')\n }\n // Last resort: regex on raw list output\n const idMatch = listResult.stdout.match(/id\\s*=\\s*\"([a-f0-9]+)\"/)\n if (idMatch) return idMatch[1]\n }\n }\n\n throw new Error(\n `Failed to create or find KV namespace \"${name}\". ` +\n `stdout: ${result.stdout}, stderr: ${result.stderr}`,\n )\n}\n\nexport function setSecret(name: string, value: string, cwd: string): void {\n log.info(`Setting secret: ${name}`)\n const result = runWrangler(['secret', 'put', name], { cwd, input: value })\n if (result.status !== 0) {\n throw new Error(`Failed to set secret ${name}: ${result.stderr}`)\n }\n log.success(`Secret \"${name}\" set`)\n}\n\nexport function readDeployState(outDir: string): DeployState | null {\n const statePath = resolve(outDir, '..', 'deploy-state.json')\n if (!existsSync(statePath)) return null\n try {\n return JSON.parse(readFileSync(statePath, 'utf8')) as DeployState\n } catch {\n return null\n }\n}\n\nexport function writeDeployState(outDir: string, state: DeployState): void {\n const statePath = resolve(outDir, '..', 'deploy-state.json')\n mkdirSync(dirname(statePath), { recursive: true })\n writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf8')\n}\n\n// --- Composable Functions ---\n\nexport function provisionCloudflare(\n appName: string,\n _outDir: string,\n dryRun: boolean,\n): { kvNamespaceId: string; r2BucketName: string; workerName: string } {\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n const r2BucketName = `${name}-storage`\n const kvTitle = `${name}-kv`\n const workerName = `${name}-worker`\n\n // Check auth\n if (dryRun) {\n log.dim('[dry-run] Would check wrangler auth (wrangler whoami)')\n log.dim(`[dry-run] Would create R2 bucket: ${r2BucketName}`)\n log.dim(`[dry-run] Would create KV namespace: ${kvTitle}`)\n log.dim('[dry-run] Would set JWT_SECRET')\n return { kvNamespaceId: 'DRY_RUN_PLACEHOLDER', r2BucketName, workerName }\n }\n\n if (!checkAuth()) {\n log.error('Not authenticated with Cloudflare.')\n log.dim('Run `wrangler login` to authenticate, then try again.')\n process.exit(1)\n }\n log.success('Authenticated with Cloudflare')\n\n // Create R2 bucket\n createR2Bucket(r2BucketName)\n\n // Create KV namespace\n const kvNamespaceId = createKvNamespace(kvTitle)\n\n return { kvNamespaceId, r2BucketName, workerName }\n}\n\nexport function generateAndDeploy(\n appName: string,\n outDir: string,\n kvNamespaceId: string,\n env: string | undefined,\n dryRun: boolean,\n): string | undefined {\n if (dryRun) {\n log.dim(`[dry-run] Would generate worker code to: ${outDir}`)\n log.dim(`[dry-run] Would run: npm install (in ${outDir})`)\n log.dim(`[dry-run] Would run: wrangler deploy${env ? ` --env ${env}` : ''}`)\n return undefined\n }\n\n // Generate worker code with real KV namespace ID\n log.info('Generating worker code...')\n cloudflareTarget.generate({ appName, outDir, kvNamespaceId })\n log.success('Worker code generated')\n\n // npm install\n log.info('Installing dependencies...')\n const installResult = spawnSync('npm', ['install'], {\n cwd: outDir,\n encoding: 'utf8',\n stdio: ['pipe', 'pipe', 'pipe'],\n })\n if (installResult.status !== 0) {\n throw new Error(`npm install failed: ${installResult.stderr}`)\n }\n log.success('Dependencies installed')\n\n // wrangler deploy\n log.info('Deploying to Cloudflare...')\n const deployArgs = ['deploy']\n if (env) deployArgs.push('--env', env)\n\n const deployResult = spawnSync('wrangler', deployArgs, {\n cwd: outDir,\n encoding: 'utf8',\n stdio: ['pipe', 'pipe', 'pipe'],\n })\n\n if (deployResult.status !== 0) {\n throw new Error(\n `wrangler deploy failed (exit ${deployResult.status}): ${deployResult.stderr}`,\n )\n }\n\n // Parse worker URL\n const urlMatch = deployResult.stdout.match(/https:\\/\\/[^\\s]+\\.workers\\.dev/)\n const workerUrl = urlMatch ? urlMatch[0] : undefined\n if (workerUrl) {\n log.success(`Deployed to: ${workerUrl}`)\n } else {\n log.success('Deployed successfully!')\n }\n\n return workerUrl\n}\n\n// --- Command Registration ---\n\nexport function registerDeploy(program: Command): void {\n program\n .command('deploy')\n .description('Deploy the LocalKit Worker to Cloudflare via Wrangler')\n .option('-d, --dir <dir>', 'Worker output directory', '.offlinekit/worker')\n .option('-n, --name <name>', 'App name for the worker', 'localkit-app')\n .option('-e, --env <env>', 'Wrangler environment (e.g. production, staging)')\n .option('-t, --target <target>', 'Deploy target (default: cloudflare)', 'cloudflare')\n .option('--dry-run', 'Show what would happen without executing')\n .action(\n (opts: {\n dir: string\n name: string\n env?: string\n target: string\n dryRun?: boolean\n }) => {\n log.bold('LocalKit Deploy')\n\n const dryRun = opts.dryRun ?? false\n\n // Check if project has been ejected\n const serverDir = resolve(process.cwd(), 'server')\n if (existsSync(serverDir)) {\n log.info(\n 'Warning: This project has been ejected (./server/ exists).',\n )\n log.dim(\n 'You can deploy the ejected code directly with `cd server && wrangler deploy`.',\n )\n }\n\n // Validate target\n if (opts.target !== 'cloudflare') {\n log.error(\n `Deploy target \"${opts.target}\" is not supported yet. Only \"cloudflare\" is deployable.`,\n )\n log.dim(\n 'For Node.js targets, run `npx mpb-localkit build --target node` then deploy the output manually.',\n )\n process.exit(1)\n }\n\n // Check wrangler\n const version = checkWrangler()\n if (!version) {\n log.error('wrangler is not installed or not in PATH.')\n log.dim('Install it with: npm install -g wrangler')\n process.exit(1)\n }\n log.success(`wrangler ${version}`)\n\n const outDir = resolve(process.cwd(), opts.dir)\n const appName = opts.name\n\n // Read existing deploy state\n const existingState = readDeployState(outDir)\n\n let kvNamespaceId: string\n let r2BucketName: string\n let workerName: string\n if (existingState?.kvNamespaceId && existingState?.r2BucketName) {\n // Subsequent deploy -- validate resources still exist\n log.info('Found existing deploy state, validating resources...')\n r2BucketName = existingState.r2BucketName\n workerName = existingState.workerName\n\n if (dryRun) {\n log.dim('[dry-run] Would validate KV namespace exists')\n kvNamespaceId = existingState.kvNamespaceId\n } else if (kvNamespaceExists(existingState.kvNamespaceId)) {\n log.success('KV namespace validated')\n kvNamespaceId = existingState.kvNamespaceId\n } else {\n log.warn('KV namespace no longer exists, re-creating...')\n const name = appName.toLowerCase().replace(/[^a-z0-9-]/g, '-')\n kvNamespaceId = createKvNamespace(`${name}-kv`)\n writeDeployState(outDir, {\n ...existingState,\n kvNamespaceId,\n })\n log.success('Deploy state updated with new KV namespace ID')\n }\n } else {\n // First deploy -- provision resources\n const provisioned = provisionCloudflare(appName, outDir, dryRun)\n kvNamespaceId = provisioned.kvNamespaceId\n r2BucketName = provisioned.r2BucketName\n workerName = provisioned.workerName\n\n // Save provisioned state (jwtSecretSet: false until secret is actually set)\n if (!dryRun) {\n writeDeployState(outDir, {\n kvNamespaceId,\n r2BucketName,\n workerName,\n jwtSecretSet: false,\n })\n log.success('Deploy state saved to .offlinekit/deploy-state.json')\n }\n }\n\n // Pre-flight: ensure we never deploy with a placeholder KV namespace ID\n if (kvNamespaceId === 'REPLACE_WITH_KV_NAMESPACE_ID' || !kvNamespaceId) {\n log.error(\n 'KV namespace ID is missing or still a placeholder. ' +\n 'Run `mpb-localkit deploy` (not `wrangler deploy` directly) to auto-provision resources, ' +\n 'or create one manually: wrangler kv namespace create KV',\n )\n process.exit(1)\n }\n\n const alreadyHasSecret = existingState?.jwtSecretSet ?? false\n\n // Generate code and deploy (this creates wrangler.toml in outDir)\n let workerUrl: string | undefined\n try {\n workerUrl = generateAndDeploy(appName, outDir, kvNamespaceId, opts.env, dryRun)\n } catch (err) {\n log.error(`Deploy failed: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n\n if (dryRun) {\n if (!alreadyHasSecret) {\n log.dim('[dry-run] Would set JWT_SECRET (first deploy only)')\n }\n log.bold('Dry run complete. No changes were made.')\n return\n }\n\n // Set JWT_SECRET now that wrangler.toml exists in outDir\n let jwtSecretSet = alreadyHasSecret\n if (!jwtSecretSet) {\n try {\n setSecret('JWT_SECRET', randomBytes(32).toString('hex'), outDir)\n log.success('JWT_SECRET set')\n jwtSecretSet = true\n } catch (err) {\n log.warn(`Could not set JWT_SECRET: ${err instanceof Error ? err.message : String(err)}`)\n log.dim('Set it manually: wrangler secret put JWT_SECRET')\n }\n }\n\n // Persist final state with workerUrl\n writeDeployState(outDir, { kvNamespaceId, r2BucketName, workerName, workerUrl, jwtSecretSet })\n\n // Optionally update .env\n if (workerUrl) {\n const envPath = resolve(process.cwd(), '.env')\n if (existsSync(envPath)) {\n let envContent = readFileSync(envPath, 'utf8')\n if (envContent.includes('VITE_SYNC_URL=')) {\n envContent = envContent.replace(/VITE_SYNC_URL=.*/, `VITE_SYNC_URL=${workerUrl}`)\n } else {\n envContent += `\\nVITE_SYNC_URL=${workerUrl}\\n`\n }\n writeFileSync(envPath, envContent, 'utf8')\n log.info(`Updated .env with VITE_SYNC_URL=${workerUrl}`)\n }\n }\n },\n )\n}\n","import type { Command } from 'commander'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { cloudflareTarget } from '../targets/cloudflare.js'\nimport { nodeTarget } from '../targets/node.js'\nimport type { DeployTarget } from '../targets/types.js'\nimport { log } from '../utils.js'\n\nconst targets: Record<string, DeployTarget> = {\n cloudflare: cloudflareTarget,\n node: nodeTarget,\n}\n\nexport function registerEject(program: Command): void {\n program\n .command('eject')\n .description('Eject generated Worker code into ./server/ for manual customization')\n .option('-o, --out <dir>', 'Output directory for ejected code', 'server')\n .option('-n, --name <name>', 'App name', 'localkit-app')\n .option('-t, --target <target>', 'Deploy target (cloudflare, node)', 'cloudflare')\n .action((opts: { out: string; name: string; target: string }) => {\n log.bold('LocalKit Eject')\n\n const target = targets[opts.target]\n if (!target) {\n log.error(`Unknown target: \"${opts.target}\"`)\n log.dim(`Available targets: ${Object.keys(targets).join(', ')}`)\n process.exit(1)\n }\n\n const outDir = resolve(process.cwd(), opts.out)\n\n if (existsSync(outDir)) {\n log.error(`Directory already exists: ${outDir}`)\n log.dim('Remove it or choose a different output directory with --out.')\n process.exit(1)\n }\n\n log.info(`Ejecting ${opts.target} code to: ${outDir}`)\n\n try {\n target.generate({ appName: opts.name, outDir })\n log.success(`Ejected to ./${opts.out}/`)\n\n if (opts.target === 'node') {\n log.dim('You can now customize and deploy your Node.js server.')\n log.dim('')\n log.dim('Next steps:')\n log.dim(` cd ${opts.out}`)\n log.dim(' npm install')\n log.dim(' npm run dev')\n log.dim('')\n log.dim('To run in production: npm start')\n } else {\n log.dim('You can now customize and deploy with `wrangler deploy`.')\n log.dim('')\n log.dim('Next steps:')\n log.dim(` cd ${opts.out}`)\n log.dim(' npm install')\n log.dim(' wrangler deploy')\n log.dim('')\n log.dim('Note: Running `npx mpb-localkit deploy` will warn that code has been ejected.')\n }\n } catch (err) {\n log.error(`Eject failed: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n })\n}\n","#!/usr/bin/env node\nimport { Command } from 'commander'\nimport { createRequire } from 'node:module'\nimport { registerDev } from './commands/dev.js'\nimport { registerBuild } from './commands/build.js'\nimport { registerDeploy } from './commands/deploy.js'\nimport { registerEject } from './commands/eject.js'\n\nconst require = createRequire(import.meta.url)\nconst pkg = require('../../package.json') as { version: string; description: string }\n\nconst program = new Command()\n\nprogram\n .name('mpb-localkit')\n .description(pkg.description)\n .version(pkg.version, '-v, --version', 'Print the current version')\n\nregisterDev(program)\nregisterBuild(program)\nregisterDeploy(program)\nregisterEject(program)\n\nprogram.parse(process.argv)\n"]}
|