import { mkdir, readFile, writeFile } from "node:fs/promises"; import { resolve } from "node:path"; import { Ajv } from "ajv"; import standaloneCode from "ajv/dist/standalone/index.js"; import { helloCapabilitiesSchema, requestSchema, responseSchema, } from "../src/protocol/schema-definitions.js"; const root = resolve(import.meta.dirname, ".."); const manifestSchema = JSON.parse( await readFile(resolve(root, "src/conformance/adapter-manifest.schema.json"), "utf8"), ) as Record; const customSuiteManifestSchema = JSON.parse( await readFile(resolve(root, "src/custom/suite-manifest.schema.json"), "utf8"), ) as Record; const ajv = new Ajv({ allErrors: true, strict: true, code: { source: true, esm: true, optimize: true }, }); ajv.addSchema(requestSchema, "adapter-request"); ajv.addSchema(responseSchema, "adapter-response"); ajv.addSchema(helloCapabilitiesSchema, "adapter-hello-capabilities"); ajv.addSchema(manifestSchema, "adapter-manifest"); ajv.addSchema(customSuiteManifestSchema, "custom-suite-manifest"); const ajvRuntimeUnicodeLength = /const (func\d+) = require\("ajv\/dist\/runtime\/ucs2length"\)\.default;/g; const inlineUnicodeLength = (name: string) => `function ${name}(value){let length=0;for(let index=0;index=0xd800&&code<=0xdbff&&index+1 inlineUnicodeLength(name)); if (generated.includes('require("ajv/')) { const dependencies = generated.match(/require\("ajv\/[^"]+"\)/g) ?? []; throw new Error( `Generated validators contain unexpected Ajv runtime dependencies: ${[ ...new Set(dependencies), ].join(", ")}`, ); } const header = `// Generated by scripts/generate-validators.ts. Do not edit.\n// @ts-nocheck\n`; const output = `${header}${generated}\n`; const outputDirectory = resolve(root, "src/generated"); const outputPath = resolve(outputDirectory, "validators.ts"); if (process.argv.includes("--check")) { let existing: string; try { existing = await readFile(outputPath, "utf8"); } catch { throw new Error("Generated validators are missing; run pnpm generate:validators"); } if (existing !== output) { throw new Error("Generated validators are stale; run pnpm generate:validators"); } } else { await mkdir(outputDirectory, { recursive: true }); await writeFile(outputPath, output, "utf8"); }