|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import path from "node:path"; |
| 4 | +import { cp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | +import { generate } from "../src/generate.js"; |
| 7 | + |
| 8 | +const root = path.join(import.meta.dirname, "..", "..", ".."); |
| 9 | +const providersPath = path.join(root, "providers"); |
| 10 | + |
| 11 | +const diffOutput = await Bun.$`git diff --name-only HEAD -- providers`.cwd(root).text(); |
| 12 | +const changedProviderPaths = diffOutput |
| 13 | + .split("\n") |
| 14 | + .filter(Boolean) |
| 15 | + .filter((filePath) => /^providers\/[^/]+\/models\/.+\.toml$/.test(filePath)); |
| 16 | + |
| 17 | +if (changedProviderPaths.length === 0) { |
| 18 | + process.exit(0); |
| 19 | +} |
| 20 | + |
| 21 | +const baselineRoot = path.join(tmpdir(), `models-dev-compare-${Date.now()}`); |
| 22 | +await mkdir(baselineRoot, { recursive: true }); |
| 23 | + |
| 24 | +try { |
| 25 | + const baselineProvidersPath = path.join(baselineRoot, "providers"); |
| 26 | + await cp(providersPath, baselineProvidersPath, { recursive: true }); |
| 27 | + |
| 28 | + for (const filePath of changedProviderPaths) { |
| 29 | + const tempFilePath = path.join(baselineRoot, filePath); |
| 30 | + const show = Bun.spawn(["git", "show", `HEAD:${filePath}`], { |
| 31 | + cwd: root, |
| 32 | + stdout: "pipe", |
| 33 | + stderr: "pipe", |
| 34 | + }); |
| 35 | + const exitCode = await show.exited; |
| 36 | + if (exitCode !== 0) { |
| 37 | + await rm(tempFilePath, { force: true }); |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + const contents = await new Response(show.stdout).text(); |
| 42 | + await mkdir(path.dirname(tempFilePath), { recursive: true }); |
| 43 | + await writeFile(tempFilePath, contents); |
| 44 | + } |
| 45 | + |
| 46 | + const before = await generate(baselineProvidersPath); |
| 47 | + const after = await generate(providersPath); |
| 48 | + |
| 49 | + for (const filePath of changedProviderPaths) { |
| 50 | + const match = /^providers\/([^/]+)\/models\/(.+)\.toml$/.exec(filePath); |
| 51 | + if (!match) continue; |
| 52 | + |
| 53 | + const [, providerID, modelID] = match; |
| 54 | + const beforeModel = before[providerID]?.models[modelID]; |
| 55 | + const afterModel = after[providerID]?.models[modelID]; |
| 56 | + const beforeJson = JSON.stringify(beforeModel, null, 2); |
| 57 | + const afterJson = JSON.stringify(afterModel, null, 2); |
| 58 | + |
| 59 | + if (beforeJson === afterJson) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + const beforeFilePath = path.join(baselineRoot, "before.json"); |
| 64 | + const afterFilePath = path.join(baselineRoot, "after.json"); |
| 65 | + await writeFile(beforeFilePath, `${beforeJson}\n`); |
| 66 | + await writeFile(afterFilePath, `${afterJson}\n`); |
| 67 | + |
| 68 | + const diff = Bun.spawn( |
| 69 | + [ |
| 70 | + "diff", |
| 71 | + "-u", |
| 72 | + "-L", |
| 73 | + `${filePath} (before)`, |
| 74 | + "-L", |
| 75 | + `${filePath} (after)`, |
| 76 | + beforeFilePath, |
| 77 | + afterFilePath, |
| 78 | + ], |
| 79 | + { |
| 80 | + stdout: "pipe", |
| 81 | + stderr: "pipe", |
| 82 | + }, |
| 83 | + ); |
| 84 | + const output = await new Response(diff.stdout).text(); |
| 85 | + process.stdout.write(output); |
| 86 | + } |
| 87 | +} finally { |
| 88 | + await rm(baselineRoot, { recursive: true, force: true }); |
| 89 | +} |
0 commit comments