diff --git a/cli.js b/cli.js index af145b62..0ecbe224 100755 --- a/cli.js +++ b/cli.js @@ -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; @@ -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( @@ -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; + }, }), diff --git a/examples/schemas/complex.schema.json b/examples/schemas/complex.schema.json index b4732956..cd8de566 100644 --- a/examples/schemas/complex.schema.json +++ b/examples/schemas/complex.schema.json @@ -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": { diff --git a/lib/markdownBuilder.js b/lib/markdownBuilder.js index 8cae01f8..2b2eed76 100644 --- a/lib/markdownBuilder.js +++ b/lib/markdownBuilder.js @@ -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`, @@ -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`)), @@ -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`]) { diff --git a/lib/writeSchema.js b/lib/writeSchema.js new file mode 100644 index 00000000..fccf40ee --- /dev/null +++ b/lib/writeSchema.js @@ -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 }; diff --git a/package.json b/package.json index e5cb210f..3d8bf249 100644 --- a/package.json +++ b/package.json @@ -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",