<commit_before>#! /usr/bin/env node

const _ = require('lodash');
const SwaggerParser = require('swagger-parser');
const jsf = require('./lib/jsfConfig.js');

/**
 * allPropsRequired - Creates a new definitions object. Each definintion is updated
 *                      to require all of its properties. This makes sure that JSON-Schema-Faker
 *                      populates data for each property of a definition
 *
 * @param definitions {Object}  - the definitions object of a swagger doc
 * @returns           {Object}  - a new definitions object
 */
function allPropsRequired(definitions) {
  const definitionPairs = _.toPairs(definitions);
  const apiProperties = definitionPairs
    .map(definitionPair => Object.keys(definitionPair[1].properties));


  const newDefinitionsCollection = definitionPairs
    // adds or overwrites the required property of each definition object
    .map((definitionPair, index) => {
      const definitionKey = definitionPair[0];
      const definitionValue = Object.assign({}, definitionPair[1]);

      definitionValue.required = apiProperties[index];

      return [definitionKey, definitionValue];
    })
    .reduce((definitionsObject, definitionPair) => {
      const key = definitionPair[0];
      const value = definitionPair[1];
      const passedDefinitions = definitionsObject;
      passedDefinitions[key] = value;

      return passedDefinitions;
    }, {});

  return newDefinitionsCollection;
}

SwaggerParser.parse('./samples/PetStore.yaml')
  .then(api => {
    const swaggerDoc = api;

    swaggerDoc.definitions = allPropsRequired(swaggerDoc.definitions);
    return SwaggerParser.dereference(swaggerDoc);
  })
  .then(api => {
    _.forEach(api.definitions, definition => {
      let temp;
      try {
        temp = jsf(definition);
        console.log('jsf', temp);
      } catch (err) {
        console.log(err);
      }
    });
  });<commit_msg>cli can now take in arguments from terminal and output generated data to a separate file<commit_after>#! /usr/bin/env node

const fs = require('fs');
const readline = require('readline');
const { forEach } = require('lodash');
const SwaggerParser = require('swagger-parser');
const jsf = require('./lib/jsfConfig.js');
const { requireAllProperties } = require('./lib/utils/helpers.js');

const passedUserArguments = process.argv.slice(2);
const swaggerFilePath = passedUserArguments[0];
const outputFilePath = passedUserArguments[1];

// fs.stat(outputFilePath, (err, stat) => {
//   if (err) {
//     return console.log(err);
//   }
//
//   return console.log(stat);
// });

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});


if (!swaggerFilePath) {
  console.log('command: sdg <path-to-file-input> <path-to-file-output>');
} else {
  SwaggerParser.parse(swaggerFilePath)
    .then(parsedApi => {
      const swaggerDoc = parsedApi;

      swaggerDoc.definitions = requireAllProperties(swaggerDoc.definitions);
      return SwaggerParser.dereference(swaggerDoc);
    })
    .catch(err => console.log(err.message))
    .then(dereferencedApi => {
      const generatedSwaggerData = {};

      forEach(dereferencedApi.definitions,
        (definition, name) => {
          try {
            generatedSwaggerData[name] = jsf(definition);
          } catch (err) {
            console.log(err);
          }
        });

      if (outputFilePath) {
        rl.question(`content in ${outputFilePath} will be overwritten. continue? (y or n)?`,
          answer => {
            if (answer === 'y' || answer === 'Y') {
              fs.writeSync(fs.openSync(outputFilePath, 'w'),
                  JSON.stringify(generatedSwaggerData, null, '\t'));
            } else {
              rl.write('...Aborting\n');
            }

            rl.close();
            process.stdin.destroy();
          });
      }
    });
}

{'fraction_non_alphanumeric': 0.13146997929606624, 'fraction_numerical': 0.002070393374741201, 'mean_word_length': 2.868868868868869, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2024822', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1077, 'n_words': 250}