Add command to export JSON schemas

This commit is contained in:
Moritz Ruth 2022-08-18 14:41:57 +02:00
parent d40c3b82e3
commit 2d1f3156c9
9 changed files with 51 additions and 13 deletions

7
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules/
.idea/
dist/
/node_modules/
/.idea/
/dist/
/schemas/

View file

@ -47,7 +47,8 @@
"toml": "^3.0.0",
"wrap-ansi": "^8.0.1",
"yazl": "^2.5.1",
"zod": "^3.18.0"
"zod": "^3.18.0",
"zod-to-json-schema": "^3.17.1"
},
"devDependencies": {
"@types/dedent": "^0.7.0",

10
pnpm-lock.yaml generated
View file

@ -42,6 +42,7 @@ specifiers:
wrap-ansi: ^8.0.1
yazl: ^2.5.1
zod: ^3.18.0
zod-to-json-schema: ^3.17.1
dependencies:
'@root/walk': 1.1.0
@ -73,6 +74,7 @@ dependencies:
wrap-ansi: 8.0.1
yazl: 2.5.1
zod: 3.18.0
zod-to-json-schema: 3.17.1_zod@3.18.0
devDependencies:
'@types/dedent': 0.7.0
@ -1777,6 +1779,14 @@ packages:
engines: {node: '>=12.20'}
dev: false
/zod-to-json-schema/3.17.1_zod@3.18.0:
resolution: {integrity: sha512-h1WLfvkdwM6lcRt5q09ytNh2OKcU13vY9D0FetN/7Erdd7lVraIiMkEnObg3KELm5EQlMqvi0r5nzMa0EA+8qw==}
peerDependencies:
zod: ^3.17.10
dependencies:
zod: 3.18.0
dev: false
/zod/3.18.0:
resolution: {integrity: sha512-gwTm8RfUCe8l9rDwN5r2A17DkAa8Ez4Yl4yXqc5VqeGaXaJahzYYXbTwvhroZi0SNBqTwh/bKm2N0mpCzuw4bA==}
dev: false

View file

@ -0,0 +1,22 @@
import { Command } from "commander"
import fs from "fs-extra"
import { zodToJsonSchema } from "zod-to-json-schema"
import { metaFileContentSchema, packManifestFileSchema } from "../files.js"
export const exportJsonSchemasCommand = new Command("export-json-schemas")
.argument("<path>")
.description("Exports the pack manifest and meta-file JSON schemas.")
.action(async path => {
await fs.mkdirp(path)
await fs.writeJson(path + "/manifest.schema.json", {
title: "Horizr pack manifest",
$id: "https://horizr.moritzruth.de/schemas/pack/manifest.schema.json",
...zodToJsonSchema(packManifestFileSchema)
}, { spaces: 2 })
await fs.writeJson(path + "/meta-file.schema.json", {
title: "Horizr pack meta-file",
$id: "https://horizr.moritzruth.de/schemas/pack/meta-file.schema.json",
...zodToJsonSchema(metaFileContentSchema)
}, { spaces: 2 })
})

View file

@ -5,7 +5,7 @@ import { output } from "../utils/output.js"
import kleur from "kleur"
import { fetchFabricMinecraftVersions, fetchFabricVersions } from "../fabricApi.js"
import enquirer from "enquirer"
import { PACK_MANIFEST_FILE_NAME, PACK_MANIFEST_FORMAT_VERSION, PackManifest } from "../files.js"
import { PACK_MANIFEST_FILE_NAME, FORMAT_VERSION, PackManifest } from "../files.js"
import pathModule from "path"
import { EXPORTS_DIRECTORY_NAME } from "../pack.js"
import slugify from "@sindresorhus/slugify"
@ -63,7 +63,7 @@ export const initCommand = new Command("init")
const fabricVersion = (await output.withLoading(fetchFabricVersions(answers.minecraftVersion), "Fetching latest Fabric version"))[0]
const file: PackManifest = {
formatVersion: PACK_MANIFEST_FORMAT_VERSION,
formatVersion: FORMAT_VERSION,
slug: slugify(answers.name),
meta: {
name: answers.name,

View file

@ -12,7 +12,7 @@ import { output } from "../../utils/output.js"
import { modrinthApi } from "../../modrinth/api.js"
import { Side, usePack } from "../../pack.js"
import kleur from "kleur"
import { META_FILE_EXTENSION, metaFileContentSchema, writeJsonFile } from "../../files.js"
import { FORMAT_VERSION, META_FILE_EXTENSION, metaFileContentSchema, writeJsonFile } from "../../files.js"
import fs from "fs-extra"
import enquirer from "enquirer"
import { orEmptyString } from "../../utils/strings.js"
@ -67,6 +67,7 @@ export const activateCommand = new Command("activate")
await fs.mkdirp(absolutePath.parent().toString())
await writeJsonFile(absolutePath, metaFileContentSchema, {
formatVersion: FORMAT_VERSION,
enabled: true,
version: getMetaFileContentVersionForModrinth(modrinthVersion),
source: {

View file

@ -44,11 +44,11 @@ export async function readJsonFile<S extends z.ZodObject<ZodRawShape>>(rootPath:
return result.data
}
export const PACK_MANIFEST_FORMAT_VERSION = 1
export const FORMAT_VERSION = 1
export const PACK_MANIFEST_FILE_NAME = "horizr.json"
export const horizrFileSchema = z.object({
formatVersion: z.literal(PACK_MANIFEST_FORMAT_VERSION),
export const packManifestFileSchema = z.object({
formatVersion: z.literal(FORMAT_VERSION),
slug: z.string(),
meta: z.object({
name: z.string(),
@ -63,7 +63,7 @@ export const horizrFileSchema = z.object({
})
})
export type PackManifest = z.output<typeof horizrFileSchema>
export type PackManifest = z.output<typeof packManifestFileSchema>
export const META_FILE_EXTENSION = "hm.json"
@ -89,6 +89,7 @@ const metaFileContentVersionSchema = z.object({
export type MetaFileContentVersion = z.output<typeof metaFileContentVersionSchema>
export const metaFileContentSchema = z.object({
formatVersion: z.literal(FORMAT_VERSION),
displayName: z.string().optional(),
enabled: z.boolean().default(true),
comment: z.string().optional(),

View file

@ -10,6 +10,7 @@ import { infoCommand } from "./commands/info.js"
import { updateCommand } from "./commands/update.js"
import { packwizCommand } from "./commands/packwiz/index.js"
import { modrinthCommand } from "./commands/modrinth/index.js"
import { exportJsonSchemasCommand } from "./commands/exportJsonSchemas.js"
const program = new Command("horizr")
.version(
@ -23,6 +24,7 @@ const program = new Command("horizr")
})
.addCommand(modrinthCommand)
.addCommand(packwizCommand)
.addCommand(exportJsonSchemasCommand, { hidden: true })
.addCommand(infoCommand)
.addCommand(initCommand)
.addCommand(updateCommand)

View file

@ -2,7 +2,7 @@ import { AbsolutePath, envPaths, RelativePath } from "./utils/path.js"
import {
findPackDirectoryPath,
PackManifest,
horizrFileSchema,
packManifestFileSchema,
MetaFileContent,
PACK_MANIFEST_FILE_NAME,
metaFileContentSchema,
@ -82,7 +82,7 @@ export async function usePack(): Promise<Pack> {
const sourceDirectoryPath = rootDirectoryPath.resolve("src")
const readSourceJsonFile: Pack["readSourceJsonFile"] = async (path, schema) => readJsonFile(sourceDirectoryPath, path, schema)
const manifest = (await readJsonFile(rootDirectoryPath, RelativePath.create(PACK_MANIFEST_FILE_NAME), horizrFileSchema))!
const manifest = (await readJsonFile(rootDirectoryPath, RelativePath.create(PACK_MANIFEST_FILE_NAME), packManifestFileSchema))!
const metaFiles: MetaFile[] = []
const staticSourceFiles: StaticSourceFile[] = []