2026-03-29 13:18:53 +02:00
|
|
|
|
import { readFileSync, writeFileSync } from 'fs'
|
|
|
|
|
|
import { load } from 'js-yaml'
|
|
|
|
|
|
import { dirname, join } from 'path'
|
|
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
|
const __dirname = dirname(__filename)
|
|
|
|
|
|
|
|
|
|
|
|
const rootDir = join(__dirname, '..')
|
|
|
|
|
|
const yamlPath = join(rootDir, 'config', 'default.yaml')
|
|
|
|
|
|
const outputPath = join(rootDir, 'config', 'config-schema.json')
|
|
|
|
|
|
|
|
|
|
|
|
type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null'
|
|
|
|
|
|
|
|
|
|
|
|
interface JsonSchema {
|
|
|
|
|
|
$schema?: string
|
|
|
|
|
|
title?: string
|
|
|
|
|
|
description?: string
|
|
|
|
|
|
type?: JsonSchemaType | JsonSchemaType[]
|
|
|
|
|
|
properties?: Record<string, JsonSchema>
|
|
|
|
|
|
items?: JsonSchema
|
|
|
|
|
|
default?: unknown
|
|
|
|
|
|
additionalProperties?: boolean | JsonSchema
|
|
|
|
|
|
examples?: unknown[]
|
|
|
|
|
|
enum?: unknown[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
// Step 1: extract comments from the raw YAML text
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Find a `#`-comment in the "rest" portion of a key: value line, ignoring
|
|
|
|
|
|
* `#` characters that appear inside single- or double-quoted strings.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function extractInlineComment (rest: string): string | null {
|
|
|
|
|
|
let inSingle = false
|
|
|
|
|
|
let inDouble = false
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < rest.length; i++) {
|
|
|
|
|
|
const ch = rest[i]
|
|
|
|
|
|
|
2026-10-16 10:56:43 +02:00
|
|
|
|
if (ch === '\'' && !inDouble) {
|
2026-03-29 13:18:53 +02:00
|
|
|
|
inSingle = !inSingle
|
|
|
|
|
|
} else if (ch === '"' && !inSingle) {
|
|
|
|
|
|
inDouble = !inDouble
|
|
|
|
|
|
} else if (ch === '#' && !inSingle && !inDouble) {
|
|
|
|
|
|
const comment = rest.slice(i + 1).trim()
|
|
|
|
|
|
return comment.length > 0 ? comment : null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Walk the YAML source line-by-line to associate block and inline comments
|
|
|
|
|
|
* with each key, returning a map from dotted-path → description string.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Limitations (acceptable for schema-generation purposes):
|
|
|
|
|
|
* - List items are identified only by their parent key path (not by index).
|
|
|
|
|
|
* - Only indentation-based nesting is tracked; flow-style YAML is ignored.
|
|
|
|
|
|
*/
|
2026-10-16 10:56:43 +02:00
|
|
|
|
function extractComments (yamlText: string) {
|
2026-03-29 13:18:53 +02:00
|
|
|
|
const lines = yamlText.split('\n')
|
|
|
|
|
|
const commentMap = new Map<string, string>()
|
2026-10-16 10:56:43 +02:00
|
|
|
|
// Keys without value followed by commented list items (`# - ...`): null by default, but admins fill them with a list
|
|
|
|
|
|
const nullListPaths = new Set<string>()
|
2026-03-29 13:18:53 +02:00
|
|
|
|
|
|
|
|
|
|
let pendingComments: string[] = []
|
|
|
|
|
|
// Stack entries: { indent, key } – used to reconstruct the dotted path.
|
2026-10-16 10:56:43 +02:00
|
|
|
|
const pathStack: { indent: number, key: string }[] = []
|
|
|
|
|
|
let lastEmptyKeyPath: string | null = null
|
2026-03-29 13:18:53 +02:00
|
|
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
|
// 1. Pure comment line
|
|
|
|
|
|
if (/^\s*#/.test(line)) {
|
2026-10-16 10:56:43 +02:00
|
|
|
|
// Keep looking after other comment lines, explaining the commented list for example
|
|
|
|
|
|
if (lastEmptyKeyPath && /^\s*#\s*- /.test(line)) nullListPaths.add(lastEmptyKeyPath)
|
|
|
|
|
|
|
2026-03-29 13:18:53 +02:00
|
|
|
|
const comment = line.replace(/^\s*#\s?/, '').trim()
|
|
|
|
|
|
if (comment) pendingComments.push(comment)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Empty line – discard accumulated comments (they belonged to the
|
|
|
|
|
|
// previous block and are separated from the next key by blank space).
|
|
|
|
|
|
if (/^\s*$/.test(line)) {
|
|
|
|
|
|
pendingComments = []
|
2026-10-16 10:56:43 +02:00
|
|
|
|
lastEmptyKeyPath = null
|
2026-03-29 13:18:53 +02:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Key-value (or key-only) line: capture indent + key name
|
|
|
|
|
|
const keyMatch = line.match(/^(\s*)([a-zA-Z0-9_]+)\s*:(.*)$/)
|
|
|
|
|
|
if (keyMatch) {
|
|
|
|
|
|
const indent = keyMatch[1].length
|
|
|
|
|
|
const key = keyMatch[2]
|
|
|
|
|
|
const rest = keyMatch[3]
|
|
|
|
|
|
|
|
|
|
|
|
// Maintain the path stack: pop any entries at >= this indent level.
|
|
|
|
|
|
while (pathStack.length > 0 && pathStack[pathStack.length - 1].indent >= indent) {
|
|
|
|
|
|
pathStack.pop()
|
|
|
|
|
|
}
|
|
|
|
|
|
pathStack.push({ indent, key })
|
|
|
|
|
|
|
|
|
|
|
|
const fullPath = pathStack.map(p => p.key).join('.')
|
2026-10-16 10:56:43 +02:00
|
|
|
|
lastEmptyKeyPath = /^\s*$/.test(rest) ? fullPath : null
|
2026-03-29 13:18:53 +02:00
|
|
|
|
|
|
|
|
|
|
// Inline comment on the same line takes priority / is appended.
|
|
|
|
|
|
const inlineComment = extractInlineComment(rest)
|
|
|
|
|
|
if (inlineComment) {
|
|
|
|
|
|
pendingComments.push(inlineComment)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (pendingComments.length > 0) {
|
|
|
|
|
|
commentMap.set(fullPath, pendingComments.join(' '))
|
|
|
|
|
|
pendingComments = []
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. List-item lines (`- value`) and anything else: discard pending comments.
|
|
|
|
|
|
pendingComments = []
|
2026-10-16 10:56:43 +02:00
|
|
|
|
lastEmptyKeyPath = null
|
2026-03-29 13:18:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-16 10:56:43 +02:00
|
|
|
|
return { commentMap, nullListPaths }
|
2026-03-29 13:18:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
// Step 2: convert the parsed YAML value tree into a JSON Schema
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
function inferType (value: unknown): JsonSchemaType | JsonSchemaType[] {
|
|
|
|
|
|
if (value === null) return [ 'null', 'string' ]
|
|
|
|
|
|
if (typeof value === 'boolean') return 'boolean'
|
|
|
|
|
|
if (typeof value === 'number') return Number.isInteger(value) ? 'integer' : 'number'
|
|
|
|
|
|
if (typeof value === 'string') return 'string'
|
|
|
|
|
|
if (Array.isArray(value)) return 'array'
|
|
|
|
|
|
return 'object'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function generateSchema (
|
|
|
|
|
|
value: unknown,
|
|
|
|
|
|
commentMap: Map<string, string>,
|
2026-10-16 10:56:43 +02:00
|
|
|
|
nullListPaths: Set<string>,
|
2026-03-29 13:18:53 +02:00
|
|
|
|
path: string
|
|
|
|
|
|
): JsonSchema {
|
|
|
|
|
|
const description = commentMap.get(path)
|
|
|
|
|
|
const base: JsonSchema = {}
|
|
|
|
|
|
if (description) base.description = description
|
|
|
|
|
|
|
|
|
|
|
|
if (value === null) {
|
2026-10-16 10:56:43 +02:00
|
|
|
|
if (nullListPaths.has(path)) return { ...base, type: [ 'null', 'array' ], default: null, items: {} }
|
|
|
|
|
|
|
2026-03-29 13:18:53 +02:00
|
|
|
|
return { ...base, type: [ 'null', 'string' ], default: null }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof value === 'boolean') {
|
|
|
|
|
|
return { ...base, type: 'boolean', default: value }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof value === 'number') {
|
|
|
|
|
|
return { ...base, type: Number.isInteger(value) ? 'integer' : 'number', default: value }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
|
return { ...base, type: 'string', default: value }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
|
const schema: JsonSchema = { ...base, type: 'array', default: value }
|
|
|
|
|
|
if (value.length > 0) {
|
2026-10-16 10:56:43 +02:00
|
|
|
|
schema.items = generateSchema(value[0], commentMap, nullListPaths, path + '[]')
|
2026-03-29 13:18:53 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
schema.items = {}
|
|
|
|
|
|
}
|
|
|
|
|
|
return schema
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Plain object
|
|
|
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
|
|
|
const properties: Record<string, JsonSchema> = {}
|
|
|
|
|
|
for (const [ key, val ] of Object.entries(value as Record<string, unknown>)) {
|
|
|
|
|
|
const childPath = path ? `${path}.${key}` : key
|
2026-10-16 10:56:43 +02:00
|
|
|
|
properties[key] = generateSchema(val, commentMap, nullListPaths, childPath)
|
2026-03-29 13:18:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
return { ...base, type: 'object', properties, additionalProperties: false }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { ...base, type: inferType(value) }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
// Main
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
const yamlContent = readFileSync(yamlPath, 'utf8')
|
|
|
|
|
|
const parsed = load(yamlContent) as Record<string, unknown>
|
2026-10-16 10:56:43 +02:00
|
|
|
|
const { commentMap, nullListPaths } = extractComments(yamlContent)
|
2026-03-29 13:18:53 +02:00
|
|
|
|
|
|
|
|
|
|
const schema: JsonSchema = {
|
2026-08-17 10:02:58 +02:00
|
|
|
|
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
2026-03-29 13:18:53 +02:00
|
|
|
|
title: 'PeerTube Configuration',
|
2026-10-16 10:56:43 +02:00
|
|
|
|
description: 'Full configuration schema for a PeerTube server instance. ' +
|
2026-03-29 13:18:53 +02:00
|
|
|
|
'Corresponds to the YAML configuration files (default.yaml / production.yaml). ',
|
|
|
|
|
|
type: 'object',
|
|
|
|
|
|
properties: {},
|
|
|
|
|
|
additionalProperties: false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const [ key, val ] of Object.entries(parsed)) {
|
2026-10-16 10:56:43 +02:00
|
|
|
|
schema.properties[key] = generateSchema(val, commentMap, nullListPaths, key)
|
2026-03-29 13:18:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writeFileSync(outputPath, JSON.stringify(schema, null, 2) + '\n')
|
|
|
|
|
|
console.log(`JSON schema written to ${outputPath}`)
|