Created
April 9, 2018 18:13
-
-
Save kyranjamie/c365c2908c5a26eaec4930fec3140504 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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