Controller.js

import Util from "./Util.js";
import fs from "fs";
import GenerateSchema from "generate-schema";

/**
 * A module to handle command-line interface commands.
 * @module Controller
 */
export default {

  /**
   * @desc Method ``parse()`` to handle the command parse.
   * @param {String} dir - Relative or absolute path to the exported JSON files generated by T.EX.
   * @param {Object} options - Options object passed by ``commander`` to this function.
   * See {@link https://www.npmjs.com/package/commander#options} for more details.
   * @method
   */
  parse(dir, options) {
    let path = Util.path(dir);
    if (!path) {
      console.log('Error: files not found at ' + path);
      process.exit(1);
    }

    if (!Util.isDir(path)) {
      console.log('Error: ' + path + ' is not a directory');
      process.exit(1);
    }

    let files = Util.batches(path);
    if (files.length === 0) {
      console.log('Error: ' + path + ' contains no JSON files');
      process.exit(1);
    }

    files
      .forEach((file, idx) => {
        let requests = JSON.parse(fs.readFileSync(path + file));
        let schema = GenerateSchema.json('Request', requests);
        fs.writeFileSync('output/json-schema-' + file, JSON.stringify(schema));
      });

  },
};