2018-04-04 19:59:50 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-04-14 19:06:43 +03:00
|
|
|
if (require.main !== module) {
|
|
|
|
module.exports = generateSchema;
|
|
|
|
} else {
|
|
|
|
const prettier = require("..");
|
|
|
|
console.log(
|
|
|
|
prettier.format(
|
|
|
|
JSON.stringify(generateSchema(prettier.getSupportInfo().options)),
|
|
|
|
{ parser: "json" }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-04-04 19:59:50 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
},
|
2018-04-14 19:06:43 +03:00
|
|
|
(option.array ? wrapWithArraySchema : identity)(
|
|
|
|
option.type === "choice"
|
|
|
|
? { oneOf: option.choices.map(choiceToSchema) }
|
|
|
|
: { type: optionTypeToSchemaType(option.type) }
|
|
|
|
)
|
2018-04-04 19:59:50 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:06:43 +03:00
|
|
|
function identity(x) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapWithArraySchema(items) {
|
|
|
|
return { type: "array", items };
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:59:50 +03:00
|
|
|
function optionTypeToSchemaType(optionType) {
|
|
|
|
switch (optionType) {
|
|
|
|
case "int":
|
|
|
|
return "integer";
|
|
|
|
case "boolean":
|
|
|
|
return optionType;
|
|
|
|
case "choice":
|
|
|
|
throw new Error(
|
|
|
|
"Please use `oneOf` instead of `enum` for better description support."
|
|
|
|
);
|
2018-04-14 19:06:43 +03:00
|
|
|
case "path":
|
2018-04-04 19:59:50 +03:00
|
|
|
return "string";
|
2018-04-14 19:06:43 +03:00
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected optionType '${optionType}'`);
|
2018-04-04 19:59:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function choiceToSchema(choice) {
|
|
|
|
return { enum: [choice.value], description: choice.description };
|
|
|
|
}
|