Skip to content
Snippets Groups Projects
Commit e42f9b4b authored by Lars Trieloff's avatar Lars Trieloff
Browse files

[trivial] enforce consistent comma spacing

parent 021c0986
Branches
Tags
No related merge requests found
......@@ -29,6 +29,7 @@
"before": true,
"after": true
}],
"space-unary-ops": 2
"space-unary-ops": 2,
"comma-spacing": ["error", { "before": false, "after": true }]
}
}
......@@ -28,8 +28,8 @@ var argv = require('optimist')
.describe('o', 'path to output directory')
.default('o', path.resolve(path.join('.', 'out')))
.alias('m', 'meta')
.describe('m','add metadata elements to .md files Eg -m template=reference. Multiple values can be added by repeating the flag Eg: -m template=reference -m hide-nav=true')
.alias('s','metaSchema')
.describe('m', 'add metadata elements to .md files Eg -m template=reference. Multiple values can be added by repeating the flag Eg: -m template=reference -m hide-nav=true')
.alias('s', 'metaSchema')
.describe('s', 'Custom meta schema path to validate schemas')
.alias('x', 'schema-out')
.describe('x', 'output JSON Schema files including description and validated examples in the _new folder at output directory')
......@@ -43,7 +43,7 @@ var argv = require('optimist')
})
.argv;
var ajv = new Ajv({ allErrors: true , messages:true });
var ajv = new Ajv({ allErrors: true, messages:true });
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
var schemaPathMap = {};
var metaElements = {};
......@@ -58,7 +58,7 @@ if (argv.s){
if (argv.m) {
if (_.isArray(argv.m)){
_.each(argv.m,function(item){
_.each(argv.m, function(item){
var meta=item.split('=');
if (meta.length === 2) {
metaElements[meta[0]] = meta[1];
......@@ -77,11 +77,11 @@ if (target.isDirectory()) {
// the ajv json validator will be passed into the main module to help with processing
var files=[];
readdirp({ root: schemaPath, fileFilter: '*.schema.json' })
.on('data',(entry) => {
.on('data', (entry) => {
files.push(entry.fullPath);
ajv.addSchema(require(entry.fullPath), entry.fullPath);
})
.on('end',() => {
.on('end', () => {
Schema.setAjv(ajv);
Schema.setSchemaPathMap(schemaPathMap);
return Promise.reduce(files, readSchemaFile, schemaPathMap)
......@@ -97,7 +97,7 @@ if (target.isDirectory()) {
process.exit(1);
});
})
.on('error',(err)=>{
.on('error', (err)=>{
logger.error(err);
process.exit(1);
});
......@@ -108,7 +108,7 @@ if (target.isDirectory()) {
Schema.setAjv(ajv);
Schema.setSchemaPathMap(schemaPathMap);
logger.info('finished reading %s, beginning processing....', schemaPath);
return Schema.load(schemaMap , schemaPath, outDir, schemaDir, metaElements);
return Schema.load(schemaMap, schemaPath, outDir, schemaDir, metaElements);
})
.then(() => {
logger.info('Processing complete.');
......
......@@ -49,22 +49,22 @@ function get$refType(refValue){
}
}
// }
return { startpart,endpart,refType };
return { startpart, endpart, refType };
}
function normaliseLinks(obj,refArr){
function normaliseLinks(obj, refArr){
let basepath = refArr.startpart ;
let $linkVal = '', $linkPath = '';
if (basepath in smap){
let newpath = path.relative(path.dirname(sPath),smap[basepath].filePath).replace(/\\/g, '/'); //to cater windows paths
let newpath = path.relative(path.dirname(sPath), smap[basepath].filePath).replace(/\\/g, '/'); //to cater windows paths
let temp = newpath.slice(0, -5).split('/');
$linkVal = obj['title'] ? obj['title'] : path.basename(newpath).slice(0, -5);
$linkPath = temp.join('/')+'.md';
return { $linkVal , $linkPath };
return { $linkVal, $linkPath };
}
}
var resolve$ref = Promise.method(function(val,base$id){
let obj,link;
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'){
......@@ -73,7 +73,7 @@ var resolve$ref = Promise.method(function(val,base$id){
if (smap[refArr.startpart]){
obj=smap[refArr.startpart].jsonSchema;
if (refArr.refType !== 'yRelWithDef'){
link = normaliseLinks(obj,refArr);
link = normaliseLinks(obj, refArr);
if (!wmap[base$id][refArr.startpart]){
wmap[base$id][refArr.startpart]=link;
}
......@@ -85,26 +85,26 @@ var resolve$ref = Promise.method(function(val,base$id){
return val;
}
if (pointer.has(obj,refArr.endpart)){
if (pointer.has(obj, refArr.endpart)){
var ischema = _.cloneDeep(pointer.get(obj, refArr.endpart));
_.forOwn(val,(v,k)=>{
_.forOwn(val, (v, k)=>{
if (k !== '$ref'){
ischema[k]=v;
}
});
return processISchema(ischema,refArr.startpart);
return processISchema(ischema, refArr.startpart);
}
}
});
var processFurther = Promise.method(function(val,key,$id){
var processFurther = Promise.method(function(val, key, $id){
let base$id =$id;
if (val['$ref']){
return resolve$ref(val,base$id);
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)=>{
_.forOwn(s, (v, k)=>{
if (k !== '$ref'){
val['items'][k]=v;
}
......@@ -117,20 +117,20 @@ var processFurther = Promise.method(function(val,key,$id){
}
});
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){
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) {
_.each(arr, function(value, index) {
if (value['$ref']){
resolve$ref(value,base$id).then((piSchema)=>{
resolve$ref(value, base$id).then((piSchema)=>{
delete arr[index];
arr[index]=piSchema;
});
} else {
processISchema(value,base$id).then((piSchema)=>{
processISchema(value, base$id).then((piSchema)=>{
delete arr[index];
arr[index]=piSchema;
});
......@@ -149,7 +149,7 @@ processISchema = Promise.method(function(schema,base$id){
} else {
if (val['$ref']){
resolve$ref(val,base$id).then((piSchema)=>{//check // not sending correct id
resolve$ref(val, base$id).then((piSchema)=>{//check // not sending correct id
schema['items']=piSchema;
});
} else {
......@@ -167,9 +167,9 @@ function processSchema(schema){
var base$id = $id;
if (!(base$id in wmap)) {wmap[base$id] = {};}
if (schema['allOf']){
_.each(schema['allOf'],function(value) {
_.each(schema['allOf'], function(value) {
if (value['$ref']){
let obj,link;
let obj, link;
var refArr = get$refType(value['$ref']);
if (refArr.refType === 'yRelWithDef'){
refArr.startpart = base$id;
......@@ -177,19 +177,19 @@ function processSchema(schema){
if (smap[refArr.startpart]){
obj=smap[refArr.startpart].jsonSchema;
if (refArr.refType !== 'yRelWithDef'){
link=normaliseLinks(obj,refArr);
link=normaliseLinks(obj, refArr);
if (!wmap[base$id][refArr.startpart]){
wmap[base$id][refArr.startpart]=link;
}
}
if (pointer.has(obj,refArr.endpart)){
if (pointer.has(obj, refArr.endpart)){
var ischema = _.cloneDeep(pointer.get(obj, refArr.endpart));
if (refArr.refType === 'yAbsFSchema'){
processSchema(ischema).then((psSchema) => {
if ( psSchema['properties'] ){
_.forOwn(psSchema['properties'],(val,key) => {
processFurther(val,key,refArr.startpart).then((pfSchema)=>{
_.forOwn(psSchema['properties'], (val, key) => {
processFurther(val, key, refArr.startpart).then((pfSchema)=>{
if (pfSchema){
schema.properties[key] = pfSchema;
schema.properties[key].$oSchema={};
......@@ -211,8 +211,8 @@ function processSchema(schema){
} else {
if ( ischema['properties'] ){
_.forOwn(ischema['properties'],(val,key) => {
processFurther(val,key,refArr.startpart).then((pfSchema)=>{
_.forOwn(ischema['properties'], (val, key) => {
processFurther(val, key, refArr.startpart).then((pfSchema)=>{
if (pfSchema){
schema.properties[key] = pfSchema;
if (refArr.refType === 'yAbsWithDef'){
......@@ -239,7 +239,7 @@ function processSchema(schema){
} else {
_.forOwn(value,function(val,key){
_.forOwn(value, function(val, key){
schema[key]=val;
//
});
......@@ -250,8 +250,8 @@ function processSchema(schema){
resolve(schema);
} else if (schema['properties']){
_.forOwn(schema['properties'],(val,key) => {
processFurther(val,key,base$id).then((pfSchema)=>{
_.forOwn(schema['properties'], (val, key) => {
processFurther(val, key, base$id).then((pfSchema)=>{
if (pfSchema){
schema.properties[key] = pfSchema;
......@@ -276,12 +276,12 @@ function processSchema(schema){
}
var Schema=function(ajv,schemaMap){
var Schema=function(ajv, schemaMap){
this._ajv = ajv;
this._schemaPathMap=schemaMap;
};
Schema.resolveRef=function(key,obj,currpath){
Schema.resolveRef=function(key, obj, currpath){
if (key === '$ref'){
var refVal = obj[key];
var temp;
......@@ -289,7 +289,7 @@ Schema.resolveRef=function(key,obj,currpath){
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
let newpath = path.relative(path.dirname(currpath), this._schemaPathMap[basepath].filePath).replace(/\\/g, '/'); //to cater windows paths
obj['$ref'] = newpath;
temp = newpath.slice(0, -5).split('/');
obj.$linkVal = path.basename(newpath).slice(0, -5);
......@@ -303,7 +303,7 @@ Schema.resolveRef=function(key,obj,currpath){
} else if (refVal.startsWith(deff)) {
obj.$linkVal = refVal.slice(deff.length);
obj.$linkPath = '#'+obj.$linkVal.replace(/ /g,'-');
obj.$linkPath = '#'+obj.$linkVal.replace(/ /g, '-');
} else if (refVal.endsWith('json')){
temp = refVal.slice(0, -5).split('/');
obj.$linkVal = temp[temp.length - 1];
......@@ -333,21 +333,21 @@ var traverseSchema = function(object,schemaFilePath){
};
*/
Schema.getExamples = function(filePath,schema){
Schema.getExamples = function(filePath, schema){
var exampleFileNames=[];
var examples=[];
var dirname=path.dirname(filePath);
var filename=path.basename(filePath,path.extname(filePath));
var filename=path.basename(filePath, path.extname(filePath));
filename=filename.split('.')[0]+'.example.*.json';
return new Promise((resolve,reject) => {
return new Promise((resolve, reject) => {
readdirp({ root: dirname, fileFilter: filename })
.on('data',(entry)=>exampleFileNames.push(entry.fullPath))
.on('end',() => resolve(exampleFileNames))
.on('error',(err) => reject(err));
.on('data', (entry)=>exampleFileNames.push(entry.fullPath))
.on('end', () => resolve(exampleFileNames))
.on('error', (err) => reject(err));
}).then((exampleFileNames)=>{
if (exampleFileNames.length > 0){
var validate=this._ajv.compile(schema);
return Promise.map(exampleFileNames,(entry)=>{
return Promise.map(exampleFileNames, (entry)=>{
return fs.readFileAsync(entry).then((example)=>{
var data = JSON.parse(example.toString());
var valid = validate(data);
......@@ -358,12 +358,12 @@ Schema.getExamples = function(filePath,schema){
});
};
Schema.getDescription = function(filePath,schema){
Schema.getDescription = function(filePath, schema){
var temp=path.basename(filePath,path.extname(filePath));
var temp=path.basename(filePath, path.extname(filePath));
//TODO should err be thrown here?
temp=temp.split('.')[0]+'.description.md';
return fs.readFileAsync(path.resolve(path.dirname(filePath),temp),'utf8')
return fs.readFileAsync(path.resolve(path.dirname(filePath), temp), 'utf8')
.then((description) => {
schema.description=description;
return schema;
......@@ -393,7 +393,7 @@ Schema.load = function(schemaMap, schemaPath, docDir, schemaDir, metaElements) {
schemaDir = schemaDir ? schemaDir : docDir;
smap=schemaMap;
let keys = Object.keys(schemaMap);
return Promise.mapSeries(keys,(schemaKey) => {
return Promise.mapSeries(keys, (schemaKey) => {
var props = Object.keys(wmap);
for (var i = 0; i < props.length; i++) {
......@@ -402,9 +402,9 @@ Schema.load = function(schemaMap, schemaPath, docDir, schemaDir, metaElements) {
let schema = schemaMap[schemaKey].jsonSchema;
sPath=schemaMap[schemaKey].filePath;
return Schema.getExamples(schemaMap[schemaKey].filePath,schema)
.then((egsSchema) => Schema.getDescription(schemaMap[schemaKey].filePath,egsSchema))
sPath = schemaMap[schemaKey].filePath;
return Schema.getExamples(schemaMap[schemaKey].filePath, schema)
.then((egsSchema) => Schema.getDescription(schemaMap[schemaKey].filePath, egsSchema))
.then((allSchema) => {
var schemaClone = _.cloneDeep(allSchema);
// return Promise.props({
......@@ -413,18 +413,14 @@ Schema.load = function(schemaMap, schemaPath, docDir, schemaDir, metaElements) {
// })
return processSchema(schemaClone).then((mSchema)=>{
mSchema.metaElements=metaElements;
return { mSchema:mSchema,wSchema:allSchema,dep:wmap };
return { mSchema:mSchema, wSchema:allSchema, dep:wmap };
});
}).then((object)=>{
return Promise.all([ Writer.generateMarkdown(schemaMap[schemaKey].filePath,object.mSchema,schemaPath,docDir,object.dep),
Writer.generateNewSchemaFiles(schemaMap[schemaKey].filePath,object.wSchema,schemaPath,schemaDir) ]);
return Promise.all([
Writer.generateMarkdown(schemaMap[schemaKey].filePath, object.mSchema, schemaPath, docDir, object.dep),
Writer.generateNewSchemaFiles(schemaMap[schemaKey].filePath, object.wSchema, schemaPath, schemaDir) ]);
});
// })
});
// .then(()=>{
// // console.log(JSON.stringify(wmap));
// })
};
......
......@@ -28,7 +28,7 @@ var writeFile = function(outputDir, fileName, data) {
}
};
Writer.generateMarkdown = function(filename, schema,schemaPath,outDir,dependencyMap) {
Writer.generateMarkdown = function(filename, schema, schemaPath, outDir, dependencyMap) {
var ctx = {
schema: schema,
......@@ -37,7 +37,7 @@ Writer.generateMarkdown = function(filename, schema,schemaPath,outDir,dependency
dependencyMap:dependencyMap
};
ejs.renderFile(path.join(__dirname,'../templates/md/topSchema.ejs'), ctx , { debug: false },function(err,str){
ejs.renderFile(path.join(__dirname, '../templates/md/topSchema.ejs'), ctx, { debug: false }, function(err, str){
if (err) {console.error(err);}
return writeFile(path.join(path.join(outDir), path.dirname(filename.substr(schemaPath.length))), path.basename(filename).slice(0, -5)+ '.md', str);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment