cli create: improve cloudflare (create d1, r2)

This commit is contained in:
dswbx
2025-03-26 10:30:47 +01:00
parent 0f56d554b0
commit 9d122da4b9
3 changed files with 155 additions and 15 deletions

View File

@@ -3,7 +3,7 @@
"type": "module",
"sideEffects": false,
"bin": "./dist/cli/index.js",
"version": "0.10.2",
"version": "0.10.3-rc.1",
"description": "Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.",
"homepage": "https://bknd.io",
"repository": {
@@ -58,7 +58,8 @@
"object-path-immutable": "^4.1.2",
"picocolors": "^1.1.1",
"radix-ui": "^1.1.3",
"swr": "^2.3.3"
"swr": "^2.3.3",
"wrangler": "^4.4.1"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.758.0",

View File

@@ -4,6 +4,7 @@ import { typewriter, wait } from "cli/utils/cli";
import { uuid } from "core/utils";
import c from "picocolors";
import type { Template, TemplateSetupCtx } from ".";
import { exec } from "cli/utils/sys";
const WRANGLER_FILE = "wrangler.json";
@@ -56,6 +57,8 @@ export const cloudflare = {
"Couldn't add database. You can add it manually later. Error: " + c.red(message),
);
}
await createR2(ctx);
},
} as const satisfies Template;
@@ -75,6 +78,13 @@ async function createD1(ctx: TemplateSetupCtx) {
process.exit(1);
}
exec(`npx wrangler d1 create ${name}`);
await $p.stream.info(
(async function* () {
yield* typewriter("Please update your wrangler configuration with the output above.");
})(),
);
await overrideJson(
WRANGLER_FILE,
(json) => ({
@@ -89,17 +99,6 @@ async function createD1(ctx: TemplateSetupCtx) {
}),
{ dir: ctx.dir },
);
await $p.stream.info(
(async function* () {
yield* typewriter(`Database added to ${c.cyan("wrangler.json")}`);
await wait();
yield* typewriter(
`\nNote that if you deploy, you have to create a real database using ${c.cyan("npx wrangler d1 create <name>")} and update your wrangler configuration.`,
c.dim,
);
})(),
);
}
async function createLibsql(ctx: TemplateSetupCtx) {
@@ -142,3 +141,55 @@ async function createLibsql(ctx: TemplateSetupCtx) {
})(),
);
}
async function createR2(ctx: TemplateSetupCtx) {
const create = await $p.confirm({
message: "Do you want to use a R2 bucket?",
initialValue: true,
});
if ($p.isCancel(create)) {
process.exit(1);
}
if (!create) {
await overrideJson(
WRANGLER_FILE,
(json) => ({
...json,
r2_buckets: undefined,
}),
{ dir: ctx.dir },
);
return;
}
const name = await $p.text({
message: "Enter bucket name",
initialValue: "bucket",
placeholder: "bucket",
validate: (v) => {
if (!v) {
return "Invalid name";
}
return;
},
});
if ($p.isCancel(name)) {
process.exit(1);
}
exec(`npx wrangler r2 bucket create ${name}`);
await overrideJson(
WRANGLER_FILE,
(json) => ({
...json,
r2_buckets: [
{
binding: "BUCKET",
bucket_name: name,
},
],
}),
{ dir: ctx.dir },
);
}