#!/usr/bin/env node
// shex-to-json http://tracker.example/schemas/Issue.shex
var ShExUtil = require("../lib/ShExUtil"); // translate back and forth to AS
var ShExLoader = require("../lib/ShExLoader"); // for verbose output
// Generate command line interface
var CommandLineOptions = [
{ name: "help", alias: "h", type: Boolean },
{ name: "shex", alias: "x", type: String, multiple: true, defaultValue: [], defaultOption: true },
{ name: "json", alias: "j", type: String, multiple: true, defaultValue: [] },
{ name: "abbreviate", alias: "a", type: Boolean, multiple: false, defaultValue: false }
];
var CLI = require("command-line-args")(CommandLineOptions);
function abort (msg) {
console.error(msg);
console.error(require('command-line-usage')([
{
header: "shex-to-json",
content: "load some number of schema files from web or filesystem and display as JSON (ShExJ), for example:\n shex-to-json http://tracker.example/schemas/Issue.shex" },
{
header: 'Options',
optionList: CommandLineOptions
},
{
content: "Project home: " + require('chalk').underline("https://github.com/shexSpec/shex.js")
}
]));
process.exit(1);
}
// Extract user commands
var cmds = CLI;
if (cmds.help)
abort("");
if (cmds.shex.length === 0 && cmds.json.length === 0) abort("no shex specified");
ShExLoader.load(cmds.shex, cmds.json, [], []).then(function (loaded) {
console.log('%s', JSON.stringify(ShExUtil.AStoShExJ(loaded.schema, cmds.abbreviate), null, ' '));
}).catch(function (e) {
console.error("aborting:", e.stack || e);
process.exit(1);
})