-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
config.ts
91 lines (83 loc) · 2.51 KB
/
config.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
import table from 'markdown-table';
import { getOptions } from '../../lib/config/options';
import { getCliName } from '../../lib/workers/global/config/parse/cli';
import { getEnvName } from '../../lib/workers/global/config/parse/env';
import { readFile, updateFile } from '../utils/index';
const options = getOptions();
function genTable(obj: [string, string][], type: string, def: any): string {
const data = [['Name', 'Value']];
const name = obj[0][1];
const ignoredKeys = [
'name',
'description',
'default',
'stage',
'allowString',
'cli',
'env',
'admin',
];
obj.forEach(([key, val]) => {
const el = [key, val];
if (
!ignoredKeys.includes(el[0]) ||
(el[0] === 'default' && typeof el[1] !== 'object' && name !== 'prBody')
) {
if (type === 'string' && el[0] === 'default') {
el[1] = `\`"${el[1]}"\``;
}
if (type === 'boolean' && el[0] === 'default') {
el[1] = `\`${el[1]}\``;
}
if (type === 'string' && el[0] === 'default' && el[1].length > 200) {
el[1] = `[template]`;
}
data.push(el);
}
});
if (type === 'list') {
data.push(['default', '`[]`']);
}
if (type === 'string' && def === undefined) {
data.push(['default', '`null`']);
}
if (type === 'boolean' && def === undefined) {
data.push(['default', '`true`']);
}
if (type === 'boolean' && def === null) {
data.push(['default', '`null`']);
}
return table(data);
}
export async function generateConfig(dist: string, bot = false): Promise<void> {
let configFile = `configuration-options.md`;
if (bot) {
configFile = `self-hosted-configuration.md`;
}
const configOptionsRaw = (await readFile(`docs/usage/${configFile}`)).split(
'\n'
);
options
.filter((option) => option.releaseStatus !== 'unpublished')
.forEach((option) => {
const el: Record<string, any> = { ...option };
let headerIndex = configOptionsRaw.indexOf(`## ${option.name}`);
if (headerIndex === -1) {
headerIndex = configOptionsRaw.indexOf(`### ${option.name}`);
}
if (bot) {
el.cli = getCliName(el);
el.env = getEnvName(el);
if (el.cli === '') {
el.cli = `N/A`;
}
if (el.env === '') {
el.env = 'N/A';
}
}
configOptionsRaw[headerIndex] +=
`\n${option.description}\n\n` +
genTable(Object.entries(el), option.type, option.default);
});
await updateFile(`${dist}/${configFile}`, configOptionsRaw.join('\n'));
}