import { execSync } from "node:child_process"; import fs from "node:fs/promises"; import path, { resolve } from "node:path"; export async function getManifest(filePath: string) { try { const workspace = process.env.WORKSPACE; if (!workspace) { throw new Error("WORKSPACE environment variable is not set."); } const absolutePath = resolve(workspace, filePath); return (await import(absolutePath)).default; } catch (e) { throw new Error(`Failed to load manifest file at ${filePath}: ${e}`); } } export async function getManifestFromGit(filePath: string, ref = "HEAD") { try { const workspace = process.env.WORKSPACE; if (!workspace) { throw new Error("WORKSPACE environment variable is not set."); } const gitCommand = `git show ${ref}:${filePath}`; const fileContent = execSync(gitCommand, { cwd: workspace, encoding: "utf-8", }); const tmpDir = path.join(workspace, ".tmp-datasource-check"); await fs.mkdir(tmpDir, { recursive: true }); const tmpFile = path.join(tmpDir, `${Date.now()}.ts`); await fs.writeFile(tmpFile, fileContent); const manifest = (await import(tmpFile)).default; await fs.rm(tmpDir, { recursive: true, force: true }); return manifest; } catch (e) { throw new Error(`Failed to load manifest file from git at ${ref}:${filePath}: ${e}`); } } export function getMigrationFilePath({ migrationType, suffix, forceTimestamp, }: { migrationType: "create" | "alter" | "drop"; suffix: string; forceTimestamp?: string; }) { const migrationsDir = path.join(process.cwd(), ".migrations"); const timestamp = forceTimestamp ?? new Date() .toISOString() .replace(/[-:.TZ]/g, "") .slice(0, 14); const migrationFileName = `${timestamp}_${migrationType}_${suffix}.sql`; return path.join(migrationsDir, migrationFileName); }