Add JSON schemas for DistroMeta and ServerMeta.
JSON schemas are used by editors to validate data and provide useful insights. The JSON schemas will be generated by the init root command. They can also be generated using the generate schemas command. The JSON files will reference the schemas on the user's local disk rather than hosted versions. This allows offline editing and ensures that the schema is exactly one-to-one with the local version of Nebula. Existing servers will have to manually add the schema property. To see how to do this, generate a new server and copy the $\schema value. The schema property will need to be added to any existing distrometa files. This is the same format as the server meta, just replace ServerMeta with DistroMeta. More information, including sample files with json schemas, is provided on the README.
This commit is contained in:
73
src/util/SchemaUtil.ts
Normal file
73
src/util/SchemaUtil.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { mkdirs, pathExists, remove, writeFile } from 'fs-extra'
|
||||
import { join, resolve } from 'path'
|
||||
import { createGenerator } from 'ts-json-schema-generator'
|
||||
import { URL } from 'url'
|
||||
import { DistroMeta } from '../model/nebula/distrometa'
|
||||
import { ServerMeta } from '../model/nebula/servermeta'
|
||||
import { LoggerUtil } from './LoggerUtil'
|
||||
|
||||
const logger = LoggerUtil.getLogger('SchemaUtil')
|
||||
|
||||
interface SchemaType {
|
||||
/**
|
||||
* URL to the JSON schema for this type of file.
|
||||
* This is used by editors to validate and annotate the data.
|
||||
*/
|
||||
$schema?: string
|
||||
}
|
||||
|
||||
export type DistroMetaSchema = DistroMeta & SchemaType
|
||||
export type ServerMetaSchema = ServerMeta & SchemaType
|
||||
|
||||
export enum SchemaTypes {
|
||||
DistroMetaSchema = 'DistroMetaSchema',
|
||||
ServerMetaSchema = 'ServerMetaSchema'
|
||||
}
|
||||
|
||||
function getSchemaFileName(typeName: string) {
|
||||
return `${typeName}.schema.json`
|
||||
}
|
||||
|
||||
function getSchemaDirectory(absoluteRoot: string): string {
|
||||
return resolve(absoluteRoot, 'schemas')
|
||||
}
|
||||
|
||||
function getSchemaLocation(typeName: string, absoluteRoot: string): string {
|
||||
return resolve(getSchemaDirectory(absoluteRoot), getSchemaFileName(typeName))
|
||||
}
|
||||
|
||||
export function addSchemaToObject<T>(obj: T, typeName: string, absoluteRoot: string): T {
|
||||
return {
|
||||
$schema: new URL(`file:${getSchemaLocation(typeName, absoluteRoot)}`).href,
|
||||
...obj
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateSchemas(absoluteRoot: string): Promise<void> {
|
||||
|
||||
const selfPath = __filename.replace('dist', 'src').replace('.js', '.ts')
|
||||
|
||||
const schemaDir = getSchemaDirectory(absoluteRoot)
|
||||
if(await pathExists(schemaDir)) {
|
||||
await remove(schemaDir)
|
||||
}
|
||||
await mkdirs(schemaDir)
|
||||
|
||||
for(const typeName of Object.values(SchemaTypes)) {
|
||||
|
||||
logger.info(`Generating schema for ${typeName}`)
|
||||
|
||||
const schema = createGenerator({
|
||||
tsconfig: join(__dirname, '..', '..', 'tsconfig.json'),
|
||||
path: selfPath,
|
||||
type: typeName
|
||||
}).createSchema(typeName)
|
||||
|
||||
const schemaString = JSON.stringify(schema)
|
||||
const schemaLoc = getSchemaLocation(typeName, absoluteRoot)
|
||||
await writeFile(schemaLoc, schemaString)
|
||||
|
||||
logger.info(`Schema for ${typeName} saved to ${schemaLoc}`)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user