Created
December 6, 2016 09:21
-
-
Save jasiek/0cd0b7ea8b8470569609b5cf321a62f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var refParser = require('json-schema-ref-parser'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const jsonPath = process.argv[2] || './schemas'; | |
const distPath = process.argv[3] || './dist'; | |
const prefix = process.argv[4] || ''; | |
const beautify = require('js-beautify').js_beautify; | |
fs.readdir(jsonPath, (err, filenames) => { | |
if (err) { | |
console.log(err); | |
throw err; | |
} | |
filenames.forEach(processFile); | |
}); | |
function processFile(filename) { | |
if (filename.indexOf('.json') === -1) { | |
return; | |
} | |
refParser | |
.dereference(jsonPath + '/' + filename) | |
.then(onDeference) | |
.catch(err => { | |
console.log(err); | |
throw err; | |
}); | |
function onDeference(schema) { | |
const flattenedSchema = beautify(JSON.stringify(schema), { indent_size: 2 }); | |
const des = distPath + '/' + prefix + filename; | |
fs.writeFile(des, flattenedSchema, (err) => { | |
if (err) { | |
console.log(err); | |
throw err; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment