All files / src/commands JSONCommand.ts

90.63% Statements 29/32
75% Branches 21/28
50% Functions 1/2
93.55% Lines 29/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 7224x       24x 24x 24x                                         24x   6x     6x   6x 6x   6x     6x       6x 6x   6x 6x 4x     6x 6x 6x 6x 6x 6x   6x 6x   6x 4x   6x 2x     6x    
import { argdown, IFileRequest, ISaveAsRequest, ILogParserErrorsRequest } from "@argdown/node";
import { Arguments } from "yargs";
import { IJSONRequest } from "@argdown/core";
 
export const command = "json [inputGlob] [outputDir]";
export const desc = "export Argdown input as JSON files";
export const builder = {
  logParserErrors: {
    alias: "e",
    describe: "Log parser errors to console",
    type: "boolean",
    default: true
  },
  spaces: {
    alias: "s",
    describe: "Spaces used for indentation",
    type: "number"
  },
  removeMap: {
    describe: "Remove map data",
    type: "boolean"
  },
  removeEmbeddedRelations: {
    describe: "Remove relations embedded in statement and relation objects",
    type: "boolean"
  }
};
export const handler = async (argv: Arguments) => {
  let config = <IJSONRequest & IFileRequest & ISaveAsRequest & ILogParserErrorsRequest>(
    await argdown.loadConfig(argv.config)
  );
 
  config.json = config.json || {};
 
  Eif (argv.spaces !== null) {
    config.json.spaces = argv.spaces;
  }
  Iif (argv.removeEmbeddedRelations) {
    config.json.removeEmbeddedRelations = true;
  }
  Iif (argv.removeMap) {
    config.json.exportMap = false;
  }
 
  Eif (argv.inputGlob) {
    config.inputPath = argv.inputGlob;
  }
  config.saveAs = config.saveAs || {};
  if (argv.outputDir) {
    config.saveAs.outputDir = argv.outputDir;
  }
 
  config.logLevel = argv.verbose ? "verbose" : config.logLevel;
  config.watch = argv.watch || config.watch;
  config.process = ["load-file", "parse-input"];
  config.logParserErrors = argv.logParserErrors || config.logParserErrors;
  Eif (config.logParserErrors) {
    config.process.push("log-parser-errors");
  }
  config.process.push("build-model");
  config.process.push("export-json");
 
  if (!argv.stdout || argv.outputDir) {
    config.process.push("save-as-json");
  }
  if (argv.stdout) {
    config.process.push("stdout-json");
  }
 
  await argdown.load(config).catch(e => console.log(e));
};