#!/usr/bin/env node import * as path from 'path'; import { compile, compileFromFile } from 'json-schema-to-typescript'; import { readFile, writeFile } from 'fs/promises'; /** * Schemas bundled with `@squiz/dx-json-schema-lib` compile via compileFromFile. * `v1.json` references `http://json-schema.org/draft-07/schema#` internally, so regeneration * may require outbound network depending on resolver behavior / environment. */ const files = ['src/manifest/v1/v1.json', 'src/manifest/v1/JobV1.json', 'src/formatted-text/v1/formattedText.json']; /** * User API manifest schema has only local `#/definitions` refs — compile in memory after * dropping `$schema` so json-schema-ref-parser never fetches draft-07 over HTTP. */ const filesCompileInMemoryWithoutRootSchema = ['src/manifest/userApi/v1/UserApiManifestV1.json']; (async function main() { for (const file of filesCompileInMemoryWithoutRootSchema) { const fullPath = path.resolve(__dirname, file); const directory = path.dirname(fullPath); const filename = path.basename(fullPath, '.json'); const raw = await readFile(fullPath, 'utf8'); type SchemaRecord = Record; const schemaObj = JSON.parse(raw) as SchemaRecord & { title?: string }; delete schemaObj.$schema; const rootName = typeof schemaObj.title === 'string' && schemaObj.title.trim() !== '' ? schemaObj.title : filename; const output = await compile(schemaObj as never, rootName, { cwd: directory, ignoreMinAndMaxItems: true, style: { singleQuote: true }, }); await writeFile(path.join(directory, `${filename}.ts`), output); } for (const file of files) { const fullPath = path.resolve(__dirname, file); const directory = path.dirname(fullPath); const filename = path.basename(fullPath, '.json'); const output = await compileFromFile(fullPath, { cwd: directory, ignoreMinAndMaxItems: true, style: { singleQuote: true }, }); await writeFile(path.join(directory, `${filename}.ts`), output); } })();