Skip to content

Commit

Permalink
Add auspice convert to allow cli v1 -> v2 JSON conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshadfield committed Jun 17, 2019
1 parent 010893c commit eef0f25
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions auspice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

Expand All @@ -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 changes: 34 additions & 0 deletions cli/convert.js
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 2 deletions cli/server/convertJsonSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -281,5 +281,5 @@ const convert = ({tree, meta, treeName}) => {


module.exports = {
convert
convertFromV1
};
4 changes: 2 additions & 2 deletions cli/server/getDatasetHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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="") => {
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit eef0f25

Please sign in to comment.