Browse Source

Add `auspice convert` to allow cli v1 -> v2 JSON conversion

master
james hadfield 3 years ago
parent
commit
eef0f253d2
  1. 4
      auspice.js
  2. 34
      cli/convert.js
  3. 4
      cli/server/convertJsonSchemas.js
  4. 4
      cli/server/getDatasetHelpers.js

4
auspice.js

@ -5,6 +5,7 @@ const version = require('./src/version').version;
const view = require("./cli/view");
const build = require("./cli/build");
const develop = require("./cli/develop");
const convert = require("./cli/convert");
const parser = new argparse.ArgumentParser({
version: version,
@ -22,6 +23,7 @@ const subparsers = parser.addSubparsers({title: 'Auspice commands', dest: "subco
view.addParser(subparsers);
build.addParser(subparsers);
develop.addParser(subparsers);
convert.addParser(subparsers);
const args = parser.parseArgs();
@ -33,6 +35,8 @@ if (args.subcommand === "build") {
view.run(args);
} else if (args.subcommand === "develop") {
develop.run(args);
} else if (args.subcommand === "convert") {
convert.run(args);
}
// console.dir(args);

34
cli/convert.js

@ -0,0 +1,34 @@
/* eslint no-console: off */
const fs = require("fs");
const convertFromV1 = require("./server/convertJsonSchemas").convertFromV1;
const utils = require("./utils");
const addParser = (parser) => {
const description = `Convert dataset files to the most up-to-date schema`;
const subparser = parser.addParser('convert', {addHelp: true, description});
subparser.addArgument('--meta-json', {action: "store", help: "v1 schema metadata json"});
subparser.addArgument('--tree-json', {action: "store", help: "v1 schema tree json"});
subparser.addArgument('--output', {action: "store", help: "File to write output to"});
};
/**
* this utility function will increase in scope over time
* but currently it only converts v1 meta + tree jsons -> v2
*/
const run = (args) => {
if (!(args.meta_json && args.tree_json)) {
utils.error("Meta + Tree (v1) JSONs are required");
}
const meta = JSON.parse(fs.readFileSync(args.meta_json, 'utf8'));
const tree = JSON.parse(fs.readFileSync(args.tree_json, 'utf8'));
const v2 = convertFromV1({tree, meta});
fs.writeFileSync(args.output, JSON.stringify(v2, null, 2));
};
module.exports = {
addParser,
run
};

4
cli/server/convertJsonSchemas.js

@ -266,7 +266,7 @@ const storeTreeAsV2 = (v2, tree) => {
};
const convert = ({tree, meta, treeName}) => {
const convertFromV1 = ({tree, meta, treeName}) => {
const v2 = {};
setColorings(v2, meta);
setMiscMetaProperties(v2, meta);
@ -281,5 +281,5 @@ const convert = ({tree, meta, treeName}) => {
module.exports = {
convert
convertFromV1
};

4
cli/server/getDatasetHelpers.js

@ -12,7 +12,7 @@ const utils = require("../utils");
const queryString = require("query-string");
const getAvailable = require("./getAvailable");
const path = require("path");
const convertJsons = require("./convertJsonSchemas").convert;
const convertFromV1 = require("./convertJsonSchemas").convertFromV1;
const handleError = (res, clientMsg, serverMsg="") => {
@ -111,7 +111,7 @@ const sendJson = async (res, info) => {
const tree = await utils.readFilePromise(info.address.tree);
/* v1 JSONs don't define a tree name, so we try to guess it here */
const mainTreeName = guessTreeName(info.parts);
const v2Json = convertJsons({tree, meta, treeName: mainTreeName});
const v2Json = convertFromV1({tree, meta, treeName: mainTreeName});
return res.json(v2Json)
}
}

Loading…
Cancel
Save