diff --git a/scripts/start.mjs b/scripts/start.mjs index 8575710..1dd3eda 100755 --- a/scripts/start.mjs +++ b/scripts/start.mjs @@ -15,7 +15,8 @@ if (Number(process.versions.node.split(".")[0]) < 20) { const win = process.platform === "win32"; const npm = (...args) => spawnSync("npm", args, { stdio: "inherit", shell: win }).status === 0; -if (!existsSync("node_modules") && !npm("install")) process.exit(1); +// npm ci installs exactly what the repo pins and leaves package-lock.json as it is (so `npm run update` stays clean). +if (!existsSync("node_modules") && !npm("ci") && !npm("install")) process.exit(1); const env = {}; for (const f of [".env", ".env.local"]) { diff --git a/scripts/update.mjs b/scripts/update.mjs index e2849d7..01bbf8c 100755 --- a/scripts/update.mjs +++ b/scripts/update.mjs @@ -11,6 +11,8 @@ const run = (...args) => spawnSync("git", args, inherit).status === 0; const out = (...args) => spawnSync("git", args, { encoding: "utf8" }).stdout.trim(); const fail = (m) => { console.error(`\n${m}`); process.exit(1); }; +// npm rewrites package-lock.json on some installs. That isn't a change of yours: put it back before updating. +if (out("status", "--porcelain") === "M package-lock.json") run("checkout", "--", "package-lock.json"); if (out("status", "--porcelain")) fail("You have changes that aren't committed. Commit them first: git add -A && git commit -m \"My changes\""); if (!run("fetch", "--quiet", UPSTREAM, "main")) fail("Couldn't reach broke's repo. Check your connection and try again."); const theirs = out("rev-parse", "FETCH_HEAD"); @@ -23,5 +25,5 @@ if (!out("merge-base", "HEAD", theirs)) { if (!run("merge", "--quiet", "--ff-only", joined)) fail("Couldn't join this copy to broke's history."); } if (!run("merge", "--no-edit", theirs)) fail("Your changes and the update touch the same lines. Run `claude` here and ask it to resolve the merge."); -if (!ci && spawnSync("npm", ["install"], inherit).status !== 0) process.exit(1); +if (!ci && spawnSync("npm", ["ci"], inherit).status !== 0 && spawnSync("npm", ["install"], inherit).status !== 0) process.exit(1); console.log(ci ? "\nUpdated." : "\nUpdated. Start broke again to use it. New database changes apply when it starts."); diff --git a/src/app/setup/SetupActions.tsx b/src/app/setup/SetupActions.tsx index c16937b..be360de 100644 --- a/src/app/setup/SetupActions.tsx +++ b/src/app/setup/SetupActions.tsx @@ -182,9 +182,17 @@ export function ConnectSupabaseButton({ onDone, onFail }: { onDone: () => void; onClick={async () => { setBusy(true); setMsg(""); + // Supabase opens in its own tab, so broke stays put. Opened now, while the click still counts, or it's blocked. + const tab = window.open("", "_blank"); const res = await fetch("/api/setup/supabase/connect", { method: "POST" }); const body = await res.json().catch(() => ({})); - if (body.url) return void (window.location.href = body.url); + if (body.url) { + if (tab) tab.location.href = body.url; + else window.location.href = body.url; // pop-ups blocked: this tab, as before + setMsg("Approve broke in the Supabase tab. This page updates when you're done."); + return; + } + tab?.close(); setBusy(false); if (!res.ok) { setMsg(body.error ?? "Couldn't start the Supabase sign-in."); diff --git a/src/app/setup/SetupChecklist.tsx b/src/app/setup/SetupChecklist.tsx index baf9b96..40d1a24 100644 --- a/src/app/setup/SetupChecklist.tsx +++ b/src/app/setup/SetupChecklist.tsx @@ -61,6 +61,20 @@ export function SetupChecklist({ signedIn }: { signedIn: boolean }) { } setChecking(false); }, []); + // Connect Supabase opens Supabase in its own tab. Back from Supabase, that tab passes the result here and closes itself. + useEffect(() => { + const db = new URLSearchParams(window.location.search).get("db"); + if (db && window.opener) { + try { localStorage.setItem("broke.supabase", `${db}|${Date.now()}`); } catch { /* private window: this tab just stays open */ } + window.close(); + return; + } + const onStorage = (e: StorageEvent) => { + if (e.key === "broke.supabase" && e.newValue) window.location.href = `/setup?db=${encodeURIComponent(e.newValue.split("|")[0])}`; + }; + addEventListener("storage", onStorage); + return () => removeEventListener("storage", onStorage); + }, []); useEffect(() => { load(); }, [load]); diff --git a/src/lib/supabase-mcp.ts b/src/lib/supabase-mcp.ts index e470717..c90567e 100644 --- a/src/lib/supabase-mcp.ts +++ b/src/lib/supabase-mcp.ts @@ -132,14 +132,18 @@ export const accountConnected = () => { }; export type Project = { id: string; name: string; region?: string; status?: string }; +// Supabase's list tools wrap the list: {"projects": [...]}, {"organizations": [...]}. A bare list works too. +const listOf = (text: string, key: string): T[] => { + const raw = JSON.parse(text) as T[] | Record; + return Array.isArray(raw) ? raw : raw[key] ?? []; +}; /** The projects this Supabase account can see. */ export async function listProjects(): Promise { const s = load(); const client = await connectClient(s); try { - const raw = JSON.parse(unwrap(await call(client, "list_projects", {}))) as Project[] | { projects?: Project[] }; - return (Array.isArray(raw) ? raw : raw.projects ?? []).map((p) => ({ id: p.id, name: p.name, region: p.region, status: p.status })); + return listOf(unwrap(await call(client, "list_projects", {})), "projects").map((p) => ({ id: p.id, name: p.name, region: p.region, status: p.status })); } finally { await client.close().catch(() => {}); } @@ -174,19 +178,26 @@ export async function createProject(region = "us-east-1") { const s = load(); const client = await connectClient(s); try { - const orgs = JSON.parse(unwrap(await call(client, "list_organizations", {}))) as { id: string }[]; + const orgs = listOf<{ id: string }>(unwrap(await call(client, "list_organizations", {})), "organizations"); if (!orgs.length) throw new Error("This Supabase account has no organization yet. Open supabase.com once to make one."); const pool: Record = { type: "project", organization_id: orgs[0].id, name: "broke", region, recurrence: "monthly" }; // Supabase asks to confirm the cost first. A free organization's project costs $0; anything else is the user's call. const cost = await callFit(client, "get_cost", pool); const amount = Number(cost.match(/"amount"\s*:\s*([\d.]+)/)?.[1] ?? cost.match(/\$\s*([\d.]+)/)?.[1] ?? 0); if (amount > 0) throw new Error("A new project would cost money on this account. Make one yourself at supabase.com instead."); - pool.amount = amount; - const confirm = await callFit(client, "confirm_cost", pool); - pool.confirm_cost_id = (() => { try { return (JSON.parse(confirm) as { confirm_cost_id?: string; id?: string }).confirm_cost_id ?? JSON.parse(confirm).id; } catch { return confirm.match(/[A-Za-z0-9_-]{16,}/)?.[0] ?? confirm.trim(); } })(); - const made = JSON.parse(await callFit(client, "create_project", pool)) as Project; + // Free: Supabase's create_project says to call it without a cost confirmation unless it asks for one. + const one = (t: string) => { const r = JSON.parse(t) as Project & { project?: Project }; return r.project ?? r; }; + let made: Project; + try { + made = one(await callFit(client, "create_project", pool)); + } catch (e) { + if (!/confirm/i.test(e instanceof Error ? e.message : "")) throw e; + const confirm = await callFit(client, "confirm_cost", { ...pool, amount }); + pool.confirm_cost_id = (() => { try { const c = JSON.parse(confirm) as { confirm_cost_id?: string; id?: string }; return c.confirm_cost_id ?? c.id; } catch { return confirm.match(/[A-Za-z0-9_-]{16,}/)?.[0] ?? confirm.trim(); } })(); + made = one(await callFit(client, "create_project", pool)); + } for (let i = 0; i < 60; i++) { - const p = JSON.parse(await callFit(client, "get_project", { id: made.id, project_id: made.id })) as Project; + const p = one(await callFit(client, "get_project", { id: made.id, project_id: made.id })); if (p.status === "ACTIVE_HEALTHY") return made.id; await new Promise((r) => setTimeout(r, 5000)); }