mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-16 12:51:39 +00:00
feat(shell): add codegen
This commit is contained in:
parent
4b7264ff25
commit
bff08af186
1
packages/shell/.gitignore
vendored
Normal file
1
packages/shell/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
generated/
|
75
packages/shell/scripts/codegen.cts
Normal file
75
packages/shell/scripts/codegen.cts
Normal 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),
|
||||
)
|
||||
})()
|
10
packages/shell/scripts/tsconfig.entity.json
Normal file
10
packages/shell/scripts/tsconfig.entity.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
},
|
||||
"include": [
|
||||
"../src/config/configEntity.ts",
|
||||
"../src/satori/types/satoriEntity.ts",
|
||||
"../src/satori/types/satoriPayloadEntity.ts",
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user