Skip to content

Commit

Permalink
feat: Automatically generate schema for typedoc.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 6, 2020
1 parent ac4c688 commit cd84548
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
69 changes: 69 additions & 0 deletions scripts/generate_options_schema.js
@@ -0,0 +1,69 @@
//@ts-check

const { addTypeDocOptions } = require("../dist/lib/utils/options/sources");
const { ParameterType } = require("../dist");

const IGNORED_OPTIONS = new Set(["help", "version"]);

const IGNORED_DEFAULT_OPTIONS = new Set(["options", "tsconfig"]);

const schema = {
$schema: "https://json-schema.org/draft/2019-09/schema",
title: "JSON Schema for typedoc.json",
type: "object",
properties: {},
};

addTypeDocOptions({
/** @param {import("../dist").DeclarationOption} option */
addDeclaration(option) {
if (IGNORED_OPTIONS.has(option.name)) return;

const data = {
description: option.help,
};

switch (option.type ?? ParameterType.String) {
case ParameterType.Array:
data.type = "array";
data.items = { type: "string" };
data.default = option.defaultValue ?? [];
break;
case ParameterType.String:
data.type = "string";
if (!IGNORED_DEFAULT_OPTIONS.has(option.name)) {
data.default = option.defaultValue ?? "";
}
break;
case ParameterType.Boolean:
data.type = "boolean";
data.default = option.defaultValue ?? false;
break;
case ParameterType.Number: {
const decl = /** @type {import("../dist").NumberDeclarationOption} */ (option);
data.type = "number";
data.default = decl.defaultValue ?? 0;
data.maximum = decl.maxValue;
data.minimum = decl.minValue;
break;
}
case ParameterType.Map: {
const map = /** @type {import("../dist").MapDeclarationOption} */ (option)
.map;
data.enum =
map instanceof Map ? [...map.keys()] : Object.keys(map);
data.default = option.defaultValue;
break;
}
case ParameterType.Mixed:
break; // Nothing to do... TypeDoc really shouldn't have any of these.
}

schema.properties[option.name] = data;
},
});

schema.properties.logger.enum = ["console", "none"];
schema.properties.logger.default = "console";

console.log(JSON.stringify(schema, null, "\t"));
1 change: 0 additions & 1 deletion src/lib/utils/options/declaration.ts
Expand Up @@ -90,7 +90,6 @@ export enum ParameterType {
Map,
Mixed,
Array,
Path,
}

export interface DeclarationOptionBase {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/options/sources/typedoc.ts
@@ -1,7 +1,7 @@
import { Options } from "..";
import { ParameterType, ParameterHint } from "../declaration";

export function addTypeDocOptions(options: Options) {
export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
options.addDeclaration({
name: "options",
help:
Expand Down

0 comments on commit cd84548

Please sign in to comment.