mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
cli create: improve cloudflare (create d1, r2)
This commit is contained in:
@@ -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 },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user