feat(shell): add codegen

This commit is contained in:
Il Harper 2024-03-04 01:43:15 +08:00
parent 4b7264ff25
commit bff08af186
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
3 changed files with 86 additions and 0 deletions

1
packages/shell/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
generated/

View File

@ -0,0 +1,75 @@
import { dump, load } from 'js-yaml'
import { readFile, writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import type { Definition, PartialArgs } from 'typescript-json-schema'
import { buildGenerator, programFromConfig } from 'typescript-json-schema'
const settings: PartialArgs = {
aliasRef: true,
titles: false,
defaultProps: true,
noExtraProps: true,
propOrder: false,
required: true,
strictNullChecks: true,
skipLibCheck: true,
}
void (async () => {
console.log('Building generator...')
const generator = buildGenerator(
programFromConfig(resolve(__dirname, 'tsconfig.entity.json')),
settings,
)!
const schemas = generator.getUserSymbols().map((x) => ({
$id: x,
...generator.getSchemaForSymbol(x),
}))
await writeFile(
resolve(__dirname, '../generated/schemas.json'),
JSON.stringify(schemas, null, 2),
)
console.log('Generating schemas for codegen...')
console.log('Modifying schemas for codegen...')
console.log('Writing schemas for codegen...')
console.log('Generating schemas for openapi...')
let oapiDefinitions = generator.getSchemaForSymbols(
generator.getUserSymbols(),
)
console.log('Modifying schemas for openapi...')
oapiDefinitions = JSON.parse(
JSON.stringify(oapiDefinitions)
.replaceAll(
'"anyOf"',
'"type":"object","discriminator":{"propertyName":"type"},"oneOf"',
)
.replaceAll('#/definitions/', '#/components/schemas/'),
) as Definition
console.log('Writing ChronocatConfig for openapi...')
const openapiSource = load(
await readFile(resolve(__dirname, '../static/openapi.yaml'), 'utf-8'),
) as {
components: {
schemas: unknown
}
}
openapiSource.components.schemas = oapiDefinitions.definitions
await writeFile(
resolve(__dirname, '../../docs/static/openapi.yaml'),
dump(openapiSource),
)
})()

View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es5",
},
"include": [
"../src/config/configEntity.ts",
"../src/satori/types/satoriEntity.ts",
"../src/satori/types/satoriPayloadEntity.ts",
]
}