|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const REQUIRED_PROP_FILES = [ |
| 6 | + "ag-grid-theme-props.json", |
| 7 | + "core-props.json", |
| 8 | + "countries-props.json", |
| 9 | + "data-grid-props.json", |
| 10 | + "embla-carousel-props.json", |
| 11 | + "icons-props.json", |
| 12 | + "lab-props.json", |
| 13 | + "react-resizable-panel-theme-props.json", |
| 14 | +]; |
| 15 | + |
| 16 | +async function pathExists(targetPath) { |
| 17 | + try { |
| 18 | + await fs.access(targetPath); |
| 19 | + return true; |
| 20 | + } catch { |
| 21 | + return false; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function getMissingInputs(repoRoot) { |
| 26 | + const missing = []; |
| 27 | + const searchDataPath = path.join( |
| 28 | + repoRoot, |
| 29 | + "site", |
| 30 | + "public", |
| 31 | + "search-data.json", |
| 32 | + ); |
| 33 | + const snapshotRoot = path.join( |
| 34 | + repoRoot, |
| 35 | + "site", |
| 36 | + "snapshots", |
| 37 | + "latest", |
| 38 | + "salt", |
| 39 | + ); |
| 40 | + const propsDir = path.join(repoRoot, "site", "src", "props"); |
| 41 | + |
| 42 | + if (!(await pathExists(searchDataPath))) { |
| 43 | + missing.push(searchDataPath); |
| 44 | + } |
| 45 | + if (!(await pathExists(snapshotRoot))) { |
| 46 | + missing.push(snapshotRoot); |
| 47 | + } |
| 48 | + |
| 49 | + for (const fileName of REQUIRED_PROP_FILES) { |
| 50 | + const filePath = path.join(propsDir, fileName); |
| 51 | + if (!(await pathExists(filePath))) { |
| 52 | + missing.push(filePath); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return missing; |
| 57 | +} |
| 58 | + |
| 59 | +function runYarnGenSnapshot(repoRoot) { |
| 60 | + const command = process.platform === "win32" ? "yarn.cmd" : "yarn"; |
| 61 | + const result = spawnSync( |
| 62 | + command, |
| 63 | + ["workspace", "@salt-ds/site", "gen:snapshot"], |
| 64 | + { |
| 65 | + cwd: repoRoot, |
| 66 | + stdio: "inherit", |
| 67 | + env: process.env, |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + if (result.status !== 0) { |
| 72 | + throw new Error("Failed to generate required Salt site artifacts for MCP."); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export async function ensureSiteBuildInputs(repoRoot) { |
| 77 | + const missingBefore = await getMissingInputs(repoRoot); |
| 78 | + if (missingBefore.length === 0) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + console.error( |
| 83 | + "Missing Salt site artifacts required for MCP registry build. Regenerating with `yarn workspace @salt-ds/site gen:snapshot`.", |
| 84 | + ); |
| 85 | + |
| 86 | + await fs.rm(path.join(repoRoot, "site", "snapshots", "latest"), { |
| 87 | + recursive: true, |
| 88 | + force: true, |
| 89 | + }); |
| 90 | + await fs.rm(path.join(repoRoot, "site", "public", "search-data.json"), { |
| 91 | + force: true, |
| 92 | + }); |
| 93 | + |
| 94 | + runYarnGenSnapshot(repoRoot); |
| 95 | + |
| 96 | + const missingAfter = await getMissingInputs(repoRoot); |
| 97 | + if (missingAfter.length > 0) { |
| 98 | + throw new Error( |
| 99 | + `Salt site artifact generation completed, but required MCP inputs are still missing:\n${missingAfter.join("\n")}`, |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments