Skip to content
Snippets Groups Projects
Commit 5b80cb97 authored by filmaj's avatar filmaj
Browse files

autofixing eslint errors

parent a077e71f
Branches
Tags
No related merge requests found
...@@ -20,29 +20,29 @@ var readSchemaFile = require('./lib/readSchemaFile'); ...@@ -20,29 +20,29 @@ var readSchemaFile = require('./lib/readSchemaFile');
// parse/process command line arguments // parse/process command line arguments
var argv = require('optimist') var argv = require('optimist')
.usage('Generate Markdown documentation from JSON Schema.\n\nUsage: $0') .usage('Generate Markdown documentation from JSON Schema.\n\nUsage: $0')
.demand('d') .demand('d')
.alias('d', 'input') .alias('d', 'input')
// TODO: is baseURL still a valid parameter? // TODO: is baseURL still a valid parameter?
.describe('d', 'path to directory containing all JSON Schemas or a single JSON Schema file. This will be considered as the baseURL. Note that only files ending in .schema.json will be processed.') .describe('d', 'path to directory containing all JSON Schemas or a single JSON Schema file. This will be considered as the baseURL. Note that only files ending in .schema.json will be processed.')
.alias('o', 'out') .alias('o', 'out')
.describe('o', 'path to output directory') .describe('o', 'path to output directory')
.default('o', path.resolve(path.join('.', 'out'))) .default('o', path.resolve(path.join('.', 'out')))
.alias('m', 'meta') .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') .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') .alias('s','metaSchema')
.describe('s', 'Custom meta schema path to validate schemas') .describe('s', 'Custom meta schema path to validate schemas')
.alias('x', 'schema-out') .alias('x', 'schema-out')
.describe('x', 'output JSON Schema files including description and validated examples in the _new folder at output directory') .describe('x', 'output JSON Schema files including description and validated examples in the _new folder at output directory')
.check(function (args) { .check(function (args) {
if (!fs.existsSync(args.input)) { if (!fs.existsSync(args.input)) {
throw 'Input file "' + args.input + '" does not exist!'; throw 'Input file "' + args.input + '" does not exist!';
} }
if (args.s && !fs.existsSync(args.s)) { if (args.s && !fs.existsSync(args.s)) {
throw 'Meta schema file "' + args.s + '" does not exist!'; throw 'Meta schema file "' + args.s + '" does not exist!';
} }
}) })
.argv; .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')); ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
...@@ -52,12 +52,12 @@ var schemaPath = path.resolve(argv.d); ...@@ -52,12 +52,12 @@ var schemaPath = path.resolve(argv.d);
var outDir = path.resolve(argv.o); var outDir = path.resolve(argv.o);
var target = fs.statSync(schemaPath); var target = fs.statSync(schemaPath);
if(argv.s){ if (argv.s){
ajv.addMetaSchema(require(path.resolve(argv.s))); ajv.addMetaSchema(require(path.resolve(argv.s)));
} }
if (argv.m) { if (argv.m) {
if(_.isArray(argv.m)){ if (_.isArray(argv.m)){
_.each(argv.m,function(item){ _.each(argv.m,function(item){
var meta=item.split('='); var meta=item.split('=');
if (meta.length === 2) { if (meta.length === 2) {
...@@ -77,17 +77,38 @@ if (target.isDirectory()) { ...@@ -77,17 +77,38 @@ if (target.isDirectory()) {
// the ajv json validator will be passed into the main module to help with processing // the ajv json validator will be passed into the main module to help with processing
var files=[]; var files=[];
readdirp({ root: schemaPath, fileFilter: '*.schema.json' }) readdirp({ root: schemaPath, fileFilter: '*.schema.json' })
.on('data',(entry) => { .on('data',(entry) => {
files.push(entry.fullPath); files.push(entry.fullPath);
ajv.addSchema(require(entry.fullPath), entry.fullPath); ajv.addSchema(require(entry.fullPath), entry.fullPath);
}) })
.on('end',() => { .on('end',() => {
Schema.setAjv(ajv); Schema.setAjv(ajv);
Schema.setSchemaPathMap(schemaPathMap); Schema.setSchemaPathMap(schemaPathMap);
return Promise.reduce(files, readSchemaFile, schemaPathMap) return Promise.reduce(files, readSchemaFile, schemaPathMap)
.then((schemaMap)=>{ .then((schemaMap)=>{
logger.info('finished reading all *.schema.json files in %s, beginning processing....', schemaPath); logger.info('finished reading all *.schema.json files in %s, beginning processing....', schemaPath);
return Schema.load(schemaMap, schemaPath, outDir, metaElements); return Schema.load(schemaMap, schemaPath, outDir, metaElements);
})
.then(() => {
logger.info('Processing complete.');
})
.catch((err) => {
logger.error(err);
process.exit(1);
});
})
.on('error',(err)=>{
logger.error(err);
process.exit(1);
});
} else {
readSchemaFile(schemaPathMap, schemaPath)
.then((schemaMap) => {
ajv.addSchema(require(schemaPath), schemaPath);
Schema.setAjv(ajv);
Schema.setSchemaPathMap(schemaPathMap);
logger.info('finished reading %s, beginning processing....', schemaPath);
return Schema.load(schemaMap , schemaPath, outDir, metaElements);
}) })
.then(() => { .then(() => {
logger.info('Processing complete.'); logger.info('Processing complete.');
...@@ -96,25 +117,4 @@ if (target.isDirectory()) { ...@@ -96,25 +117,4 @@ if (target.isDirectory()) {
logger.error(err); logger.error(err);
process.exit(1); process.exit(1);
}); });
})
.on('error',(err)=>{
logger.error(err);
process.exit(1);
});
} else {
readSchemaFile(schemaPathMap, schemaPath)
.then((schemaMap) => {
ajv.addSchema(require(schemaPath), schemaPath);
Schema.setAjv(ajv);
Schema.setSchemaPathMap(schemaPathMap);
logger.info('finished reading %s, beginning processing....', schemaPath);
return Schema.load(schemaMap , schemaPath, outDir, metaElements);
})
.then(() => {
logger.info('Processing complete.');
})
.catch((err) => {
logger.error(err);
process.exit(1);
});
} }
...@@ -12,23 +12,23 @@ var fs = Promise.promisifyAll(require('fs')); ...@@ -12,23 +12,23 @@ var fs = Promise.promisifyAll(require('fs'));
// Returns the schema path map object. // Returns the schema path map object.
module.exports = function readSchemaFile(schemaPathMap, fullPath) { module.exports = function readSchemaFile(schemaPathMap, fullPath) {
if (!schemaPathMap) { if (!schemaPathMap) {
schemaPathMap = {}; schemaPathMap = {};
} }
return fs.readFileAsync(fullPath) return fs.readFileAsync(fullPath)
.then((data)=>{ .then((data)=>{
let schema = JSON.parse(data); let schema = JSON.parse(data);
let obj = {}; let obj = {};
obj.filePath = fullPath; obj.filePath = fullPath;
obj.jsonSchema = schema; obj.jsonSchema = schema;
if(schema["$id"] && schema["$id"].length > 0) { if (schema['$id'] && schema['$id'].length > 0) {
if(! schemaPathMap[schema["$id"]]) { if (!schemaPathMap[schema['$id']]) {
schemaPathMap[schema["$id"]] = obj; schemaPathMap[schema['$id']] = obj;
} }
// TODO err // TODO err
//TODO check Missing Specific properties to throw warning // function for warning //TODO check Missing Specific properties to throw warning // function for warning
} else { } else {
schemaPathMap[fullPath] = obj; schemaPathMap[fullPath] = obj;
} }
return schemaPathMap; return schemaPathMap;
}); });
}; };
This diff is collapsed.
...@@ -14,27 +14,26 @@ var ejs = require('ejs'); ...@@ -14,27 +14,26 @@ var ejs = require('ejs');
var logger = require('winston'); var logger = require('winston');
var mkdirp = Promise.promisify(require('mkdirp')); var mkdirp = Promise.promisify(require('mkdirp'));
var readdirp = require('readdirp'); var readdirp = require('readdirp');
var validUrl = require("valid-url"); var validUrl = require('valid-url');
var url = require("url"); var url = require('url');
// var Writer = function(schemaPath,outDir){ // var Writer = function(schemaPath,outDir){
// this._outDir = outDir; // this._outDir = outDir;
// this._schemaPath = schemaPath; // this._schemaPath = schemaPath;
// }; // };
var Writer = {} var Writer = {};
var writeFile = function(outputDir, fileName, data) { var writeFile = function(outputDir, fileName, data) {
if(!fs.existsSync(outputDir)){ if (!fs.existsSync(outputDir)){
return mkdirp(outputDir).then((err)=>{ return mkdirp(outputDir).then((err)=>{
return fs.writeFileAsync(path.join(outputDir, fileName), data); return fs.writeFileAsync(path.join(outputDir, fileName), data);
}) });
} } else {
else {
return fs.writeFileAsync(path.join(outputDir, fileName), data); return fs.writeFileAsync(path.join(outputDir, fileName), data);
} }
} };
Writer.generateMarkdown = function(filename, schema,schemaPath,outDir,dependencyMap) { Writer.generateMarkdown = function(filename, schema,schemaPath,outDir,dependencyMap) {
var ctx = { var ctx = {
schema: schema, schema: schema,
_: _, _: _,
...@@ -43,15 +42,14 @@ Writer.generateMarkdown = function(filename, schema,schemaPath,outDir,dependency ...@@ -43,15 +42,14 @@ Writer.generateMarkdown = function(filename, schema,schemaPath,outDir,dependency
}; };
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) if (err) {console.error(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); return writeFile(path.join(path.join(outDir), path.dirname(filename.substr(schemaPath.length))), path.basename(filename).slice(0, -5)+ '.md', str);
}) });
} };
Writer.generateNewSchemaFiles = function(filename, schema,schemaPath,outDir) { Writer.generateNewSchemaFiles = function(filename, schema,schemaPath,outDir) {
return writeFile(path.join(path.join(outDir,"_newSchema"), path.dirname(filename.substr(schemaPath.length))), path.basename(filename), JSON.stringify(schema, null, 4)); return writeFile(path.join(path.join(outDir,'_newSchema'), path.dirname(filename.substr(schemaPath.length))), path.basename(filename), JSON.stringify(schema, null, 4));
} };
module.exports = Writer; module.exports = Writer;
...@@ -17,13 +17,13 @@ describe('readSchemaFile module', () => { ...@@ -17,13 +17,13 @@ describe('readSchemaFile module', () => {
describe('reading schema files without an $id key', () => { describe('reading schema files without an $id key', () => {
it('should return a schema path map with path to the file as a key, and object value with path and json schema', (done) => { it('should return a schema path map with path to the file as a key, and object value with path and json schema', (done) => {
readSchemaFile({}, fakePath) readSchemaFile({}, fakePath)
.then((map) => { .then((map) => {
expect(map[fakePath]).toBeDefined(); expect(map[fakePath]).toBeDefined();
expect(map[fakePath].filePath).toEqual(fakePath); expect(map[fakePath].filePath).toEqual(fakePath);
expect(map[fakePath].jsonSchema).toEqual({schema:'yes'}); expect(map[fakePath].jsonSchema).toEqual({ schema:'yes' });
}) })
.catch(fail) .catch(fail)
.done(done); .done(done);
}); });
}); });
describe('reading schema files with an $id key', () => { describe('reading schema files with an $id key', () => {
...@@ -32,23 +32,23 @@ describe('readSchemaFile module', () => { ...@@ -32,23 +32,23 @@ describe('readSchemaFile module', () => {
}); });
it('should return a schema path map with $id value as a key, and object value with path and json schema', (done) => { it('should return a schema path map with $id value as a key, and object value with path and json schema', (done) => {
readSchemaFile({}, fakePath) readSchemaFile({}, fakePath)
.then((map) => { .then((map) => {
expect(map['allyourbase']).toBeDefined(); expect(map['allyourbase']).toBeDefined();
expect(map['allyourbase'].filePath).toEqual(fakePath); expect(map['allyourbase'].filePath).toEqual(fakePath);
expect(map['allyourbase'].jsonSchema).toEqual({$id:'allyourbase'}); expect(map['allyourbase'].jsonSchema).toEqual({ $id:'allyourbase' });
}) })
.catch(fail) .catch(fail)
.done(done); .done(done);
}); });
it('should not overwrite the value for an existing $id key in the schema path map', (done) => { it('should not overwrite the value for an existing $id key in the schema path map', (done) => {
readSchemaFile({allyourbase:{}}, fakePath) readSchemaFile({ allyourbase:{} }, fakePath)
.then((map) => { .then((map) => {
expect(map['allyourbase']).toBeDefined(); expect(map['allyourbase']).toBeDefined();
expect(map['allyourbase'].filePath).not.toBeDefined(); expect(map['allyourbase'].filePath).not.toBeDefined();
expect(map['allyourbase'].jsonSchema).not.toBeDefined(); expect(map['allyourbase'].jsonSchema).not.toBeDefined();
}) })
.catch(fail) .catch(fail)
.done(done); .done(done);
}); });
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment