Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kyranjamie/c365c2908c5a26eaec4930fec3140504 to your computer and use it in GitHub Desktop.
Save kyranjamie/c365c2908c5a26eaec4930fec3140504 to your computer and use it in GitHub Desktop.
const $RefParser = require('json-schema-ref-parser');
const fs = require('fs-extra');
const chokidar = require('chokidar');
const glob = require('glob');
const _ = require('lodash');
const path = require('path');
const args = require('yargs').argv;
const chalk = require('chalk');
const schemas = 'src/**/*.schema.json';
//
// Allow for watching of files
if (args.watch) {
const watcher = chokidar.watch(schemas);
watcher
.on('add', path => compileSchema(path))
.on('change', path => compileSchema(path));
}
//
// Find `.schema` files
glob(schemas, function (err, files) {
files.forEach(compileSchema);
});
function compileSchema (file) {
$RefParser.dereference(file, (err, schema) => {
if (err) {
console.error(`${chalk.red(err)}`);
process.exit();
}
writeFiles(file, schema);
});
}
function writeFiles (path, schema) {
fs.outputFileSync(path.replace('src', 'dist'), JSON.stringify(schema, null, 2));
console.log(`[${path}] ${chalk.green('COMPILED')}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment