feat: create json schema from definitions file (#3425)

Closes #545
This commit is contained in:
Rishabh Jain 2019-03-23 12:08:24 +05:30 committed by Rhys Arkins
parent 5c9c527f38
commit 8124634b6a
3 changed files with 1149 additions and 0 deletions

94
bin/create-json-schema.js Normal file
View file

@ -0,0 +1,94 @@
const fs = require('fs');
const upath = require('upath');
const { getOptions } = require('../lib/config/definitions');
const types = {
list: 'array',
json: 'object',
object: 'object',
integer: 'integer',
string: 'string',
boolean: 'boolean',
};
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();
const properties = {};
function createSingleConfig(option) {
const temp = {};
temp.type = types[option.type];
if (temp.type === 'object') {
temp.$ref = '#';
}
if (temp.type === 'array' && option.subType) {
temp.items = {
type: types[option.subType],
};
}
if (option.format) {
temp.format = option.format;
}
if (option.description) {
temp.description = option.description;
}
if (option.default) {
temp.default = option.default;
}
if (option.allowedValues) {
temp.enum = option.allowedValues;
}
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();

View file

@ -11,6 +11,7 @@ const options = [
'Configuration presets to use/extend. Note: does not work if configured in config.js', 'Configuration presets to use/extend. Note: does not work if configured in config.js',
stage: 'package', stage: 'package',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
cli: false, cli: false,
}, },
@ -20,6 +21,7 @@ const options = [
'A list of presets to ignore, including nested ones inside `extends`', 'A list of presets to ignore, including nested ones inside `extends`',
stage: 'package', stage: 'package',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
cli: false, cli: false,
}, },
@ -27,6 +29,7 @@ const options = [
name: 'description', name: 'description',
description: 'Plain text description for a config or preset', description: 'Plain text description for a config or preset',
type: 'list', type: 'list',
subType: 'string',
stage: 'repository', stage: 'repository',
allowString: true, allowString: true,
mergeable: true, mergeable: true,
@ -176,6 +179,7 @@ const options = [
name: 'schedule', name: 'schedule',
description: 'Times of day/week to limit branch creation to', description: 'Times of day/week to limit branch creation to',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
cli: true, cli: true,
env: false, env: false,
@ -408,6 +412,7 @@ const options = [
description: description:
'List of languages to match (e.g. ["python"]). Valid only within `packageRules` object', 'List of languages to match (e.g. ["python"]). Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
parent: 'packageRules', parent: 'packageRules',
stage: 'package', stage: 'package',
@ -420,6 +425,7 @@ const options = [
description: description:
'List of branches to match (e.g. ["master"]). Valid only within `packageRules` object', 'List of branches to match (e.g. ["master"]). Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
parent: 'packageRules', parent: 'packageRules',
stage: 'package', stage: 'package',
@ -432,6 +438,7 @@ const options = [
description: description:
'List of package managers to match (e.g. ["pipenv"]). Valid only within `packageRules` object', 'List of package managers to match (e.g. ["pipenv"]). Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
parent: 'packageRules', parent: 'packageRules',
stage: 'package', stage: 'package',
@ -444,6 +451,7 @@ const options = [
description: description:
'List of datasources to match (e.g. ["orb"]). Valid only within `packageRules` object', 'List of datasources to match (e.g. ["orb"]). Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
parent: 'packageRules', parent: 'packageRules',
stage: 'package', stage: 'package',
@ -456,6 +464,7 @@ const options = [
description: description:
'List of depTypes to match (e.g. [`peerDependencies`]). Valid only within `packageRules` object', 'List of depTypes to match (e.g. [`peerDependencies`]). Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
parent: 'packageRules', parent: 'packageRules',
stage: 'package', stage: 'package',
@ -468,6 +477,7 @@ const options = [
description: description:
'Package names to match. Valid only within `packageRules` object', 'Package names to match. Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
stage: 'package', stage: 'package',
parent: 'packageRules', parent: 'packageRules',
@ -480,6 +490,7 @@ const options = [
description: description:
'Package names to exclude. Valid only within `packageRules` object', 'Package names to exclude. Valid only within `packageRules` object',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
stage: 'package', stage: 'package',
parent: 'packageRules', parent: 'packageRules',
@ -492,6 +503,7 @@ const options = [
description: description:
'Package name patterns to match. Valid only within `packageRules` object.', 'Package name patterns to match. Valid only within `packageRules` object.',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
stage: 'package', stage: 'package',
parent: 'packageRules', parent: 'packageRules',
@ -504,6 +516,7 @@ const options = [
description: description:
'Package name patterns to exclude. Valid only within `packageRules` object.', 'Package name patterns to exclude. Valid only within `packageRules` object.',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
stage: 'package', stage: 'package',
parent: 'packageRules', parent: 'packageRules',
@ -527,6 +540,7 @@ const options = [
description: description:
'A list of source URL prefixes to match against, commonly used for grouping of monorepos or packages from the same organization.', 'A list of source URL prefixes to match against, commonly used for grouping of monorepos or packages from the same organization.',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
stage: 'package', stage: 'package',
parent: 'packageRules', parent: 'packageRules',
@ -539,6 +553,7 @@ const options = [
description: description:
'Update types to match against (major, minor, pin, etc). Valid only within `packageRules` object.', 'Update types to match against (major, minor, pin, etc). Valid only within `packageRules` object.',
type: 'list', type: 'list',
subType: 'string',
allowString: true, allowString: true,
stage: 'package', stage: 'package',
parent: 'packageRules', parent: 'packageRules',
@ -613,6 +628,7 @@ const options = [
description: 'Regex for identifying unstable versions (docker only)', description: 'Regex for identifying unstable versions (docker only)',
stage: 'package', stage: 'package',
type: 'string', type: 'string',
format: 'regex',
cli: false, cli: false,
env: false, env: false,
}, },
@ -1021,6 +1037,8 @@ const options = [
name: 'fileMatch', name: 'fileMatch',
description: 'JS RegExp pattern for matching manager files', description: 'JS RegExp pattern for matching manager files',
type: 'list', type: 'list',
subType: 'string',
format: 'regex',
stage: 'repository', stage: 'repository',
allowString: true, allowString: true,
mergeable: true, mergeable: true,
@ -1190,6 +1208,7 @@ const options = [
description: description:
'Dependency support policy, e.g. used for LTS vs non-LTS etc (node-only)', 'Dependency support policy, e.g. used for LTS vs non-LTS etc (node-only)',
type: 'list', type: 'list',
subType: 'string',
stage: 'package', stage: 'package',
allowString: true, allowString: true,
}, },
@ -1534,6 +1553,7 @@ const options = [
description: description:
'List of additional notes/templates to be included in the Pull Request bodies.', 'List of additional notes/templates to be included in the Pull Request bodies.',
type: 'list', type: 'list',
subType: 'string',
default: [], default: [],
allowString: true, allowString: true,
mergeable: true, mergeable: true,

1035
renovate-schema.json Normal file

File diff suppressed because it is too large Load diff