Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"lint": "biome lint",
"check": "biome check",
"check-types": "tsc --noEmit",
"migrate": "wrangler d1 migrations apply quickhub-db --local",
"migrate": "node ../../scripts/run-d1-migrations.mjs quickhub-db",
"deploy": "pnpm run build && wrangler deploy"
},
"dependencies": {
Expand Down
14 changes: 13 additions & 1 deletion apps/dashboard/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import viteReact from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import {
getSharedWranglerStatePath,
isWorktreeCheckout,
} from "../../scripts/shared-worktree-paths.mjs";

const dashboardRoot = new URL(".", import.meta.url);
const worktreePersistState = isWorktreeCheckout(dashboardRoot)
? { persistState: { path: getSharedWranglerStatePath(dashboardRoot) } }
: {};

const config = defineConfig({
plugins: [
devtools(),
cloudflare({ viteEnvironment: { name: "ssr" } }),
cloudflare({
viteEnvironment: { name: "ssr" },
...worktreePersistState,
}),
tsconfigPaths({ projects: ["./tsconfig.json"] }),
tailwindcss(),
tanstackStart(),
Expand Down
26 changes: 26 additions & 0 deletions scripts/run-d1-migrations.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { spawnSync } from "node:child_process";
import { getSharedWranglerStatePath, isWorktreeCheckout } from "./shared-worktree-paths.mjs";

const databaseName = process.argv[2];

if (!databaseName) {
console.error("Usage: node scripts/run-d1-migrations.mjs <database-name>");
process.exit(1);
}

const args = ["exec", "wrangler", "d1", "migrations", "apply", databaseName, "--local"];

if (isWorktreeCheckout()) {
args.push("--persist-to", getSharedWranglerStatePath());
}

const result = spawnSync("pnpm", args, {
stdio: "inherit",
});

if (result.error) {
console.error(result.error.message);
process.exit(1);
}

process.exit(result.status ?? 0);
4 changes: 4 additions & 0 deletions scripts/shared-worktree-paths.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getRepoRoot(cwd?: string | URL): string;
export function getPrimaryWorktreeRoot(cwd?: string | URL): string;
export function isWorktreeCheckout(cwd?: string | URL): boolean;
export function getSharedWranglerStatePath(cwd?: string | URL): string;
36 changes: 36 additions & 0 deletions scripts/shared-worktree-paths.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { execSync } from "node:child_process";
import path from "node:path";

function safeExec(command, cwd = process.cwd()) {
return execSync(command, { cwd, stdio: ["ignore", "pipe", "pipe"] })
.toString()
.trim();
}

export function getRepoRoot(cwd = process.cwd()) {
return safeExec("git rev-parse --show-toplevel", cwd);
}

export function getPrimaryWorktreeRoot(cwd = process.cwd()) {
const repoRoot = getRepoRoot(cwd);
const commonDirRaw = safeExec("git rev-parse --git-common-dir", cwd);
const commonDir = path.isAbsolute(commonDirRaw)
? commonDirRaw
: path.resolve(repoRoot, commonDirRaw);

return path.dirname(commonDir);
}

export function isWorktreeCheckout(cwd = process.cwd()) {
return path.resolve(getRepoRoot(cwd)) !== path.resolve(getPrimaryWorktreeRoot(cwd));
}

export function getSharedWranglerStatePath(cwd = process.cwd()) {
return path.join(getPrimaryWorktreeRoot(cwd), ".wrangler", "state");
}

if (process.argv[1] && import.meta.url === new URL(process.argv[1], "file:").href) {
if (isWorktreeCheckout()) {
console.log(getSharedWranglerStatePath());
}
}