/
schema.ts
121 lines (115 loc) · 3.12 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { getOptions } from '../../lib/config/options';
import type { RenovateOptions } from '../../lib/config/types';
import { hasKey } from '../../lib/util/object';
import { updateFile } from '../utils';
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 = schema.properties as Record<string, any>;
function createSingleConfig(option: RenovateOptions): Record<string, unknown> {
const temp: Record<string, any> & Partial<RenovateOptions> = {};
if (option.description) {
temp.description = option.description;
}
temp.type = option.type;
if (option.type === 'array') {
if (option.subType) {
temp.items = {
type: option.subType,
};
if (hasKey('format', option) && option.format) {
temp.items.format = option.format;
}
if (option.allowedValues) {
temp.items.enum = option.allowedValues;
}
}
if (option.subType === 'string' && option.allowString === true) {
const items = temp.items;
delete temp.items;
delete temp.type;
temp.oneOf = [{ type: 'array', items }, { ...items }];
}
} else {
if (hasKey('format', option) && option.format) {
temp.format = option.format;
}
if (option.name === 'versioning') {
temp.oneOf = [
{ enum: option.allowedValues },
{ type: 'string', pattern: '^regex:' },
];
} else if (option.allowedValues) {
temp.enum = option.allowedValues;
}
}
if (option.default !== undefined) {
temp.default = option.default;
}
if (
hasKey('additionalProperties', option) &&
option.additionalProperties !== undefined
) {
temp.additionalProperties = option.additionalProperties;
}
if (temp.type === 'object' && !option.freeChoice) {
temp.$ref = '#';
}
return temp;
}
function createSchemaForParentConfigs(): void {
for (const option of options) {
if (!option.parent) {
properties[option.name] = createSingleConfig(option);
}
}
}
function addChildrenArrayInParents(): void {
for (const option of options) {
if (option.parent) {
properties[option.parent].items = {
allOf: [
{
type: 'object',
properties: {
description: {
type: 'string',
description: 'A custom description',
},
},
},
],
};
}
}
}
function createSchemaForChildConfigs(): void {
for (const option of options) {
if (option.parent) {
properties[option.parent].items.allOf[0].properties[option.name] =
createSingleConfig(option);
}
}
}
export async function generateSchema(dist: string): Promise<void> {
createSchemaForParentConfigs();
addChildrenArrayInParents();
createSchemaForChildConfigs();
await updateFile(
`${dist}/renovate-schema.json`,
`${JSON.stringify(schema, null, 2)}\n`
);
}