import { readFile } from "node:fs/promises"; import path from "node:path"; import type { HostEnvironment } from "../host/index.js"; import { discoverAllMigrations } from "../migrations/index.js"; import type { ModuleMigrationAggregate } from "../modules/index.js"; import { resolveCommandModulesDirectory } from "./modules-directory-environment.js"; interface PackageVersion { readonly version?: unknown; } async function readPackageVersion(packagePath: string): Promise { const packageData = JSON.parse(await readFile(packagePath, "utf8")) as PackageVersion; if (typeof packageData.version !== "string") { throw new TypeError("A required workspace package does not declare a version."); } return packageData.version; } /** * Core's migrations come from the platform (`/core/drizzle`); installed modules' * come from the configured modules directory, wherever it is (ADR 0048). Neither is ever derived * from the other. */ export async function discoverRepositoryMigrations( repositoryRoot = process.cwd(), environment: HostEnvironment = process.env, ): Promise { const modulesDirectory = resolveCommandModulesDirectory(environment, repositoryRoot); const [platformVersion, moduleSdkVersion] = await Promise.all([ readPackageVersion(path.join(repositoryRoot, "package.json")), readPackageVersion(path.join(repositoryRoot, "packages/module-sdk/package.json")), ]); return discoverAllMigrations({ coreMigrationsDirectory: path.join(repositoryRoot, "core", "drizzle"), moduleDiscovery: { moduleSdkVersion, ...modulesDirectory, platformVersion, }, }); }