import prettierOptions from '@coast/project-config/prettier.json'; import { writeFileSync } from 'fs'; import { compile, Options } from 'json-schema-to-typescript'; import path from 'path'; import { makeSchemaMap } from './makeSchemaMap'; // tslint:disable: no-console interface Resolver { order?: number; canRead: any; read(file: any, callback: (error?: Error, value?: string) => void): void; } const makeLocalResolver = (schemaMap: Map): Resolver => ({ order: 1, canRead: /.*/, // tslint:disable-next-line: typedef read(file, callback) { console.log(` > ${file.url}`); for (const schema of schemaMap.values()) { if (schema.$id === file.url) { callback(undefined, schema); return; } } callback(Error('not found')); }, }); const makeOptions = (resolver: Resolver): Partial => ({ style: prettierOptions as any, format: true, $refOptions: { resolve: { http: resolver } as any }, declareExternallyReferenced: true, unknownAny: false, enableConstEnums: false, additionalProperties: false, ignoreMinAndMaxItems: true, }); async function main(srcDir: string, indexPath: string, outPath: string): Promise { const schemaMap = await makeSchemaMap(srcDir); const options = makeOptions(makeLocalResolver(schemaMap)); console.log(`Compiling...`, options); const indexFile = await compile(require(path.join(process.cwd(), indexPath)).default, 'index', options); console.log(`Writing ${outPath}...`); writeFileSync(outPath, indexFile); } // tslint:disable-next-line: no-floating-promises main('src/schemas/card', 'src/schemas', 'src/core-lib/schema.ts');