Skip to content

Commit dd18f3b

Browse files
committed
feat(cli): generate JSON schema output
fixes #176
1 parent 3a45c6a commit dd18f3b

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

cli.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ const logger = require('@adobe/helix-log');
2020
const {
2121
iter, pipe, filter, map, obj,
2222
} = require('ferrum');
23+
const npath = require('path');
2324
const traverse = require('./lib/traverseSchema');
2425
const build = require('./lib/markdownBuilder');
2526
const { writereadme, writemarkdown } = require('./lib/writeMarkdown');
2627
const readme = require('./lib/readmeBuilder');
2728
const { loader } = require('./lib/schemaProxy');
29+
const { writeSchema } = require('./lib/writeSchema');
2830

2931
const { info, error, debug } = logger;
3032

@@ -115,11 +117,19 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio
115117
map(path => schemaloader(require(path), path)),
116118
// find contained schemas
117119
traverse,
118-
// build readme
119120
);
120121
})
121122

122123
.then(schemas => Promise.all([
124+
(() => {
125+
if (argv.x !== '-') {
126+
writeSchema({
127+
schemadir: argv.x,
128+
origindir: argv.d,
129+
})(schemas);
130+
}
131+
})(),
132+
123133
(() => {
124134
if (argv.n) {
125135
return pipe(
@@ -148,6 +158,17 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio
148158
header: argv.h,
149159
links: docs,
150160
includeproperties: argv.p,
161+
rewritelinks: (origin) => {
162+
const mddir = argv.o;
163+
const srcdir = argv.d;
164+
const schemadir = argv.x !== '-' ? argv.x : argv.d;
165+
166+
const target = npath.relative(
167+
mddir,
168+
npath.resolve(schemadir, npath.relative(srcdir, origin)),
169+
);
170+
return target;
171+
},
151172
}),
152173

153174

examples/schemas/complex.schema.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@
2626
"version": "1.0.0",
2727
"testProperty": "test"
2828
},
29-
"reflist": {
30-
"type": "array",
31-
"items": {
32-
"$ref": "https://example.com/schemas/simple",
33-
"version": "1.0.0",
34-
"testProperty": "test"
35-
},
36-
"version": "1.0.0",
37-
"testProperty": "test"
38-
},
3929
"refobj": {
4030
"type": "object",
4131
"properties": {

lib/markdownBuilder.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const s = require('./symbols');
2222
const { gentitle } = require('./formattingTools');
2323
const { keyword } = require('./keywords');
2424

25-
function build({ header, links = {}, includeproperties = [] } = {}) {
25+
function build({
26+
header, links = {}, includeproperties = [], rewritelinks = x => x,
27+
} = {}) {
2628
const formats = {
2729
'date-time': {
2830
label: i18n`date time`,
@@ -190,6 +192,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
190192
* @param {*} schema
191193
*/
192194
function makeheader(schema) {
195+
// console.log('making header for', schema[s.filename], schema[s.pointer]);
193196
if (header) {
194197
return [
195198
heading(1, text(i18n`${gentitle(schema[s.titles], schema.type)} Schema`)),
@@ -217,7 +220,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
217220
&& typeof schema[s.meta][prop.name] === 'object'
218221
&& schema[s.meta][prop.name].link
219222
&& schema[s.meta][prop.name].text) {
220-
return tableCell(link(schema[s.meta][prop.name].link, i18n`open original schema`, [text(schema[s.meta][prop.name].text)]));
223+
return tableCell(link(rewritelinks(schema[s.meta][prop.name].link), i18n`open original schema`, [text(schema[s.meta][prop.name].text)]));
221224
}
222225
const value = schema[s.meta] ? schema[s.meta][prop.name] : undefined;
223226
if (prop[`${String(value)}label`]) {

lib/writeSchema.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
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+
20+
return (schemas) => {
21+
console.log('writing schemas to', schemadir);
22+
23+
const realschemas = Object.values(schemas).filter(schema => !schema[s.parent]);
24+
realschemas.forEach((schema) => {
25+
// console.log('writing', schema[s.filename], 'to ', targetpath(schema[s.filename]));
26+
const filename = targetpath(schema[s.filename]);
27+
const dirname = path.dirname(filename);
28+
29+
30+
const out = Object.assign({}, schema);
31+
if (schema[s.meta] && schema[s.meta].description) {
32+
// copy description from external file
33+
out.description = schema[s.meta].description;
34+
}
35+
if (schema.examples && Array.isArray(schema.examples) && schema.examples.length > 0) {
36+
// copy examples from external files
37+
out.examples = [...schema.examples];
38+
}
39+
fs.mkdirpSync(dirname);
40+
fs.writeJsonSync(filename, out);
41+
});
42+
};
43+
}
44+
45+
module.exports = { writeSchema };

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"js-yaml": "^3.13.1",
2525
"mdast-builder": "^1.1.1",
2626
"mdast-util-to-string": "^1.0.7",
27-
"mkdirp": "^0.5.1",
2827
"mocha": "^6.2.2",
2928
"readdirp": "^3.3.0",
3029
"remark-parse": "^7.0.2",

0 commit comments

Comments
 (0)