var Templates = module.exports = {};

var FS = require('fs');
var Path = require('path');

var TEMPLATE_DIR = __dirname + '/../code_templates';

var populateTemplates = function(type) {
  Templates[type] = {};
  var tmpls = FS.readdirSync(Path.join(TEMPLATE_DIR, type));
  tmpls.forEach(function(tmpl) {
    var ext = Path.extname(tmpl);
    var name = Path.basename(tmpl, ext);
    var filename = Path.join(TEMPLATE_DIR, type, tmpl);
    if (type === 'views' || type === 'actions' || type === 'static') {
      var lang = name;
      var tmplsInner = FS.readdirSync(filename);
      tmplsInner.forEach(function(tmplInner) {
        ext = Path.extname(tmplInner);
        name = Path.basename(tmplInner, ext);
        var filenameInner = Path.join(TEMPLATE_DIR, type, lang, tmplInner);
        if (type === 'static') {
          Templates[type][lang] = Templates[type][lang] || {};
          Templates[type][lang][name] = FS.readFileSync(filenameInner, 'utf8');
        } else {
          Templates[type][name] = Templates[type][name] || {};
          Templates[type][name][lang] = FS.readFileSync(filenameInner, 'utf8');
        }
      })
    } else {
      Templates[type][name] = FS.readFileSync(filename, 'utf8');
    }
  });
}

var types = ['actions', 'views', 'generic_actions', 'setups', 'static'];
types.forEach(populateTemplates);

@@ -1,12 +1,33 @@
+module.exports = %7B%7D;%0A
 var Template
@@ -40,24 +40,34 @@
 dule.exports
+.templates
  = %7B%7D;%0A%0Avar 
@@ -172,49 +172,202 @@
 s';%0A
-%0A
 var 
-populateTemplates = function(type) %7B
+TYPES = %5B'actions', 'views', 'generic_actions', 'setups', 'static'%5D;%0A%0Amodule.exports.populateTemplates = function(type) %7B%0A  if (!type) return TYPES.forEach(module.exports.populateTemplates);
 %0A  T
@@ -1414,96 +1414,23 @@
 ;%0A%7D%0A
-%0Avar types = %5B'actions', 'views', 'generic_actions', 'setups', 'static'%5D;%0Atypes.forEach(
+module.exports.
 popu
@@ -1422,31 +1422,32 @@
 le.exports.populateTemplates
+(
 );%0A

add ability to refresh templates