1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const fs = require('fs-extra');
|
13 | const path = require('path');
|
14 | const s = require('./symbols');
|
15 |
|
16 | function writeSchema({ schemadir, origindir }) {
|
17 | const targetpath = (filename) => path.resolve(schemadir, path.relative(origindir, filename));
|
18 |
|
19 | return (schemas) => {
|
20 | console.log('writing schemas to', schemadir);
|
21 |
|
22 | const realschemas = Object.values(schemas).filter((schema) => !schema[s.parent]);
|
23 | realschemas.forEach((schema) => {
|
24 |
|
25 | const filename = targetpath(schema[s.filename]);
|
26 | const dirname = path.dirname(filename);
|
27 |
|
28 | const out = fs.readJSONSync(schema[s.filename]);
|
29 | if (schema[s.meta] && schema[s.meta].description) {
|
30 |
|
31 | out.description = schema[s.meta].description;
|
32 | }
|
33 | if (schema.examples && Array.isArray(schema.examples) && schema.examples.length > 0) {
|
34 |
|
35 | out.examples = [...schema.examples];
|
36 | }
|
37 | fs.mkdirpSync(dirname);
|
38 | fs.writeJsonSync(filename, out);
|
39 | });
|
40 | };
|
41 | }
|
42 |
|
43 | module.exports = { writeSchema };
|