Skip to content

Commit

Permalink
feat(cli): generate JSON schema output
Browse files Browse the repository at this point in the history
fixes #176
  • Loading branch information
trieloff committed Dec 11, 2019
1 parent 3a45c6a commit dd18f3b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
23 changes: 22 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const logger = require('@adobe/helix-log');
const {
iter, pipe, filter, map, obj,
} = require('ferrum');
const npath = require('path');
const traverse = require('./lib/traverseSchema');
const build = require('./lib/markdownBuilder');
const { writereadme, writemarkdown } = require('./lib/writeMarkdown');
const readme = require('./lib/readmeBuilder');
const { loader } = require('./lib/schemaProxy');
const { writeSchema } = require('./lib/writeSchema');

const { info, error, debug } = logger;

Expand Down Expand Up @@ -115,11 +117,19 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio
map(path => schemaloader(require(path), path)),
// find contained schemas
traverse,
// build readme
);
})

.then(schemas => Promise.all([
(() => {
if (argv.x !== '-') {
writeSchema({
schemadir: argv.x,
origindir: argv.d,
})(schemas);
}
})(),

(() => {
if (argv.n) {
return pipe(
Expand Down Expand Up @@ -148,6 +158,17 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio
header: argv.h,
links: docs,
includeproperties: argv.p,
rewritelinks: (origin) => {
const mddir = argv.o;
const srcdir = argv.d;
const schemadir = argv.x !== '-' ? argv.x : argv.d;

const target = npath.relative(
mddir,
npath.resolve(schemadir, npath.relative(srcdir, origin)),
);
return target;
},
}),


Expand Down
10 changes: 0 additions & 10 deletions examples/schemas/complex.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@
"version": "1.0.0",
"testProperty": "test"
},
"reflist": {
"type": "array",
"items": {
"$ref": "https://example.com/schemas/simple",
"version": "1.0.0",
"testProperty": "test"
},
"version": "1.0.0",
"testProperty": "test"
},
"refobj": {
"type": "object",
"properties": {
Expand Down
7 changes: 5 additions & 2 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const s = require('./symbols');
const { gentitle } = require('./formattingTools');
const { keyword } = require('./keywords');

function build({ header, links = {}, includeproperties = [] } = {}) {
function build({
header, links = {}, includeproperties = [], rewritelinks = x => x,
} = {}) {
const formats = {
'date-time': {
label: i18n`date time`,
Expand Down Expand Up @@ -190,6 +192,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
* @param {*} schema
*/
function makeheader(schema) {
// console.log('making header for', schema[s.filename], schema[s.pointer]);
if (header) {
return [
heading(1, text(i18n`${gentitle(schema[s.titles], schema.type)} Schema`)),
Expand Down Expand Up @@ -217,7 +220,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
&& typeof schema[s.meta][prop.name] === 'object'
&& schema[s.meta][prop.name].link
&& schema[s.meta][prop.name].text) {
return tableCell(link(schema[s.meta][prop.name].link, i18n`open original schema`, [text(schema[s.meta][prop.name].text)]));
return tableCell(link(rewritelinks(schema[s.meta][prop.name].link), i18n`open original schema`, [text(schema[s.meta][prop.name].text)]));
}
const value = schema[s.meta] ? schema[s.meta][prop.name] : undefined;
if (prop[`${String(value)}label`]) {
Expand Down
45 changes: 45 additions & 0 deletions lib/writeSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const fs = require('fs-extra');
const path = require('path');
const s = require('./symbols');

function writeSchema({ schemadir, origindir }) {
const targetpath = filename => path.resolve(schemadir, path.relative(origindir, filename));


return (schemas) => {
console.log('writing schemas to', schemadir);

const realschemas = Object.values(schemas).filter(schema => !schema[s.parent]);
realschemas.forEach((schema) => {
// console.log('writing', schema[s.filename], 'to ', targetpath(schema[s.filename]));
const filename = targetpath(schema[s.filename]);
const dirname = path.dirname(filename);


const out = Object.assign({}, schema);
if (schema[s.meta] && schema[s.meta].description) {
// copy description from external file
out.description = schema[s.meta].description;
}
if (schema.examples && Array.isArray(schema.examples) && schema.examples.length > 0) {
// copy examples from external files
out.examples = [...schema.examples];
}
fs.mkdirpSync(dirname);
fs.writeJsonSync(filename, out);
});
};
}

module.exports = { writeSchema };
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"js-yaml": "^3.13.1",
"mdast-builder": "^1.1.1",
"mdast-util-to-string": "^1.0.7",
"mkdirp": "^0.5.1",
"mocha": "^6.2.2",
"readdirp": "^3.3.0",
"remark-parse": "^7.0.2",
Expand Down

0 comments on commit dd18f3b

Please sign in to comment.