Skip to content

Instantly share code, notes, and snippets.

@jasiek
Created December 6, 2016 09:21
Show Gist options
  • Save jasiek/0cd0b7ea8b8470569609b5cf321a62f5 to your computer and use it in GitHub Desktop.
Save jasiek/0cd0b7ea8b8470569609b5cf321a62f5 to your computer and use it in GitHub Desktop.
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