-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
224 lines (129 loc) · 4.66 KB
/
Copy pathbuild.js
File metadata and controls
224 lines (129 loc) · 4.66 KB
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
const fs = require('fs')
const _ = require ('underscore')
const nunjucks = require('nunjucks')
const async = require('async')
fs.readFile('./schema/schema.json', 'utf8', function (err, data) {
if (err) throw err;
let schema = JSON.parse(data);
// console.log(JSON.stringify(schema,null,4))
const docs = serialize(schema, undefined, schema)
const sorted_docs = _.sortBy(docs, function(doc){return doc.title.toLowerCase()})
// console.log(JSON.stringify(docs,null,4))
// loop over the array with docobjects and try to add an example for each from the examples folder
async.eachOfSeries(sorted_docs, function(doc, key, cb_next_doc){
fs.readFile('./examples/' + doc.title + '.json', function (err, example_data){
if (err){
// couldn't fine file: tye next one
cb_next_doc()
} else {
// add the example to the doc
console.log(typeof example_data)
doc.example = JSON.stringify(JSON.parse(example_data), null, 4) // don't parse the JSON: we need the string
console.log(doc.example)
cb_next_doc()
}
})
}, function(err){
if (err) throw err
// render the readme with nunjucks
nunjucks.render('README_main.md',
{
docs: sorted_docs
}, function(err, res){
console.log('rendering finished')
fs.writeFile('README_built.md', res, function(err){
if (err) throw err
console.log('doc build finished')
})
})
})
});
const serialize = function(node, node_key, schema){
// console.log(node_key)
let doc_list = []
let doc_object = {}
// if multiple possible definitions with oneOf:
if (node.oneOf != undefined){
doc_object.oneOf = []
_.each(node.oneOf, function(choicenode){
const answer = create_doc_object(choicenode, undefined, schema)
doc_object.oneOf.push(answer.doc_object)
doc_list = _.union(doc_list, answer.doc_list)
})
doc_object.title = node.title
// else
} else {
const answer = create_doc_object(node, node_key, schema, doc_list)
doc_object = answer.doc_object
doc_list = _.union(doc_list, answer.doc_list)
}
doc_list.push(doc_object)
return doc_list
}
function create_doc_object(node, node_key, schema){
let result = {}
let doc_object = {}
let doc_list = []
_.each(node, function(value, key){
// serialize the properties and definitions
if (key == 'properties'|| key == 'definitions'){
doc_object[key] = []
// loop over the properties and serialize them one by one
_.each(node[key], function(subnode, subnode_key){
// add a link
let link = {}
link.property = subnode_key
if (subnode['$ref'] != undefined) {
const ref = subnode['$ref'].replace('#/definitions/','')
if (schema.definitions[ref] != undefined) {
link.link = schema.definitions[ref].title
} else {
console.log('=> ref not defined in definitions:', ref)
// console.log(schema.definitions.comment)
// throw 'error'
}
} else if (subnode.title != undefined){
doc_list = _.union(doc_list, serialize(subnode, subnode_key, schema))
link.link = subnode.title
} else {
doc_list = _.union(doc_list, serialize(subnode, subnode_key, schema))
link.link = ""
console.log('=> unable to define link because title not defined for:', subnode_key)
}
doc_object[key].push(link)
})
// serialize the additonalProperties
} else if (key == 'additonalProperties'){
// add a link
let link = {}
link.property = 'list'
if (node.additonalProperties['$ref'] != undefined) {
const ref = node.additonalProperties['$ref'].replace('#/definitions/','')
if (schema.definitions[ref] != undefined) {
link.link = schema.definitions[ref].title
} else {
console.log('ref not defined in definitions:', ref)
// console.log(schema.definitions)
}
} else if (node.additonalProperties.title != undefined){
doc_list = _.union(doc_list, serialize(node.additonalProperties, undefined, schema))
link.link = node.additonalProperties.title
} else {
doc_list = _.union(doc_list, serialize(node.additonalProperties, undefined, schema))
link.link = ""
console.log('unable to define link because title not defined for additonalProperties')
}
doc_object[key] = link
// serialize the definitions
} else {
doc_object[key] = value
}
})
// add the node_key if defined
if (node_key != undefined){
doc_object.name = node_key
}
result.doc_object = doc_object
result.doc_list = doc_list
return result
}