-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
config.js
269 lines (228 loc) · 7.29 KB
/
config.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const RefParser = require('json-schema-ref-parser')
const parser = new RefParser()
const jsf = require('json-schema-faker').default
const YAML = require('yaml')
const { pathOr } = require('ramda')
const path = require('path')
const fs = require('fs')
const prettier = require('prettier')
const prettierStyles = require('ory-prettier-styles')
const { execSync } = require('child_process')
const fetch = require('node-fetch')
const oryXVersion = execSync(
"cd ..; go list -f '{{.Module.Version}}' -find github.com/ory/x"
)
.toString('utf-8')
.trim()
const refs = {
'ory://tracing-config': `https://raw.githubusercontent.com/ory/x/${oryXVersion}/tracing/config.schema.json`,
'ory://logging-config': `https://raw.githubusercontent.com/ory/x/${oryXVersion}/logrusx/config.schema.json`
}
const oryResolver = {
order: 1,
canRead: /^ory:/i,
read: ({ url }) => fetch(refs[url]).then((res) => res.json())
}
jsf.option({
alwaysFakeOptionals: true,
useExamplesValue: true,
useDefaultValue: true,
minItems: 1,
random: () => 0
})
if (process.argv.length !== 3 || process.argv[1] === 'help') {
console.error(`
usage:
node config.js path/to/config.js
`)
return
}
const config = require(path.resolve(process.argv[2]))
const enhance =
(schema, parents = []) =>
(item) => {
const key = item.key.value
const path = [
...parents.map((parent) => ['properties', parent]),
['properties', key]
].flat()
if (['title', 'description'].find((f) => path[path.length - 1] === f)) {
return
}
const comments = [`# ${pathOr(key, [...path, 'title'], schema)} ##`, '']
const description = pathOr('', [...path, 'description'], schema)
if (description) {
comments.push(' ' + description.split('\n').join('\n '), '')
}
const defaultValue = pathOr('', [...path, 'default'], schema)
if (defaultValue || defaultValue === false) {
comments.push(' Default value: ' + defaultValue, '')
}
const enums = pathOr('', [...path, 'enum'], schema)
if (enums && Array.isArray(enums)) {
comments.push(
' One of:',
...YAML.stringify(enums)
.split('\n')
.map((i) => ` ${i}`)
) // split always returns one empty object so no need for newline
}
const min = pathOr('', [...path, 'minimum'], schema)
if (min || min === 0) {
comments.push(` Minimum value: ${min}`, '')
}
const max = pathOr('', [...path, 'maximum'], schema)
if (max || max === 0) {
comments.push(` Maximum value: ${max}`, '')
}
const examples = pathOr('', [...path, 'examples'], schema)
if (examples) {
comments.push(
' Examples:',
...YAML.stringify(examples)
.split('\n')
.map((i) => ` ${i}`)
) // split always returns one empty object so no need for newline
}
let hasChildren
if (item.value.items) {
item.value.items.forEach((item) => {
if (item.key) {
enhance(schema, [...parents, key])(item)
hasChildren = true
}
})
}
const showEnvVarBlockForObject = pathOr(
'',
[...path, 'showEnvVarBlockForObject'],
schema
)
if (!hasChildren || showEnvVarBlockForObject) {
const env = [...parents, key].map((i) => i.toUpperCase()).join('_')
comments.push(
' Set this value using environment variables on',
' - Linux/macOS:',
` $ export ${env}=<value>`,
' - Windows Command Line (CMD):',
` > set ${env}=<value>`,
''
)
// Show this if the config property is an object, to call out how to specify the env var
if (hasChildren) {
comments.push(
' This can be set as an environment variable by supplying it as a JSON object.',
''
)
}
}
item.commentBefore = comments.join('\n')
item.spaceBefore = true
}
new Promise((resolve, reject) => {
parser.dereference(
require(path.resolve(config.updateConfig.src)),
{
resolve: {
ory: oryResolver
}
},
(err, result) => (err ? reject(err) : resolve(result))
)
})
.then((schema) => {
const removeAdditionalProperties = (o) => {
delete o['additionalProperties']
if (o.properties) {
Object.keys(o.properties).forEach((key) =>
removeAdditionalProperties(o.properties[key])
)
}
}
const enableAll = (o) => {
if (o.properties) {
Object.keys(o.properties).forEach((key) => {
if (key === 'enable') {
o.properties[key] = true
}
enableAll(o.properties[key])
})
}
}
removeAdditionalProperties(schema)
enableAll(schema)
if (schema.definitions) {
Object.keys(schema.definitions).forEach((key) => {
removeAdditionalProperties(schema.definitions[key])
enableAll(schema.definitions[key])
})
}
jsf.option({
useExamplesValue: true,
useDefaultValue: false, // do not change this!!
fixedProbabilities: true,
alwaysFakeOptionals: true
})
const values = jsf.generate(schema)
const doc = YAML.parseDocument(YAML.stringify(values))
const comments = [`# ${pathOr(config.projectSlug, ['title'], schema)}`, '']
const description = pathOr('', ['description'], schema)
if (description) {
comments.push(' ' + description)
}
doc.commentBefore = comments.join('\n')
doc.spaceAfter = false
doc.spaceBefore = false
doc.contents.items.forEach(enhance(schema, []))
return Promise.resolve({
// schema,
// values,
yaml: doc.toString()
})
})
.then((out) => {
const content = `---
id: configuration
title: Configuration
---
<!-- THIS FILE IS BEING AUTO-GENERATED. DO NOT MODIFY IT AS ALL CHANGES WILL BE OVERWRITTEN.
OPEN AN ISSUE IF YOU WOULD LIKE TO MAKE ADJUSTMENTS HERE AND MAINTAINERS WILL HELP YOU LOCATE THE RIGHT
FILE -->
If file \`$HOME/.${config.projectSlug}.yaml\` exists, it will be used as a configuration file which supports all
configuration settings listed below.
You can load the config file from another source using the \`-c path/to/config.yaml\` or \`--config path/to/config.yaml\`
flag: \`${config.projectSlug} --config path/to/config.yaml\`.
Config files can be formatted as JSON, YAML and TOML. Some configuration values support reloading without server restart.
All configuration values can be set using environment variables, as documented below.
:::warning Disclaimer
This reference configuration documents all keys, also deprecated ones!
It is a reference for all possible configuration values.
If you are looking for an example configuration, it is better to try out the quickstart.
:::
To find out more about edge cases like setting string array values through environmental variables head to the
[Configuring ORY services](https://www.ory.sh/docs/ecosystem/configuring) section.
\`\`\`yaml
${out.yaml}
\`\`\``
return new Promise((resolve, reject) => {
fs.writeFile(
path.resolve(config.updateConfig.dst),
prettier.format(content, { ...prettierStyles, parser: 'markdown' }),
'utf8',
(err) => {
if (err) {
reject(err)
return
}
resolve()
}
)
})
})
.then(() => {
console.log('Done!')
})
.catch((err) => {
console.error(err)
process.exit(1)
})