diff --git a/cli.js b/cli.js index bafbbf729a9a5d65ddbe468390f2d3858f4ae887..e388ff6123730084313845412236df07a41d8703 100644 --- a/cli.js +++ b/cli.js @@ -14,7 +14,6 @@ var fs = Promise.promisifyAll(require('fs')); var readdirp = require('readdirp'); var Ajv = require('ajv'); -var processFile = require('./lib/main'); var Schema = require('./lib/schema'); var readSchemaFile = require('./lib/readSchemaFile'); diff --git a/lib/schema.js b/lib/schema.js index 10d34e303af2c7375570990ca9288ccc9b1bcca2..6911fdeb66df328cae960ebac7c2bd8adb93b2ef 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -5,14 +5,10 @@ * of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -'use strict'; -var validUrl = require('valid-url'); -var util = require('util'); var path = require('path'); var _ = require('lodash'); var logger = require('winston'); var readdirp = require('readdirp'); -var url = require('url'); var Promise=require('bluebird'); var fs = Promise.promisifyAll(require('fs')); var Writer=require('./writeFiles'); @@ -22,7 +18,6 @@ var pointer = require('json-pointer'); var smap; //TODO remove global var sPath; var wmap={}; -var async = require('async'); function get$refType(refValue){ var startpart = '', endpart = '', refType = ''; var arr = refValue.split('#'); @@ -32,8 +27,7 @@ function get$refType(refValue){ //TODO yRelNoDef //relative-- yRelWithDef, yRelNoDef, //absolute-- yAbsWithDef, yAbsFSchema, yAbsWithFragment - var refType='',currStartId; - var traversedRefMap; + var refType=''; var deff='/definitions/'; //if( absUrlRegex.test(refVal) ){ @@ -42,7 +36,11 @@ function get$refType(refValue){ if (endpart.startsWith(deff)){ refType = 'yAbsWithDef'; } else { - if (endpart.length == 0) {refType = 'yAbsFSchema';} else {refType = 'yAbsWithFragment';} + if (endpart.length === 0) { + refType = 'yAbsFSchema'; + } else { + refType = 'yAbsWithFragment'; + } } } } else { @@ -65,14 +63,109 @@ function normaliseLinks(obj,refArr){ return { $linkVal , $linkPath }; } } +var resolve$ref = Promise.method(function(val,base$id){ + let obj,link; + if (!(base$id in wmap) ) {wmap[base$id] = {};} + let refArr = get$refType(val['$ref']); + if (refArr.refType === 'yRelWithDef'){ + refArr.startpart = base$id; + } + if (smap[refArr.startpart]){ + obj=smap[refArr.startpart].jsonSchema; + if (refArr.refType !== 'yRelWithDef'){ + link = normaliseLinks(obj,refArr); + if (!wmap[base$id][refArr.startpart]){ + wmap[base$id][refArr.startpart]=link; + } + + } + if (refArr.refType === 'yAbsFSchema'){ + val.$linkVal = link.$linkVal; + val.$linkPath = link.$linkPath; + return val; + } + + if (pointer.has(obj,refArr.endpart)){ + var ischema = _.cloneDeep(pointer.get(obj, refArr.endpart)); + _.forOwn(val,(v,k)=>{ + if (k !== '$ref'){ + ischema[k]=v; + } + }); + return processISchema(ischema,refArr.startpart); + } + } +}); +var processFurther = Promise.method(function(val,key,$id){ + let base$id =$id; + if (val['$ref']){ + return resolve$ref(val,base$id); + } else { + if (val['items'] && val['type'] === 'array'){ + if (val['items']['$ref']){ + resolve$ref(val['items']).then((s)=>{ + _.forOwn(s,(v,k)=>{ + if (k !== '$ref'){ + val['items'][k]=v; + } + }); + }); + } + } + //TODO if any other keyword + return val; + } +}); +function processISchema() {}; // define w/ function so it gets hoisted and we avoid eslint errors about what is defined first: processISchema or resolve$ref. Both rely on each other! +processISchema = Promise.method(function(schema,base$id){ + if (!(base$id in wmap) ) {wmap[base$id] = {};} + if (schema['anyOf'] || schema['oneOf']){ + // var $definitions=[] + schema['type'] = schema['anyOf'] ? 'anyOf' : 'oneOf'; + let arr = schema['anyOf']? schema['anyOf'] : schema['oneOf']; + _.each(arr,function(value,index) { + if (value['$ref']){ + resolve$ref(value,base$id).then((piSchema)=>{ + delete arr[index]; + arr[index]=piSchema; + }); + } else { + processISchema(value,base$id).then((piSchema)=>{ + delete arr[index]; + arr[index]=piSchema; + }); + } + }); + // schema["$definitions"] = $definitions; + return schema; + } + + if (schema['items'] ){ + + let val=schema['items']; + if (!schema['type']) {schema['type'] = 'array';} + if (_.isArray(val)){ + //TODO + + } else { + if (val['$ref']){ + resolve$ref(val,base$id).then((piSchema)=>{//check // not sending correct id + schema['items']=piSchema; + }); + } else { + //TODO if such a scenario + } + } + } + // schema.$definitions = $definitions + return schema; +}); function processSchema(schema){ - var propertyMap={}; return new Promise((resolve,reject)=>{ if (!schema.properties) {schema.properties={};} var $id = schema['$id'] || schema['id']; var base$id = $id; if (!(base$id in wmap)) {wmap[base$id] = {};} - var required = schema['required'] || []; if (schema['allOf']){ _.each(schema['allOf'],function(value) { if (value['$ref']){ @@ -83,7 +176,7 @@ function processSchema(schema){ } if (smap[refArr.startpart]){ obj=smap[refArr.startpart].jsonSchema; - if (refArr.refType != 'yRelWithDef'){ + if (refArr.refType !== 'yRelWithDef'){ link=normaliseLinks(obj,refArr); if (!wmap[base$id][refArr.startpart]){ wmap[base$id][refArr.startpart]=link; @@ -96,7 +189,7 @@ function processSchema(schema){ if (refArr.refType === 'yAbsFSchema'){ processSchema(ischema).then((psSchema) => { if ( psSchema['properties'] ){ - _.forOwn(psSchema['properties'],(val,key,object) => { + _.forOwn(psSchema['properties'],(val,key) => { processFurther(val,key,refArr.startpart).then((pfSchema)=>{ if (pfSchema){ schema.properties[key] = pfSchema; @@ -119,7 +212,7 @@ function processSchema(schema){ } else { if ( ischema['properties'] ){ - _.forOwn(ischema['properties'],(val,key,object) => { + _.forOwn(ischema['properties'],(val,key) => { processFurther(val,key,refArr.startpart).then((pfSchema)=>{ if (pfSchema){ schema.properties[key] = pfSchema; @@ -158,7 +251,7 @@ function processSchema(schema){ resolve(schema); } else if (schema['properties']){ - _.forOwn(schema['properties'],(val,key,object) => { + _.forOwn(schema['properties'],(val,key) => { processFurther(val,key,base$id).then((pfSchema)=>{ if (pfSchema){ schema.properties[key] = pfSchema; @@ -184,108 +277,6 @@ function processSchema(schema){ } -var resolve$ref = Promise.method(function(val,base$id){ - - let obj,link; - if (!(base$id in wmap) ) {wmap[base$id] = {};} - let refArr = get$refType(val['$ref']); - if (refArr.refType === 'yRelWithDef'){ - refArr.startpart = base$id; - } - if (smap[refArr.startpart]){ - obj=smap[refArr.startpart].jsonSchema; - if (refArr.refType != 'yRelWithDef'){ - link = normaliseLinks(obj,refArr); - if (!wmap[base$id][refArr.startpart]){ - wmap[base$id][refArr.startpart]=link; - } - - } - if (refArr.refType === 'yAbsFSchema'){ - val.$linkVal = link.$linkVal; - val.$linkPath = link.$linkPath; - return val; - } - - if (pointer.has(obj,refArr.endpart)){ - var ischema = _.cloneDeep(pointer.get(obj, refArr.endpart)); - _.forOwn(val,(v,k)=>{ - if (k != '$ref'){ - ischema[k]=v; - } - }); - return processISchema(ischema,refArr.startpart); - - } - } - - -}); - -var processISchema = Promise.method(function(schema,base$id){ - let refArr; - if (!(base$id in wmap) ) {wmap[base$id] = {};} - if (schema['anyOf'] || schema['oneOf']){ - // var $definitions=[] - schema['type'] = schema['anyOf'] ? 'anyOf' : 'oneOf'; - let arr = schema['anyOf']? schema['anyOf'] : schema['oneOf']; - _.each(arr,function(value,index) { - if (value['$ref']){ - resolve$ref(value,base$id).then((piSchema)=>{ - delete arr[index]; - arr[index]=piSchema; - }); - } else { - processISchema(value,base$id).then((piSchema)=>{ - delete arr[index]; - arr[index]=piSchema; - }); - } - }); - // schema["$definitions"] = $definitions; - return schema; - } - - if (schema['items'] ){ - - let val=schema['items']; - if (!schema['type']) {schema['type'] = 'array';} - if (_.isArray(val)){ - //TODO - - } else { - if (val['$ref']){ - resolve$ref(val,base$id).then((piSchema)=>{//check // not sending correct id - schema['items']=piSchema; - }); - } else { - //TODO if such a scenario - } - } - } - // schema.$definitions = $definitions - return schema; -}); -var processFurther = Promise.method(function(val,key,$id){ - let base$id =$id; - if (val['$ref']){ - return resolve$ref(val,base$id); - } else { - if (val['items'] && val['type'] === 'array'){ - if (val['items']['$ref']){ - resolve$ref(val['items']).then((s)=>{ - _.forOwn(s,(v,k)=>{ - if (k != '$ref'){ - val['items'][k]=v; - } - }); - }); - } - } - //TODO if any other keyword - return val; - } -}); var Schema=function(ajv,schemaMap){ this._ajv = ajv; this._schemaPathMap=schemaMap; @@ -294,19 +285,20 @@ var Schema=function(ajv,schemaMap){ Schema.resolveRef=function(key,obj,currpath){ if (key === '$ref'){ var refVal = obj[key]; + var temp; if ( absUrlRegex.test(refVal) ){ let parsedUrl = refVal.split('#'); let basepath = parsedUrl[0] ; if (basepath in this._schemaPathMap){ let newpath = path.relative(path.dirname(currpath),this._schemaPathMap[basepath].filePath).replace(/\\/g, '/'); //to cater windows paths obj['$ref'] = newpath; - var temp = newpath.slice(0, -5).split('/'); + temp = newpath.slice(0, -5).split('/'); obj.$linkVal = path.basename(newpath).slice(0, -5); obj.$linkPath = temp.join('/')+'.md'; //TODO display with title or file path name title } else { obj.$linkPath = refVal; - var temp = refVal.split('/'); + temp = refVal.split('/'); obj.$linkVal = temp.pop() || temp.pop(); } @@ -314,7 +306,7 @@ Schema.resolveRef=function(key,obj,currpath){ obj.$linkVal = refVal.slice(deff.length); obj.$linkPath = '#'+obj.$linkVal.replace(/ /g,'-'); } else if (refVal.endsWith('json')){ - var temp = refVal.slice(0, -5).split('/'); + temp = refVal.slice(0, -5).split('/'); obj.$linkVal = temp[temp.length - 1]; obj.$linkPath = temp.join('/')+'.md'; } @@ -324,6 +316,7 @@ Schema.resolveRef=function(key,obj,currpath){ return; }; +/* The following function does not seem to be used anymore! var traverseSchema = function(object,schemaFilePath){ return new Promise((resolve,reject) => { var recurse=function(curr,key,prev){ @@ -338,8 +331,8 @@ var traverseSchema = function(object,schemaFilePath){ }; resolve(recurse(object)); }); - }; +*/ Schema.getExamples = function(filePath,schema){ var exampleFileNames=[]; @@ -351,7 +344,7 @@ Schema.getExamples = function(filePath,schema){ readdirp({ root: dirname, fileFilter: filename }) .on('data',(entry)=>exampleFileNames.push(entry.fullPath)) .on('end',() => resolve(exampleFileNames)) - .on('error',() => reject(err)); + .on('error',(err) => reject(err)); }).then((exampleFileNames)=>{ if (exampleFileNames.length > 0){ var validate=this._ajv.compile(schema); @@ -376,7 +369,7 @@ Schema.getDescription = function(filePath,schema){ schema.description=description; return schema; }) - .catch((err) => { + .catch(() => { return schema; }); @@ -404,16 +397,16 @@ Schema.load = function(schemaMap,schemaPath,outDir,metaElements){ let schema = schemaMap[schemaKey].jsonSchema; sPath=schemaMap[schemaKey].filePath; return Schema.getExamples(schemaMap[schemaKey].filePath,schema) - .then((egs_schema) => Schema.getDescription(schemaMap[schemaKey].filePath,egs_schema)) - .then((all_schema) => { - var schemaClone = _.cloneDeep(all_schema); + .then((egsSchema) => Schema.getDescription(schemaMap[schemaKey].filePath,egsSchema)) + .then((allSchema) => { + var schemaClone = _.cloneDeep(allSchema); // return Promise.props({ // wSchema:schemaClone, - // mSchema:traverseSchema(all_schema,schemaMap[schemaKey].filePath) + // mSchema:traverseSchema(allSchema,schemaMap[schemaKey].filePath) // }) return processSchema(schemaClone).then((mSchema)=>{ mSchema.metaElements=metaElements; - return { mSchema:mSchema,wSchema:all_schema,dep:wmap }; + return { mSchema:mSchema,wSchema:allSchema,dep:wmap }; }); }).then((object)=>{ return Promise.all([ Writer.generateMarkdown(schemaMap[schemaKey].filePath,object.mSchema,schemaPath,outDir,object.dep), diff --git a/lib/writeFiles.js b/lib/writeFiles.js index 870870741e526afac969990596b647ad562f918b..9f72c254393dbee03d853d218134f078614a9072 100644 --- a/lib/writeFiles.js +++ b/lib/writeFiles.js @@ -9,13 +9,9 @@ var Promise=require('bluebird'); var fs = Promise.promisifyAll(require('fs')); var path = require('path'); var _ = require('lodash'); -var async = require('async'); var ejs = require('ejs'); -var logger = require('winston'); var mkdirp = Promise.promisify(require('mkdirp')); -var readdirp = require('readdirp'); var validUrl = require('valid-url'); -var url = require('url'); // var Writer = function(schemaPath,outDir){ // this._outDir = outDir; @@ -24,7 +20,7 @@ var url = require('url'); var Writer = {}; var writeFile = function(outputDir, fileName, data) { if (!fs.existsSync(outputDir)){ - return mkdirp(outputDir).then((err)=>{ + return mkdirp(outputDir).then(()=>{ return fs.writeFileAsync(path.join(outputDir, fileName), data); }); } else {