From e5ec50e8ce91f6f274faf0e93a9d04bb8400af63 Mon Sep 17 00:00:00 2001 From: ivan-aksamentov Date: Tue, 16 Jun 2020 01:12:23 +0200 Subject: [PATCH] fix: adjust io versioning for schema 2.1.0 --- config/nodemon/dev.json | 1 + config/nodemon/prod.json | 1 + .../{v2.0.0 => v2.1.0}/serialize.ts | 0 src/io/serialization/versioning.ts | 4 ++-- tools/generateTypes.ts | 23 +++++++++++++++---- 5 files changed, 23 insertions(+), 6 deletions(-) rename src/io/serialization/{v2.0.0 => v2.1.0}/serialize.ts (100%) diff --git a/config/nodemon/dev.json b/config/nodemon/dev.json index 62f4e75fb..52cc75322 100644 --- a/config/nodemon/dev.json +++ b/config/nodemon/dev.json @@ -17,6 +17,7 @@ "postcss.config.js", "schemas", "stylelint.config.js", + "tools/generateTypes.ts", "tsconfig.json", "tslint.json", "webpack.config.js", diff --git a/config/nodemon/prod.json b/config/nodemon/prod.json index b823e0cc9..46fbe7f5e 100644 --- a/config/nodemon/prod.json +++ b/config/nodemon/prod.json @@ -19,6 +19,7 @@ "src", "static", "stylelint.config.js", + "tools/generateTypes.ts", "tsconfig.json", "tslint.json", "webpack.config.js", diff --git a/src/io/serialization/v2.0.0/serialize.ts b/src/io/serialization/v2.1.0/serialize.ts similarity index 100% rename from src/io/serialization/v2.0.0/serialize.ts rename to src/io/serialization/v2.1.0/serialize.ts diff --git a/src/io/serialization/versioning.ts b/src/io/serialization/versioning.ts index 42a748fdf..72b442086 100644 --- a/src/io/serialization/versioning.ts +++ b/src/io/serialization/versioning.ts @@ -8,7 +8,7 @@ import type { ScenarioParameters } from '../../algorithms/types/Param.types' import { getOrThrow } from '../../helpers/getOrThrow' -import v2_0_0 from './v2.0.0/serialize' +import v2_1_0 from './v2.1.0/serialize' export type Serializer = (input: ScenarioParameters) => string export type Deserializer = (input: string) => ScenarioParameters @@ -20,7 +20,7 @@ export interface Serializers { export const SERIALIZERS = new Map( Object.entries({ - ...v2_0_0, + ...v2_1_0, }), ) diff --git a/tools/generateTypes.ts b/tools/generateTypes.ts index 48127366d..ca7b25ccb 100644 --- a/tools/generateTypes.ts +++ b/tools/generateTypes.ts @@ -8,13 +8,16 @@ import yaml from 'js-yaml' import path from 'path' import prettier from 'prettier' import { quicktype, InputData, JSONSchema, JSONSchemaInput, JSONSchemaStore, parseJSON } from 'quicktype-core' +import { valid } from 'semver' import util from 'util' import { findModuleRoot } from '../lib/findModuleRoot' +import { get } from 'lodash' const rimraf = util.promisify(rimrafOriginal) const SCHEMA_EXTENSION = '.yml' +const SCHEMA_VER_FILEPATH = 'schemas/_SchemaVer.yml' class Store extends JSONSchemaStore { private schemasRoot: string @@ -42,6 +45,7 @@ function quicktypesAddSources(schemasRoot: string, schemaInput: JSONSchemaInput) async function quicktypesGenerate( lang: string, + schemaVer: string, schemasRoot: string, schemaFilenames: string[], outputPath: string, @@ -56,8 +60,6 @@ async function quicktypesGenerate( const { lines } = await quicktype({ inputData, lang, rendererOptions }) let code = lines.join('\n') - const schemaVer = '2.0.0' - if (lang === 'typescript') { code = prettier.format(code, { parser: 'typescript' }) @@ -105,6 +107,16 @@ async function ajvGenerate(schemasRoot: string, schemaFilenames: string[], outpu return FA.concurrent.forEach(ajvGenerateOne(schemasRoot, ajv, outputDir), schemaFilenames) } +async function getSchemaVer(schemaVerFilepath: string) { + const jsonSchemaString = fs.readFileSync(schemaVerFilepath).toString('utf-8') + const schema = yaml.safeLoad(jsonSchemaString) as Record + const schemaVer = get(schema, 'const') as string + if (!valid(schemaVer)) { + throw new Error(`Expected \`schemaVer\` to be a valid version string but got: "${schemaVer}"`) + } + return schemaVer +} + export default async function generateTypes() { const { moduleRoot } = findModuleRoot() const schemasRoot = path.join(moduleRoot, 'schemas') @@ -112,6 +124,9 @@ export default async function generateTypes() { const pyOutputDir = path.join(moduleRoot, 'data', 'generated') const tsOutput = path.join(tsOutputDir, 'types.ts') const pyOutput = path.join(pyOutputDir, 'types.py') + const schemaVerFilepath = path.join(moduleRoot, SCHEMA_VER_FILEPATH) + + const schemaVer = await getSchemaVer(schemaVerFilepath) let schemaFilenames = await fs.readdir(schemasRoot) schemaFilenames = schemaFilenames.filter((schemaFilename) => schemaFilename.endsWith(SCHEMA_EXTENSION)) @@ -120,12 +135,12 @@ export default async function generateTypes() { await FA.concurrent.forEach(async (d) => fs.mkdirp(d), [tsOutputDir, pyOutputDir]) return Promise.all([ - quicktypesGenerate('typescript', schemasRoot, schemaFilenames, tsOutput, { + quicktypesGenerate('typescript', schemaVer, schemasRoot, schemaFilenames, tsOutput, { 'converters': 'all-objects', 'nice-property-names': 'true', 'runtime-typecheck': 'true', }), - quicktypesGenerate('python', schemasRoot, schemaFilenames, pyOutput, { + quicktypesGenerate('python', schemaVer, schemasRoot, schemaFilenames, pyOutput, { 'python-version': '3.6', 'alphabetize-properties': 'false', }),