import { execFileSync } from "node:child_process"; import { createHash } from "node:crypto"; import { constants, copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname, isAbsolute, join, normalize, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; import { developmentInstanceIdForWorktree } from "../src/main/development-profile"; import { type DevelopmentStateOutcome, ensureDevelopmentState } from "./development-secrets"; const scriptsRoot = dirname(fileURLToPath(import.meta.url)); export const developmentProjectRoot = dirname(scriptsRoot); export type DevelopmentCommandRunner = ( executable: string, args: string[], options: { cwd: string; stdio: "inherit"; env?: NodeJS.ProcessEnv }, ) => void; export const supportedBunVersion = "1.4.2"; const INSTALL_STAMP = join("node_modules", ".openbot-install-stamp"); const LOCAL_D1_STATE = join("apps", "auth-api", ".wrangler", "state", "v3", "d1"); const MIGRATION_STAMP = join(LOCAL_D1_STATE, ".openbot-migrate-local-stamp"); const MIGRATIONS_DIRECTORY = join("apps", "auth-api", "migrations"); export interface DevelopmentPreparationInput { projectRoot?: string; mainCheckoutRoot?: string; executable?: string; bunVersion?: string; run?: DevelopmentCommandRunner; } export function prepareDevelopmentEnvironment(input: DevelopmentPreparationInput = {}): DevelopmentStateOutcome { const projectRoot = input.projectRoot ?? developmentProjectRoot; assertSupportedBunVersion(input.bunVersion ?? process.versions.bun ?? "unknown"); const copied = copyWorktreeIncludes(projectRoot, input.mainCheckoutRoot ?? findMainCheckoutRoot(projectRoot)); for (const path of copied) process.stdout.write(`Copied ${path} from the main checkout.\n`); const stateOutcome = ensureDevelopmentState(projectRoot); const executable = input.executable ?? process.execPath; const run = input.run ?? execDevelopmentCommand; const options = { cwd: projectRoot, stdio: "inherit" as const }; runUnlessStamped( join(projectRoot, INSTALL_STAMP), () => installFingerprint(projectRoot), () => run(executable, ["install", "--frozen-lockfile"], options), ); runUnlessStamped( join(projectRoot, MIGRATION_STAMP), () => (existsSync(join(projectRoot, LOCAL_D1_STATE)) ? migrationFingerprint(projectRoot) : null), () => run(executable, ["run", "api:migrate:local"], options), ); return stateOutcome; } function runUnlessStamped(stampPath: string, fingerprint: () => string | null, command: () => void): void { const before = fingerprint(); if (before !== null && readStamp(stampPath) === before) return; command(); const after = fingerprint(); if (after !== null) writeStamp(stampPath, after); } function writeStamp(path: string, fingerprint: string): void { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, `${fingerprint}\n`); } export function writeInstallStamp(projectRoot = developmentProjectRoot): void { writeStamp(join(projectRoot, INSTALL_STAMP), installFingerprint(projectRoot)); } function readStamp(path: string): string | null { try { return readFileSync(path, "utf8").trim(); } catch { return null; } } export function installFingerprint(projectRoot: string): string { const files = ["bun.lock", "bunfig.toml", "package.json", ...workspaceManifests(projectRoot)]; const patches = join(projectRoot, "patches"); if (existsSync(patches)) { for (const entry of readdirSync(patches, { withFileTypes: true })) { if (entry.isFile()) files.push(join("patches", entry.name)); } } return fingerprintFiles(projectRoot, files, [`bun ${process.versions.bun ?? "unknown"}`]); } export function migrationFingerprint(projectRoot: string): string { const directory = join(projectRoot, MIGRATIONS_DIRECTORY); const names = existsSync(directory) ? readdirSync(directory) : []; return fingerprintFiles( projectRoot, names.map((name) => join(MIGRATIONS_DIRECTORY, name)), [], ); } function workspaceManifests(projectRoot: string): string[] { let patterns: unknown; try { patterns = JSON.parse(readFileSync(join(projectRoot, "package.json"), "utf8")).workspaces?.packages; } catch { return []; } if (!Array.isArray(patterns)) return []; const manifests: string[] = []; for (const pattern of patterns) { if (typeof pattern !== "string") continue; if (!pattern.endsWith("/*")) { manifests.push(join(pattern, "package.json")); continue; } const parent = pattern.slice(0, -2); if (!existsSync(join(projectRoot, parent))) continue; for (const entry of readdirSync(join(projectRoot, parent), { withFileTypes: true })) { if (entry.isDirectory()) manifests.push(join(parent, entry.name, "package.json")); } } return manifests; } function fingerprintFiles(projectRoot: string, files: string[], extra: string[]): string { const hash = createHash("sha256"); for (const line of extra) hash.update(`${line}\n`); for (const file of [...files].sort()) { const path = join(projectRoot, file); hash.update(`${file}\n`); hash.update(existsSync(path) ? readFileSync(path) : "missing"); hash.update("\n"); } return hash.digest("hex"); } export function prepareDevelopmentWorktree(input: DevelopmentPreparationInput = {}): DevelopmentStateOutcome { const projectRoot = input.projectRoot ?? developmentProjectRoot; const stateOutcome = prepareDevelopmentEnvironment({ ...input, projectRoot }); const executable = input.executable ?? process.execPath; const run = input.run ?? execDevelopmentCommand; const options = { cwd: projectRoot, stdio: "inherit" as const }; const instanceId = developmentInstanceIdForWorktree(projectRoot); run(executable, ["run", "dev:seed", "--if-missing"], { ...options, env: { ...process.env, OPENBOT_DEV_INSTANCE_ID: instanceId }, }); run(executable, ["run", "marketplace:seed:local"], options); return stateOutcome; } export function copyWorktreeIncludes(projectRoot: string, mainCheckoutRoot: string | undefined): string[] { if (!mainCheckoutRoot || resolve(mainCheckoutRoot) === resolve(projectRoot)) return []; const listPath = join(projectRoot, ".worktreeinclude"); if (!existsSync(listPath)) return []; const copied: string[] = []; for (const line of readFileSync(listPath, "utf8").split("\n")) { const path = line.trim(); if (!path || path.startsWith("#") || !isInsideCheckout(path)) continue; const source = join(mainCheckoutRoot, path); const target = join(projectRoot, path); if (!existsSync(source) || existsSync(target)) continue; mkdirSync(dirname(target), { recursive: true }); copyFileSync(source, target, constants.COPYFILE_EXCL); copied.push(path); } return copied; } function isInsideCheckout(path: string): boolean { const normalized = normalize(path); return !isAbsolute(normalized) && normalized !== ".." && !normalized.startsWith(`..${sep}`); } function findMainCheckoutRoot(projectRoot: string): string | undefined { try { const commonDir = execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], { cwd: projectRoot, encoding: "utf8", }).trim(); return dirname(commonDir); } catch { return undefined; } } export function assertSupportedBunVersion(version: string): void { if (version === supportedBunVersion) return; throw new Error( `Unsupported Bun ${version}. OpenBot development requires stable Bun ${supportedBunVersion}. Install the exact version with the command in https://github.com/nightly-labs/openbot#development, then retry.`, ); } function execDevelopmentCommand( executable: string, args: string[], options: { cwd: string; stdio: "inherit"; env?: NodeJS.ProcessEnv }, ): void { execFileSync(executable, args, options); } if (import.meta.main) { if (prepareDevelopmentWorktree() === "created") { process.stdout.write("Prepared local development state.\n"); } }