<commit_before>
var fs = require('fs'),
    us = require('underscore'),
  yaml = require('js-yaml'),
  path = require('path');

if (!process.argv[2]) {
  console.log('requires argument');
  process.exit();
}

var models_location = process.argv[2],
    out_path = path.basename(models_location, '.models.yml') + '-models.ts',
    models = yaml.safeLoad(fs.readFileSync(models_location, encoding='utf-8'));

function mapHash(hash, mapper) {
  return Object.keys(hash).map(function(key){
    var value = hash[key];
    return mapper(key, value);
  });
}

function checkAndRemove(arr, val) {
  var index = arr.indexOf(val);
  if (index < 0) return false;
  arr.splice(index, 1);
  return true;
}

var normalized_models = mapHash(models, function(model_name, members){
  return {
    name: model_name,
    members: mapHash(members, function(member_name, member_def){
      var member_opts = member_def.split(' ');
      return {
        name: member_name,
        readonly: checkAndRemove(member_opts, 'readonly'),
        optional: checkAndRemove(member_opts, 'optional'),
        type: us.last(member_opts)
      };
    })
  };
});

var template = fs.readFileSync(__dirname + '/template.ts', encoding='utf-8');
var output = us.template(template, {models: normalized_models});
fs.writeFileSync(out_path, output);

/*
TODO:
[ ] grunt plugin
[ ] handle paths more reasonably
[ ] add to npm
[ ] throw on additional fields
*/
<commit_msg>reorganized code to allow better future handling of multiple files<commit_after>
var fs = require('fs'),
    us = require('underscore'),
  yaml = require('js-yaml'),
  path = require('path');

function mapHash(hash, mapper) {
  return Object.keys(hash).map(function(key){
    var value = hash[key];
    return mapper(key, value);
  });
}

function checkAndRemove(arr, val) {
  var index = arr.indexOf(val);
  if (index < 0) return false;
  arr.splice(index, 1);
  return true;
}

function generate_typescript(src, dest) {
  var models = yaml.safeLoad(fs.readFileSync(src, encoding='utf-8'));

  var normalized_models = mapHash(models, function(model_name, members){
    return {
      name: model_name,
      members: mapHash(members, function(member_name, member_def){
        var member_opts = member_def.split(' ');
        return {
          name: member_name,
          readonly: checkAndRemove(member_opts, 'readonly'),
          optional: checkAndRemove(member_opts, 'optional'),
          type: us.last(member_opts)
        };
      })
    };
  });

  var template = fs.readFileSync(__dirname + '/template.ts', encoding='utf-8');
  var output = us.template(template, {models: normalized_models});
  fs.writeFileSync(dest, output);
}

function default_dest(src) {
  var basename = path.basename(src, '.models.yml');
  var dir = path.dirname(src);
  return path.join(dir, basename + '-models.ts');
}

if (!process.argv[2]) {
  console.log('requires argument');
} else {
  var src = process.argv[2];
  generate_typescript(src, default_dest(src));
}



/*
TODO:
[ ] grunt plugin
[ ] handle paths more reasonably
[ ] add to npm
[ ] throw on additional fields
*/

{'fraction_non_alphanumeric': 0.14894991922455572, 'fraction_numerical': 0.0038772213247172858, 'mean_word_length': 3.852664576802508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5673029', 'n_tokens_mistral': 1010, 'n_tokens_neox': 986, 'n_words': 230}