renovate/bin/create-json-schema.js

103 lines
2.3 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
const upath = require('upath');
const { getOptions } = require('../lib/config/definitions');
const schema = {
title: 'JSON schema for Renovate config files (https://renovatebot.com/)',
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'object',
properties: {},
};
const options = getOptions();
options.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
const properties = {};
function createSingleConfig(option) {
const temp = {};
if (option.description) {
temp.description = option.description;
}
temp.type = option.type;
if (temp.type === 'array') {
if (option.subType) {
temp.items = {
type: option.subType,
};
if (option.format) {
temp.items.format = option.format;
}
if (option.allowedValues) {
temp.items.enum = option.allowedValues;
}
}
} else {
2019-03-23 07:27:46 +00:00
if (option.format) {
temp.format = option.format;
}
if (option.allowedValues) {
temp.enum = option.allowedValues;
2019-03-23 07:27:46 +00:00
}
}
if (option.default !== undefined) {
temp.default = option.default;
}
if (option.additionalProperties !== undefined) {
temp.additionalProperties = option.additionalProperties;
}
2019-03-31 07:16:29 +00:00
if (temp.type === 'object' && !option.freeChoice) {
2019-03-23 07:43:00 +00:00
temp.$ref = '#';
}
return temp;
}
function createSchemaForParentConfigs() {
for (const option of options) {
if (!option.parent) {
properties[option.name] = createSingleConfig(option);
}
}
}
function addChildrenArrayInParents() {
for (const option of options) {
if (option.parent) {
properties[option.parent].items = {
allOf: [
{
type: 'object',
properties: {},
},
],
};
}
}
}
function createSchemaForChildConfigs() {
for (const option of options) {
if (option.parent) {
properties[option.parent].items.allOf[0].properties[
option.name
] = createSingleConfig(option);
}
}
}
function generateSchema() {
createSchemaForParentConfigs();
addChildrenArrayInParents();
createSchemaForChildConfigs();
schema.properties = properties;
fs.writeFileSync(
upath.join(__dirname, '../renovate-schema.json'),
JSON.stringify(schema, null, 2),
'utf-8'
);
}
generateSchema();