1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-21 15:51:49 +03:00
leon/scripts/generate/generate-json-schemas.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-01-06 23:18:19 +03:00
import fs from 'node:fs'
import path from 'node:path'
import { LogHelper } from '@/helpers/log-helper'
import {
domainSchemaObject,
skillSchemaObject,
skillConfigSchemaObject
} from '@/schemas/skill-schemas'
import {
globalEntitySchemaObject,
globalResolverSchemaObject,
globalAnswersSchemaObject
} from '@/schemas/global-data-schemas'
import {
amazonVoiceConfiguration,
googleCloudVoiceConfiguration,
watsonVoiceConfiguration
} from '@/schemas/voice-config-schemas'
/**
* Generate JSON schemas
* @param {string} categoryName
* @param {Map<string, Object>} schemas
2023-01-06 23:18:19 +03:00
*/
export const generateSchemas = async (categoryName, schemas) => {
const categorySchemasPath = path.join(process.cwd(), 'schemas', categoryName)
2023-04-27 15:24:16 +03:00
2023-01-06 23:18:19 +03:00
await fs.promises.mkdir(categorySchemasPath, { recursive: true })
2023-04-27 15:24:16 +03:00
2023-01-06 23:18:19 +03:00
for (const [schemaName, schemaObject] of schemas.entries()) {
const schemaPath = path.join(categorySchemasPath, `${schemaName}.json`)
2023-04-27 15:24:16 +03:00
2023-01-06 23:18:19 +03:00
await fs.promises.writeFile(
schemaPath,
JSON.stringify(
{
$schema: 'https://json-schema.org/draft-07/schema',
...schemaObject
},
null,
2
)
)
}
}
export default async () => {
LogHelper.info('Generating the JSON schemas...')
2023-04-27 15:24:16 +03:00
2023-01-06 23:18:19 +03:00
await Promise.all([
generateSchemas(
'global-data',
new Map([
['global-entity', globalEntitySchemaObject],
['global-resolver', globalResolverSchemaObject],
['global-answers', globalAnswersSchemaObject]
])
),
generateSchemas(
'skill-schemas',
new Map([
['domain', domainSchemaObject],
['skill', skillSchemaObject],
['skill-config', skillConfigSchemaObject]
])
),
generateSchemas(
'voice-config-schemas',
new Map([
['amazon', amazonVoiceConfiguration],
['google-cloud', googleCloudVoiceConfiguration],
2023-01-06 23:18:19 +03:00
['watson-stt', watsonVoiceConfiguration],
['watson-tts', watsonVoiceConfiguration]
])
)
])
2023-04-27 15:24:16 +03:00
2023-01-06 23:18:19 +03:00
LogHelper.success('JSON schemas generated')
}