chore: add a script to generate schema (#4248)

* chore: add a script to generate schema

* typo fix

* refactor
master
Ika 2018-04-05 00:59:50 +08:00 committed by GitHub
parent 6d7bc4402f
commit 93caa7642e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 102 additions and 0 deletions

102
scripts/generate-schema.js Executable file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env node
"use strict";
const prettier = require("..");
console.log(
prettier.format(
JSON.stringify(generateSchema(prettier.getSupportInfo().options)),
{ parser: "json" }
)
);
function generateSchema(options) {
return {
$schema: "http://json-schema.org/draft-04/schema#",
title: "Schema for .prettierrc",
type: "object",
definitions: {
optionsDefinition: {
type: "object",
properties: options.reduce(
(props, option) =>
Object.assign(props, { [option.name]: optionToSchema(option) }),
{}
)
},
overridesDefinition: {
type: "object",
properties: {
overrides: {
type: "array",
description:
"Provide a list of patterns to override prettier configuration.",
items: {
type: "object",
required: ["files"],
properties: {
files: {
description: "Include these files in this override.",
oneOf: [
{ type: "string" },
{ type: "array", items: { type: "string" } }
]
},
excludeFiles: {
description: "Exclude these files from this override.",
oneOf: [
{ type: "string" },
{ type: "array", items: { type: "string" } }
]
},
options: {
type: "object",
description: "The options to apply for this override.",
$ref: "#/definitions/optionsDefinition"
}
},
additionalProperties: false
}
}
}
}
},
allOf: [
{ $ref: "#/definitions/optionsDefinition" },
{ $ref: "#/definitions/overridesDefinition" }
]
};
}
function optionToSchema(option) {
return Object.assign(
{
description: option.description,
default: option.default
},
option.type === "choice"
? { oneOf: option.choices.map(choiceToSchema) }
: { type: optionTypeToSchemaType(option.type) }
);
}
function optionTypeToSchemaType(optionType) {
switch (optionType) {
case "int":
return "integer";
case "array":
case "boolean":
return optionType;
case "choice":
throw new Error(
"Please use `oneOf` instead of `enum` for better description support."
);
default:
return "string";
}
}
function choiceToSchema(choice) {
return { enum: [choice.value], description: choice.description };
}