<commit_before>'use strict';

var fs = require('fs');
var _ = require('lodash');
var inflection = require('inflection');
var jsesc = require('jsesc');

var keywords = {
  string: {
    minLength: 'number',
    maxLength: 'number',
    pattern: 'string',
    format: 'string',
    default: 'string'
  },
  number: {
    multipleOf: 'number',
    minimum: 'number',
    maximum: 'number',
    exclusiveMinimum: 'boolean',
    exclusiveMaximum: 'boolean',
    default: 'number'
  },
  object: {
    minProperties: 'number',
    maxProperties: 'number'
  },
  array: {
    minItems: 'number',
    maxItems: 'number',
    uniqueItems: 'boolean'
  }
};

module.exports = function(typhen, options) {
  options = _.defaults(options, {
    baseUri: '',
    enumType: 'integer'
  });

  function getEnumValues(enumSymbol) {
    return enumSymbol.members.map(function(m) {
      return options.enumType === 'string' ? '"' + m.name + '"' : m.value;
    }).join('');
  }

  return typhen.createPlugin({
    pluginDirectory: __dirname,
    namespaceSeparator: '/',
    disallow: {
      any: true
    },
    handlebarsOptions: {
      data: options,
      partials: {
        object: fs.readFileSync('./templates/object.hbs', 'utf-8')
      },
      helpers: {
        escape: function(str) {
          return jsesc(str, { quotes: 'double' });
        },
        required: function(properties) {
          return properties.filter(function(p) {
            return !p.isOptional;
          }).map(function(p) {
            return '"' +  p.name + '"';
          }).join(', ');
        },
        keyword: function(type, tag) {
          var keywordsByType;
          if (type.name === 'number') {
            keywordsByType = keywords.number;
          } else if (type.name === 'string') {
            keywordsByType = keywords.string;
          } else if (type.isArray || type.isTuple) {
            keywordsByType = keywords.array;
          } else {
            keywordsByType = keywords.object;
          }
          if (_.isString(keywordsByType[tag.name])) {
            if (keywordsByType[tag.name] === 'string') {
              return '"' + tag.name + '": "' + tag.value + '",';
            } else {
              return '"' + tag.name + '": ' + tag[keywordsByType[tag.name]] + ',';
            }
          } else {
            typhen.logger.warn('Unsupported tag: ' + tag.name);
            return;
          }
        },
        typeProperty: function(type) {
          if (type.isPrimitiveType) {
            return '"type": "' + type.name + '"';
          } else if (type.isEnum) {
            return '"enum": [' + getEnumValues(type) + ']';
          } else {
            var pathSegments = _.flatten([type.ancestorModules.map(function(m) { return m.name; }), type.name], true);
            var typePath = pathSegments.map(function(s) { return inflection.underscore(s); }).join('/');
            return '"$ref": "' + options.baseUri + '/' + typePath + '.json"';
          }
        }
      }
    },

    generate: function(generator, types) {
      types.forEach(function(type) {
        switch (type.kind) {
          case typhen.SymbolKind.Interface:
          case typhen.SymbolKind.Class:
            generator.generate('templates/base.hbs', 'underscore:**/*.json', type);
            break;
        }
      });
    }
  });
};
<commit_msg>Fix reading a partial template file<commit_after>'use strict';

var fs = require('fs');
var _ = require('lodash');
var inflection = require('inflection');
var jsesc = require('jsesc');

var keywords = {
  string: {
    minLength: 'number',
    maxLength: 'number',
    pattern: 'string',
    format: 'string',
    default: 'string'
  },
  number: {
    multipleOf: 'number',
    minimum: 'number',
    maximum: 'number',
    exclusiveMinimum: 'boolean',
    exclusiveMaximum: 'boolean',
    default: 'number'
  },
  object: {
    minProperties: 'number',
    maxProperties: 'number'
  },
  array: {
    minItems: 'number',
    maxItems: 'number',
    uniqueItems: 'boolean'
  }
};

module.exports = function(typhen, options) {
  options = _.defaults(options, {
    baseUri: '',
    enumType: 'integer'
  });

  function getEnumValues(enumSymbol) {
    return enumSymbol.members.map(function(m) {
      return options.enumType === 'string' ? '"' + m.name + '"' : m.value;
    }).join('');
  }

  return typhen.createPlugin({
    pluginDirectory: __dirname,
    namespaceSeparator: '/',
    disallow: {
      any: true
    },
    handlebarsOptions: {
      data: options,
      partials: {
        object: fs.readFileSync(__dirname + '/templates/object.hbs', 'utf-8')
      },
      helpers: {
        escape: function(str) {
          return jsesc(str, { quotes: 'double' });
        },
        required: function(properties) {
          return properties.filter(function(p) {
            return !p.isOptional;
          }).map(function(p) {
            return '"' +  p.name + '"';
          }).join(', ');
        },
        keyword: function(type, tag) {
          var keywordsByType;
          if (type.name === 'number') {
            keywordsByType = keywords.number;
          } else if (type.name === 'string') {
            keywordsByType = keywords.string;
          } else if (type.isArray || type.isTuple) {
            keywordsByType = keywords.array;
          } else {
            keywordsByType = keywords.object;
          }
          if (_.isString(keywordsByType[tag.name])) {
            if (keywordsByType[tag.name] === 'string') {
              return '"' + tag.name + '": "' + tag.value + '",';
            } else {
              return '"' + tag.name + '": ' + tag[keywordsByType[tag.name]] + ',';
            }
          } else {
            typhen.logger.warn('Unsupported tag: ' + tag.name);
            return;
          }
        },
        typeProperty: function(type) {
          if (type.isPrimitiveType) {
            return '"type": "' + type.name + '"';
          } else if (type.isEnum) {
            return '"enum": [' + getEnumValues(type) + ']';
          } else {
            var pathSegments = _.flatten([type.ancestorModules.map(function(m) { return m.name; }), type.name], true);
            var typePath = pathSegments.map(function(s) { return inflection.underscore(s); }).join('/');
            return '"$ref": "' + options.baseUri + '/' + typePath + '.json"';
          }
        }
      }
    },

    generate: function(generator, types) {
      types.forEach(function(type) {
        switch (type.kind) {
          case typhen.SymbolKind.Interface:
          case typhen.SymbolKind.Class:
            generator.generate('templates/base.hbs', 'underscore:**/*.json', type);
            break;
        }
      });
    }
  });
};

{'fraction_non_alphanumeric': 0.161775659168777, 'fraction_numerical': 0.00029792939073439596, 'mean_word_length': 2.014818140996857, 'pattern_counts': {'":': 10, '<': 3, '<?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': '543550', 'n_tokens_mistral': 2019, 'n_tokens_neox': 2014, 'n_words': 384}