@samebase/convex-hosting 0.0.0-stage → 0.3.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.
Files changed (122) hide show
  1. package/CHANGELOG.md +133 -0
  2. package/INTEGRATION.md +399 -0
  3. package/LICENSE +201 -0
  4. package/NOTICE +7 -0
  5. package/README.md +471 -2
  6. package/dist/cli/args.d.ts +28 -0
  7. package/dist/cli/args.d.ts.map +1 -0
  8. package/dist/cli/args.js +148 -0
  9. package/dist/cli/args.js.map +1 -0
  10. package/dist/cli/argumentChunks.d.ts +3 -0
  11. package/dist/cli/argumentChunks.d.ts.map +1 -0
  12. package/dist/cli/argumentChunks.js +29 -0
  13. package/dist/cli/argumentChunks.js.map +1 -0
  14. package/dist/cli/collectFiles.d.ts +10 -0
  15. package/dist/cli/collectFiles.d.ts.map +1 -0
  16. package/dist/cli/collectFiles.js +53 -0
  17. package/dist/cli/collectFiles.js.map +1 -0
  18. package/dist/cli/commands.d.ts +8 -0
  19. package/dist/cli/commands.d.ts.map +1 -0
  20. package/dist/cli/commands.js +56 -0
  21. package/dist/cli/commands.js.map +1 -0
  22. package/dist/cli/componentName.d.ts +32 -0
  23. package/dist/cli/componentName.d.ts.map +1 -0
  24. package/dist/cli/componentName.js +43 -0
  25. package/dist/cli/componentName.js.map +1 -0
  26. package/dist/cli/deploy.d.ts +16 -0
  27. package/dist/cli/deploy.d.ts.map +1 -0
  28. package/dist/cli/deploy.js +283 -0
  29. package/dist/cli/deploy.js.map +1 -0
  30. package/dist/cli/deployEnvironment.d.ts +10 -0
  31. package/dist/cli/deployEnvironment.d.ts.map +1 -0
  32. package/dist/cli/deployEnvironment.js +12 -0
  33. package/dist/cli/deployEnvironment.js.map +1 -0
  34. package/dist/cli/index.d.ts +12 -0
  35. package/dist/cli/index.d.ts.map +1 -0
  36. package/dist/cli/index.js +60 -0
  37. package/dist/cli/index.js.map +1 -0
  38. package/dist/cli/upload.d.ts +22 -0
  39. package/dist/cli/upload.d.ts.map +1 -0
  40. package/dist/cli/upload.js +630 -0
  41. package/dist/cli/upload.js.map +1 -0
  42. package/dist/client/_generated/_ignore.d.ts +1 -0
  43. package/dist/client/_generated/_ignore.d.ts.map +1 -0
  44. package/dist/client/_generated/_ignore.js +3 -0
  45. package/dist/client/_generated/_ignore.js.map +1 -0
  46. package/dist/client/index.d.ts +119 -0
  47. package/dist/client/index.d.ts.map +1 -0
  48. package/dist/client/index.js +272 -0
  49. package/dist/client/index.js.map +1 -0
  50. package/dist/component/_generated/api.d.ts +38 -0
  51. package/dist/component/_generated/api.d.ts.map +1 -0
  52. package/dist/component/_generated/api.js +31 -0
  53. package/dist/component/_generated/api.js.map +1 -0
  54. package/dist/component/_generated/component.d.ts +43 -0
  55. package/dist/component/_generated/component.d.ts.map +1 -0
  56. package/dist/component/_generated/component.js +11 -0
  57. package/dist/component/_generated/component.js.map +1 -0
  58. package/dist/component/_generated/dataModel.d.ts +46 -0
  59. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  60. package/dist/component/_generated/dataModel.js +11 -0
  61. package/dist/component/_generated/dataModel.js.map +1 -0
  62. package/dist/component/_generated/server.d.ts +121 -0
  63. package/dist/component/_generated/server.d.ts.map +1 -0
  64. package/dist/component/_generated/server.js +78 -0
  65. package/dist/component/_generated/server.js.map +1 -0
  66. package/dist/component/convex.config.d.ts +3 -0
  67. package/dist/component/convex.config.d.ts.map +1 -0
  68. package/dist/component/convex.config.js +3 -0
  69. package/dist/component/convex.config.js.map +1 -0
  70. package/dist/component/http.d.ts +4 -0
  71. package/dist/component/http.d.ts.map +1 -0
  72. package/dist/component/http.js +125 -0
  73. package/dist/component/http.js.map +1 -0
  74. package/dist/component/lib.d.ts +164 -0
  75. package/dist/component/lib.d.ts.map +1 -0
  76. package/dist/component/lib.js +768 -0
  77. package/dist/component/lib.js.map +1 -0
  78. package/dist/component/schema.d.ts +84 -0
  79. package/dist/component/schema.d.ts.map +1 -0
  80. package/dist/component/schema.js +69 -0
  81. package/dist/component/schema.js.map +1 -0
  82. package/dist/component/serving.d.ts +17 -0
  83. package/dist/component/serving.d.ts.map +1 -0
  84. package/dist/component/serving.js +150 -0
  85. package/dist/component/serving.js.map +1 -0
  86. package/dist/react/index.d.ts +59 -0
  87. package/dist/react/index.d.ts.map +1 -0
  88. package/dist/react/index.js +137 -0
  89. package/dist/react/index.js.map +1 -0
  90. package/package.json +78 -3
  91. package/src/cli/args.test.ts +79 -0
  92. package/src/cli/args.ts +153 -0
  93. package/src/cli/argumentChunks.test.ts +27 -0
  94. package/src/cli/argumentChunks.ts +40 -0
  95. package/src/cli/collectFiles.test.ts +26 -0
  96. package/src/cli/collectFiles.ts +59 -0
  97. package/src/cli/commands.ts +68 -0
  98. package/src/cli/componentName.test.ts +41 -0
  99. package/src/cli/componentName.ts +46 -0
  100. package/src/cli/deploy.ts +336 -0
  101. package/src/cli/deployEnvironment.test.ts +27 -0
  102. package/src/cli/deployEnvironment.ts +17 -0
  103. package/src/cli/index.ts +65 -0
  104. package/src/cli/upload.ts +837 -0
  105. package/src/client/_generated/_ignore.ts +1 -0
  106. package/src/client/index.test.ts +510 -0
  107. package/src/client/index.ts +355 -0
  108. package/src/client/setup.test.ts +22 -0
  109. package/src/component/_generated/api.ts +54 -0
  110. package/src/component/_generated/component.ts +55 -0
  111. package/src/component/_generated/dataModel.ts +60 -0
  112. package/src/component/_generated/server.ts +156 -0
  113. package/src/component/convex.config.ts +3 -0
  114. package/src/component/http.test.ts +223 -0
  115. package/src/component/http.ts +144 -0
  116. package/src/component/lib.test.ts +1007 -0
  117. package/src/component/lib.ts +862 -0
  118. package/src/component/schema.ts +75 -0
  119. package/src/component/serving.ts +158 -0
  120. package/src/component/setup.test.ts +11 -0
  121. package/src/react/index.tsx +197 -0
  122. package/src/test.ts +23 -0
