Revision f4950f549113fd2a0cf6ac4c090fe98297f2ced9 authored by Eric Prud'hommeaux on 19 December 2018, 17:03:36 UTC, committed by Eric Prud'hommeaux on 19 December 2018, 17:05:31 UTC
1 parent 575ae8d
Raw File
shex-to-json
#!/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);
})
back to top