Skip to content

Commit

Permalink
fix: adjust io versioning for schema 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-aksamentov committed Jun 15, 2020
1 parent a7fa794 commit e5ec50e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/nodemon/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"postcss.config.js",
"schemas",
"stylelint.config.js",
"tools/generateTypes.ts",
"tsconfig.json",
"tslint.json",
"webpack.config.js",
Expand Down
1 change: 1 addition & 0 deletions config/nodemon/prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"src",
"static",
"stylelint.config.js",
"tools/generateTypes.ts",
"tsconfig.json",
"tslint.json",
"webpack.config.js",
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/io/serialization/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,7 @@ export interface Serializers {

export const SERIALIZERS = new Map<string, Serializers>(
Object.entries({
...v2_0_0,
...v2_1_0,
}),
)

Expand Down
23 changes: 19 additions & 4 deletions tools/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,6 +45,7 @@ function quicktypesAddSources(schemasRoot: string, schemaInput: JSONSchemaInput)

async function quicktypesGenerate(
lang: string,
schemaVer: string,
schemasRoot: string,
schemaFilenames: string[],
outputPath: string,
Expand All @@ -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' })

Expand Down Expand Up @@ -105,13 +107,26 @@ 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<string, unknown>
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')
const tsOutputDir = path.join(moduleRoot, 'src', '.generated', 'latest')
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))
Expand All @@ -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',
}),
Expand Down

0 comments on commit e5ec50e

Please sign in to comment.