@@ -0,0 +1,336 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * One-shot deployment command that deploys both Convex backend and static files.
4
+ *
5
+ * Usage:
6
+ * npx @samebase/convex-hosting deploy [options]
7
+ *
8
+ * This command:
9
+ * 1. Builds the frontend with the correct VITE_CONVEX_URL
10
+ * 2. Deploys the Convex backend (npx convex deploy)
11
+ * 3. Deploys static files to Convex storage
12
+ *
13
+ * The goal is to minimize the inconsistency window between backend and frontend.
14
+ */
15
+
16
+ import { existsSync } from "fs";
17
+ import { resolve } from "path";
18
+ import { runConvex, spawnConvex, spawnShell, spawnStaticHostingCli } from "./commands.js";
19
+ import { buildDeployUploadArgs, parseDeployArgs, type DeployArgs } from "./args.js";
20
+ import {
21
+ buildEnvironment,
22
+ buildEnvironmentChanged,
23
+ type DeploymentUrls,
24
+ } from "./deployEnvironment.js";
25
+ import {
26
+ componentNameCandidates,
27
+ isLegacyAutoDetected,
28
+ legacyComponentNameWarning,
29
+ } from "./componentName.js";
30
+
31
+ function showHelp(): void {
32
+ console.log(`
33
+ Usage: npx @samebase/convex-hosting deploy [options]
34
+
35
+ One-shot deployment: builds frontend, deploys Convex backend, then deploys static files.
36
+ Minimizes the inconsistency window between backend and frontend updates.
37
+
38
+ Options:
39
+ -d, --dist <path> Path to dist directory (default: ./dist)
40
+ -c, --component <name> Convex component name (default: hosting)
41
+ --skip-build Skip the build step (use existing dist)
42
+ --skip-convex Skip Convex backend deployment
43
+ --build-command <cmd> Build command to run (default: 'npm run build')
44
+ --no-spa Disable SPA fallback (404 instead of /index.html)
45
+ --spa Enable SPA fallback (default)
46
+ --cdn Use the legacy convex-fs integration
47
+ --cdn-delete-function <name> Legacy app function that deletes CDN blobs
48
+ -h, --help Show this help message
49
+
50
+ Deployment Flow:
51
+ 1. Build frontend with production VITE_CONVEX_URL
52
+ 2. Deploy Convex backend (npx convex deploy)
53
+ 3. Rebuild if the backend changed the static mount path
54
+ 4. Deploy static files to Convex storage
55
+
56
+ Examples:
57
+ # Full deployment
58
+ npx @samebase/convex-hosting deploy
59
+
60
+ # Skip build (if already built)
61
+ npx @samebase/convex-hosting deploy --skip-build
62
+
63
+ # Only deploy static files (skip Convex backend)
64
+ npx @samebase/convex-hosting deploy --skip-convex
65
+ `);
66
+ }
67
+
68
+ /**
69
+ * Resolve the component's deployment URLs (siteUrl + cloudUrl). Returns null
70
+ * if the component isn't reachable yet; on first deploy the backend may not
71
+ * exist, in which case the caller should deploy it first and retry.
72
+ */
73
+ function tryFetchUrls(componentName: string): DeploymentUrls | null {
74
+ try {
75
+ const out = runConvex([
76
+ "run",
77
+ "--component",
78
+ componentName,
79
+ "lib:getUrls",
80
+ "{}",
81
+ "--prod",
82
+ "--typecheck=disable",
83
+ "--codegen=disable",
84
+ ]);
85
+ return JSON.parse(out);
86
+ } catch {
87
+ return null;
88
+ }
89
+ }
90
+
91
+ interface ResolvedComponent {
92
+ urls: DeploymentUrls;
93
+ componentName: string;
94
+ }
95
+
96
+ // The legacy-name warning is printed at most once per invocation.
97
+ let warnedLegacyName = false;
98
+
99
+ /**
100
+ * Resolve the deployment URLs and the instance name that answered. When the
101
+ * caller relied on the default name we also probe the @convex-dev/static-hosting
102
+ * name, so an app that kept that instance works without `--component staticHosting`. Returns
103
+ * null if no candidate is deployed yet (the caller may deploy the backend and
104
+ * retry).
105
+ */
106
+ function tryResolveComponent(requested: string): ResolvedComponent | null {
107
+ for (const componentName of componentNameCandidates(requested)) {
108
+ const urls = tryFetchUrls(componentName);
109
+ if (urls) {
110
+ if (isLegacyAutoDetected(requested, componentName) && !warnedLegacyName) {
111
+ console.warn(legacyComponentNameWarning(componentName));
112
+ warnedLegacyName = true;
113
+ }
114
+ return { urls, componentName };
115
+ }
116
+ }
117
+ return null;
118
+ }
119
+
120
+ function resolveComponentOrExit(requested: string): ResolvedComponent {
121
+ const resolved = tryResolveComponent(requested);
122
+ if (!resolved) {
123
+ const names = componentNameCandidates(requested)
124
+ .map((name) => `"${name}"`)
125
+ .join(" or ");
126
+ console.error(
127
+ `Could not reach component ${names}. Deploy the Convex backend first (npx convex deploy) and ensure --component matches the name in convex.config.ts.`,
128
+ );
129
+ process.exit(1);
130
+ }
131
+ return resolved;
132
+ }
133
+
134
+ /**
135
+ * Run the Convex storage upload flow
136
+ */
137
+ async function uploadToConvexStorage(args: DeployArgs): Promise<boolean> {
138
+ console.log("");
139
+ console.log(
140
+ args.cdn
141
+ ? "📦 Uploading static files (HTML to Convex, assets to CDN)..."
142
+ : "📦 Uploading static files to Convex storage...",
143
+ );
144
+ console.log("");
145
+
146
+ const result = spawnStaticHostingCli(buildDeployUploadArgs(args));
147
+
148
+ return result === 0;
149
+ }
150
+
151
+ function runFrontendBuild(args: DeployArgs, urls: DeploymentUrls): void {
152
+ const environment = buildEnvironment(urls);
153
+ console.log(` Build command: ${args.buildCommand}`);
154
+ console.log(` VITE_CONVEX_URL=${environment.cloudUrl}`);
155
+ console.log(` CONVEX_HOSTING_BASE_PATH=${environment.basePath}`);
156
+ console.log("");
157
+
158
+ const buildResult = spawnShell(args.buildCommand, {
159
+ ...process.env,
160
+ VITE_CONVEX_URL: environment.cloudUrl,
161
+ CONVEX_HOSTING_BASE_PATH: environment.basePath,
162
+ });
163
+ if (buildResult !== 0) {
164
+ throw new Error("Frontend build failed");
165
+ }
166
+ }
167
+
168
+ async function main(): Promise<void> {
169
+ const args = parseDeployArgs(process.argv.slice(2));
170
+
171
+ if (args.help) {
172
+ showHelp();
173
+ process.exit(0);
174
+ }
175
+
176
+ console.log("");
177
+ console.log("🚀 Convex + Static Files Deployment");
178
+ console.log("═══════════════════════════════════════════════════════════");
179
+
180
+ const startTime = Date.now();
181
+
182
+ // Step 1: Get deployment URLs (needed for build)
183
+ console.log("");
184
+ console.log("Step 1: Getting deployment URLs...");
185
+
186
+ let resolved = tryResolveComponent(args.component);
187
+ let urls = resolved?.urls ?? null;
188
+ // Falls back to the requested name until the backend is deployed and a real
189
+ // instance answers; re-resolved after each deploy below.
190
+ let componentName = resolved?.componentName ?? args.component;
191
+ const shouldDeployBackend = !args.skipConvex;
192
+ let backendAlreadyDeployed = false;
193
+
194
+ if (!urls && !args.skipConvex) {
195
+ console.log(" Component not yet deployed. Will fetch URLs after deploying backend.");
196
+ } else if (urls) {
197
+ console.log(` ✓ ${urls.siteUrl}`);
198
+ }
199
+
200
+ // Step 2: Build frontend
201
+ if (!args.skipBuild) {
202
+ console.log("");
203
+ console.log("Step 2: Building frontend...");
204
+
205
+ // If the component isn't deployed yet, deploy the backend first so we
206
+ // can ask it for the URLs.
207
+ if (!urls && shouldDeployBackend) {
208
+ console.log(" Deploying Convex backend first to get URLs...");
209
+ console.log("");
210
+
211
+ const convexResult = spawnConvex(["deploy"]);
212
+
213
+ if (convexResult !== 0) {
214
+ console.error("");
215
+ console.error("❌ Convex deployment failed");
216
+ process.exit(1);
217
+ }
218
+
219
+ resolved = resolveComponentOrExit(args.component);
220
+ urls = resolved.urls;
221
+ componentName = resolved.componentName;
222
+ console.log("");
223
+ console.log(` ✓ Site URL: ${urls.siteUrl}`);
224
+ backendAlreadyDeployed = true;
225
+ }
226
+
227
+ if (!urls) {
228
+ console.error("");
229
+ console.error("❌ Could not determine deployment URLs for build");
230
+ console.error(" Run 'npx convex deploy' first or remove --skip-convex");
231
+ process.exit(1);
232
+ }
233
+
234
+ try {
235
+ runFrontendBuild(args, urls);
236
+ } catch {
237
+ console.error("");
238
+ console.error("❌ Build failed");
239
+ process.exit(1);
240
+ }
241
+
242
+ console.log("");
243
+ console.log(" ✓ Build complete");
244
+ } else {
245
+ console.log("");
246
+ console.log("Step 2: Skipping build (--skip-build)");
247
+ }
248
+
249
+ // Step 3: Deploy Convex backend
250
+ if (shouldDeployBackend && !backendAlreadyDeployed) {
251
+ console.log("");
252
+ console.log("Step 3: Deploying Convex backend...");
253
+ console.log("");
254
+
255
+ const convexResult = spawnConvex(["deploy"]);
256
+
257
+ if (convexResult !== 0) {
258
+ console.error("");
259
+ console.error("❌ Convex deployment failed");
260
+ process.exit(1);
261
+ }
262
+
263
+ console.log("");
264
+ console.log(" ✓ Convex backend deployed");
265
+
266
+ const deployedComponent = resolveComponentOrExit(args.component);
267
+ const deployedUrls = deployedComponent.urls;
268
+ componentName = deployedComponent.componentName;
269
+ if (!args.skipBuild) {
270
+ if (!urls || buildEnvironmentChanged(urls, deployedUrls)) {
271
+ console.log("");
272
+ console.log(
273
+ " Site URL changed after backend deployment. Rebuilding with the new environment...",
274
+ );
275
+ try {
276
+ runFrontendBuild(args, deployedUrls);
277
+ } catch {
278
+ console.error("");
279
+ console.error("❌ Rebuild failed after backend URL change");
280
+ process.exit(1);
281
+ }
282
+ console.log("");
283
+ console.log(" ✓ Rebuild complete");
284
+ }
285
+ }
286
+ urls = deployedUrls;
287
+ } else {
288
+ console.log("");
289
+ console.log("Step 3: Skipping Convex deployment (--skip-convex or already deployed)");
290
+ }
291
+
292
+ // Step 4: Deploy static files
293
+ console.log("");
294
+ console.log("Step 4: Deploying static files to Convex storage...");
295
+
296
+ const distDir = resolve(args.dist);
297
+
298
+ if (!existsSync(distDir)) {
299
+ console.error("");
300
+ console.error(`❌ Dist directory not found: ${distDir}`);
301
+ console.error(" Run build first or check --dist path");
302
+ process.exit(1);
303
+ }
304
+
305
+ // Pass the resolved instance name to the upload subprocess so it targets the
306
+ // same component (and doesn't re-probe or re-warn).
307
+ const staticDeploySuccess = await uploadToConvexStorage({
308
+ ...args,
309
+ dist: distDir,
310
+ component: componentName,
311
+ });
312
+
313
+ if (!staticDeploySuccess) {
314
+ console.error("");
315
+ console.error("❌ Static file upload failed");
316
+ process.exit(1);
317
+ }
318
+
319
+ // Done!
320
+ const duration = ((Date.now() - startTime) / 1000).toFixed(1);
321
+
322
+ console.log("");
323
+ console.log("═══════════════════════════════════════════════════════════");
324
+ console.log(`✨ Deployment complete! (${duration}s)`);
325
+ console.log("");
326
+
327
+ const finalUrls = urls ?? resolveComponentOrExit(args.component).urls;
328
+ console.log(`Frontend: ${finalUrls.siteUrl}`);
329
+
330
+ console.log("");
331
+ }
332
+
333
+ main().catch((error) => {
334
+ console.error("Deployment failed:", error);
335
+ process.exit(1);
336
+ });
@@ -0,0 +1,27 @@
1
+ import { describe, expect, test } from "vite-plus/test";
2
+ import { buildEnvironmentChanged } from "./deployEnvironment.js";
3
+
4
+ describe("deploy build environment", () => {
5
+ test("detects a mount-prefix change after backend deployment", () => {
6
+ expect(
7
+ buildEnvironmentChanged(
8
+ {
9
+ siteUrl: "https://example.convex.site/old/",
10
+ cloudUrl: "https://example.convex.cloud",
11
+ },
12
+ {
13
+ siteUrl: "https://example.convex.site/new/",
14
+ cloudUrl: "https://example.convex.cloud",
15
+ },
16
+ ),
17
+ ).toBe(true);
18
+ });
19
+
20
+ test("does not rebuild when the build environment is unchanged", () => {
21
+ const urls = {
22
+ siteUrl: "https://example.convex.site/app/",
23
+ cloudUrl: "https://example.convex.cloud",
24
+ };
25
+ expect(buildEnvironmentChanged(urls, urls)).toBe(false);
26
+ });
27
+ });
@@ -0,0 +1,17 @@
1
+ export interface DeploymentUrls {
2
+ siteUrl: string;
3
+ cloudUrl: string;
4
+ }
5
+
6
+ export function buildEnvironment(urls: DeploymentUrls) {
7
+ return {
8
+ cloudUrl: urls.cloudUrl,
9
+ basePath: new URL(urls.siteUrl).pathname || "/",
10
+ };
11
+ }
12
+
13
+ export function buildEnvironmentChanged(before: DeploymentUrls, after: DeploymentUrls): boolean {
14
+ const previous = buildEnvironment(before);
15
+ const next = buildEnvironment(after);
16
+ return previous.cloudUrl !== next.cloudUrl || previous.basePath !== next.basePath;
17
+ }
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * CLI for Convex Hosting
4
+ *
5
+ * Commands:
6
+ * deploy One-shot deployment (Convex backend + static files)
7
+ * upload Upload static files to Convex storage
8
+ */
9
+
10
+ const command = process.argv[2];
11
+
12
+ async function main() {
13
+ switch (command) {
14
+ case "deploy":
15
+ // Pass remaining args to deploy command
16
+ process.argv.splice(2, 1);
17
+ await import("./deploy.js");
18
+ break;
19
+
20
+ case "upload":
21
+ // Pass remaining args to upload command
22
+ process.argv.splice(2, 1);
23
+ await import("./upload.js");
24
+ break;
25
+
26
+ case "--help":
27
+ case "-h":
28
+ case undefined:
29
+ printHelp();
30
+ break;
31
+
32
+ default:
33
+ console.error(`Unknown command: ${command}`);
34
+ console.log("");
35
+ printHelp();
36
+ process.exit(1);
37
+ }
38
+ }
39
+
40
+ function printHelp() {
41
+ console.log(`
42
+ Convex Hosting CLI
43
+
44
+ Usage:
45
+ npx @samebase/convex-hosting <command> [options]
46
+
47
+ Commands:
48
+ deploy One-shot deployment (Convex backend + static files)
49
+ upload Upload static files to Convex storage
50
+
51
+ Examples:
52
+ # One-shot deployment
53
+ npx @samebase/convex-hosting deploy
54
+
55
+ # Upload only (no Convex backend deploy)
56
+ npx @samebase/convex-hosting upload --build --prod
57
+
58
+ Run '<command> --help' for more information on a specific command.
59
+ `);
60
+ }
61
+
62
+ main().catch((err) => {
63
+ console.error("Error:", err);
64
+ process.exit(1);
65
+ });