23 lines
1.2 KiB
JavaScript
23 lines
1.2 KiB
JavaScript
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from 'quicktype-core';
|
|
|
|
const schema = await readFile(new URL('../schemas/protocol.json', import.meta.url), 'utf8');
|
|
const input = new JSONSchemaInput(new FetchingJSONSchemaStore());
|
|
await input.addSource({ name: 'Protocol', schema });
|
|
const inputData = new InputData();
|
|
inputData.addInput(input);
|
|
for (const [lang, path, rendererOptions] of [
|
|
['go', '../server/internal/protocol/generated.go', { package: 'protocol', 'just-types': 'true' }],
|
|
['typescript', '../client/src/generated.ts', { 'just-types': 'true' }],
|
|
]) {
|
|
const result = await quicktype({ inputData, lang, rendererOptions, alphabetizeProperties: true });
|
|
const output = '// Code generated from schemas/protocol.json. DO NOT EDIT.\n' + (lang === 'go' ? 'package protocol\n\n' : '') + result.lines.join('\n').trimEnd() + '\n';
|
|
const url = new URL(path, import.meta.url);
|
|
if (process.argv.includes('--check')) {
|
|
if (await readFile(url, 'utf8') !== output) throw new Error(`Stale generated contract: ${path}`);
|
|
} else {
|
|
await mkdir(new URL('.', url), { recursive: true });
|
|
await writeFile(url, output);
|
|
}
|
|
}
|