fix: prepare wrangler config for cloudflare builds
This commit is contained in:
1 parent
a217459847
commit
4ecff6e9bc
5 files changed
+102
-5
No files matched your search
+3
-1
@@ -19,7 +19,9 @@ EDGE_EVER_WORKER_NAME=edgeever
|
||||
EDGE_EVER_D1_DATABASE_NAME=edgeever
|
||||
|
||||
# D1 数据库 ID。
|
||||
# 创建 D1 后,把 wrangler 返回的 database_id 填到这里,并同步替换 wrangler.toml 里的占位值。
|
||||
# 创建 D1 后,把 wrangler 返回的 database_id 填到这里。
|
||||
# Cloudflare Git 自动部署时,也需要在 Build Variables 里设置同名变量。
|
||||
# 脚本会生成临时 .wrangler.generated.toml,不需要把个人 D1 ID 提交到公开仓库。
|
||||
EDGE_EVER_D1_DATABASE_ID=00000000-0000-0000-0000-000000000000
|
||||
|
||||
# R2 生产资源桶名称。
|
||||
|
||||
@@ -10,6 +10,7 @@ build/
|
||||
coverage/
|
||||
|
||||
.wrangler/
|
||||
.wrangler.generated.toml
|
||||
.dev.vars
|
||||
wrangler.local.toml
|
||||
|
||||
|
||||
@@ -146,7 +146,13 @@ bunx wrangler d1 create edgeever
|
||||
bunx wrangler r2 bucket create edgeever-resources
|
||||
```
|
||||
|
||||
把 D1 创建命令返回的 `database_id` 填入本机 `.env.local` 的 `EDGE_EVER_D1_DATABASE_ID`,并同步替换 `wrangler.toml` 里的 `database_id` 占位值。
|
||||
把 D1 创建命令返回的 `database_id` 填入本机 `.env.local` 的 `EDGE_EVER_D1_DATABASE_ID`。部署脚本会在运行时生成临时 `.wrangler.generated.toml`,不需要把个人 D1 ID 提交到公开仓库。
|
||||
|
||||
如果你把 Worker 连接到 GitHub 并开启 Cloudflare 自动构建,需要在 Cloudflare 的 Build Variables 里设置:
|
||||
|
||||
```text
|
||||
EDGE_EVER_D1_DATABASE_ID=<你的 D1 database_id>
|
||||
```
|
||||
|
||||
应用本地迁移:
|
||||
|
||||
|
||||
+3
-3
@@ -15,9 +15,9 @@
|
||||
"build:web": "vite build --config apps/web/vite.config.ts",
|
||||
"preview:web": "vite preview --config apps/web/vite.config.ts --host 127.0.0.1 --port 4173",
|
||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||
"db:migrate:local": "wrangler d1 migrations apply DB --local",
|
||||
"db:migrate:remote": "wrangler d1 migrations apply DB --remote",
|
||||
"deploy": "bun run build && bun run db:migrate:remote && wrangler deploy"
|
||||
"db:migrate:local": "bun scripts/run-wrangler.mjs d1 migrations apply DB --local",
|
||||
"db:migrate:remote": "bun scripts/run-wrangler.mjs d1 migrations apply DB --remote",
|
||||
"deploy": "bun run build && bun run db:migrate:remote && bun scripts/run-wrangler.mjs deploy"
|
||||
},
|
||||
"cloudflare": {
|
||||
"bindings": {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
const PLACEHOLDER_D1_ID = "00000000-0000-0000-0000-000000000000";
|
||||
const UUID_PATTERN =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
const wranglerArgs = process.argv.slice(2);
|
||||
if (wranglerArgs.length === 0) {
|
||||
console.error("Usage: bun scripts/run-wrangler.mjs <wrangler args...>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const baseConfigPath = resolve(process.env.WRANGLER_CONFIG ?? "wrangler.toml");
|
||||
const generatedConfigPath = resolve(".wrangler.generated.toml");
|
||||
let config = readFileSync(baseConfigPath, "utf8");
|
||||
let changed = false;
|
||||
|
||||
const replaceTomlValue = (source, key, value) => {
|
||||
if (!value) {
|
||||
return source;
|
||||
}
|
||||
|
||||
const pattern = new RegExp(`(^${key}\\s*=\\s*")[^"]*(")`, "m");
|
||||
if (!pattern.test(source)) {
|
||||
throw new Error(`Cannot find ${key} in ${baseConfigPath}`);
|
||||
}
|
||||
|
||||
changed = true;
|
||||
return source.replace(pattern, `$1${value}$2`);
|
||||
};
|
||||
|
||||
const d1DatabaseId = process.env.EDGE_EVER_D1_DATABASE_ID?.trim();
|
||||
if (d1DatabaseId) {
|
||||
if (!UUID_PATTERN.test(d1DatabaseId)) {
|
||||
throw new Error("EDGE_EVER_D1_DATABASE_ID must be a Cloudflare D1 UUID.");
|
||||
}
|
||||
|
||||
config = replaceTomlValue(config, "database_id", d1DatabaseId);
|
||||
}
|
||||
|
||||
config = replaceTomlValue(
|
||||
config,
|
||||
"database_name",
|
||||
process.env.EDGE_EVER_D1_DATABASE_NAME?.trim(),
|
||||
);
|
||||
config = replaceTomlValue(
|
||||
config,
|
||||
"bucket_name",
|
||||
process.env.EDGE_EVER_R2_BUCKET_NAME?.trim(),
|
||||
);
|
||||
config = replaceTomlValue(
|
||||
config,
|
||||
"preview_bucket_name",
|
||||
process.env.EDGE_EVER_R2_PREVIEW_BUCKET_NAME?.trim(),
|
||||
);
|
||||
|
||||
const isRemoteCommand =
|
||||
wranglerArgs.includes("deploy") || wranglerArgs.includes("--remote");
|
||||
if (isRemoteCommand && config.includes(`database_id = "${PLACEHOLDER_D1_ID}"`)) {
|
||||
console.error(
|
||||
[
|
||||
"Missing Cloudflare D1 database id.",
|
||||
"Set EDGE_EVER_D1_DATABASE_ID as a Cloudflare Build Variable,",
|
||||
"or replace the database_id placeholder in wrangler.toml / WRANGLER_CONFIG.",
|
||||
].join(" "),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const configPath = changed ? generatedConfigPath : baseConfigPath;
|
||||
if (changed) {
|
||||
writeFileSync(generatedConfigPath, config);
|
||||
}
|
||||
|
||||
const executable = process.platform === "win32" ? "wrangler.cmd" : "wrangler";
|
||||
const result = spawnSync(executable, ["--config", configPath, ...wranglerArgs], {
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
console.error(result.error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
process.exit(result.status ?? 1);
|
||||
Reference in new issue
Block a user