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:
Daniel Scalzi
2020-09-13 00:47:18 -04:00
parent 24b0923903
commit 42e47f4748
9 changed files with 207 additions and 21 deletions

View File

@@ -1,9 +1,13 @@
import { mkdirs, writeFile, readFile } from 'fs-extra'
import { mkdirs, writeFile, readFile, pathExists } from 'fs-extra'
import { Distribution } from 'helios-distribution-types'
import { SpecModelStructure } from './SpecModelStructure'
import { ServerStructure } from './Server.struct'
import { join, resolve } from 'path'
import { DistroMeta, getDefaultDistroMeta } from '../../model/nebula/distrometa'
import { addSchemaToObject, SchemaTypes } from '../../util/SchemaUtil'
import { LoggerUtil } from '../../util/LoggerUtil'
const logger = LoggerUtil.getLogger('DistributionStructure')
export class DistributionStructure implements SpecModelStructure<Distribution> {
@@ -24,8 +28,18 @@ export class DistributionStructure implements SpecModelStructure<Distribution> {
await mkdirs(this.absoluteRoot)
await mkdirs(this.metaPath)
const distroMeta: DistroMeta = getDefaultDistroMeta()
await writeFile(resolve(this.metaPath, this.DISTRO_META_FILE), JSON.stringify(distroMeta, null, 2))
const distroMetaFile = resolve(this.metaPath, this.DISTRO_META_FILE)
if(await pathExists(distroMetaFile)) {
logger.warn(`Distro Meta file already exists at ${distroMetaFile}!`)
logger.warn('If you wish to regenerate this file, you must delete the existing one!')
} else {
const distroMeta: DistroMeta = addSchemaToObject(
getDefaultDistroMeta(),
SchemaTypes.DistroMetaSchema,
this.absoluteRoot
)
await writeFile(distroMetaFile, JSON.stringify(distroMeta, null, 2))
}
await this.serverStruct.init()
}

View File

@@ -9,6 +9,7 @@ import { MiscFileStructure } from './module/File.struct'
import { LiteModStructure } from './module/LiteMod.struct'
import { LibraryStructure } from './module/Library.struct'
import { MinecraftVersion } from '../../util/MinecraftVersion'
import { addSchemaToObject, SchemaTypes } from '../../util/SchemaUtil'
export class ServerStructure extends BaseModelStructure<Server> {
@@ -73,7 +74,11 @@ export class ServerStructure extends BaseModelStructure<Server> {
serverMetaOpts.liteloaderVersion = options.liteloaderVersion
}
const serverMeta: ServerMeta = getDefaultServerMeta(id, minecraftVersion.toString(), serverMetaOpts)
const serverMeta: ServerMeta = addSchemaToObject(
getDefaultServerMeta(id, minecraftVersion.toString(), serverMetaOpts),
SchemaTypes.ServerMetaSchema,
this.absoluteRoot
)
await writeFile(resolvePath(absoluteServerRoot, this.SERVER_META_FILE), JSON.stringify(serverMeta, null, 2))
const libS = new LibraryStructure(absoluteServerRoot, relativeServerRoot, this.baseUrl, minecraftVersion, [])