import fs from 'fs';
var Errorable = require('./errorable');

function Generator(options, loc, toUpper) {
  var json = {};
  (typeof toUpper == 'boolean' && toUpper) ? toUpper = true : toUpper = false;
  this.errors = {};
  this.json = json;
  this.toUpper = toUpper;
  this._generate(json, options, loc);
}
Generator.prototype.capitalize = function(str,toUpper) {
  if(!toUpper) return str[0].toUpperCase() + str.substring(1).toLowerCase();
  return str.toUpperCase();
};

Generator.prototype.getName = function(name, k, toUpper) {
  name = name || '';
  if(!toUpper) return name+ this.capitalize(k);
  return (name&&name!=='') ? name+"_"+ this.capitalize(k,toUpper) : name+this.capitalize(k,toUpper);
};

Generator.prototype.save = function(file) {
  fs.writeFileSync(file, JSON.stringify(this.json, null, 2));
};

Generator.prototype._generate = function(savor, data, locale, name) {
  if (name && data && data.messages) {
    if (!savor[name]) {
      savor[name] = {};
    }
    savor[name].messages = data.messages;
    if (data.code !== undefined) {
      savor[name].code = data.code;
    }
    this.errors[name] = new Errorable({
      name: name,
      messages: data.messages,
      code: data.code,
      locale: locale
    });
  }
  if (typeof data !== 'object') {
    throw Error('Parse JSON Error, Please check you Json Format at : ' + JSON.stringify(data));
  }
  for (var k in data) {
    if (data[k].alias) {
      this._generate(savor, data[data[k].alias], locale, this.getName(name, k,this.toUpper));
      continue;
    }
    if (k === 'messages' || k === 'code') {
      continue;
    }
    this._generate(savor, data[k], locale, this.getName(name, k,this.toUpper));
  }
};

module.exports = Generator;

@@ -118,16 +118,18 @@
 = %7B%7D;%0A  
+if
 (typeof 
@@ -138,16 +138,17 @@
 Upper ==
+=
  'boolea
@@ -162,17 +162,21 @@
 oUpper) 
-?
+%7B%0A   
  toUpper
@@ -182,18 +182,31 @@
 r = true
+;%0A  %7Delse %7B%0A  
  
-:
  toUpper
@@ -215,16 +215,20 @@
  false;%0A
+  %7D%0A
   this.e
@@ -371,16 +371,17 @@
 ion(str,
+ 
 toUpper)
@@ -389,32 +389,38 @@
 %7B%0A  if(!toUpper)
+ %7B%0A   
  return str%5B0%5D.t
@@ -466,16 +466,20 @@
 Case();%0A
+  %7D%0A
   return
@@ -596,16 +596,22 @@
 toUpper)
+ %7B%0A   
  return 
@@ -614,16 +614,17 @@
 urn name
+ 
 + this.c
@@ -643,43 +643,70 @@
 ;%0A  
-return 
+%7Delse %7B%0A    if
 (name
+ 
 &&
+ 
 name
+ 
 !==
+ 
 '') 
-? name+%22_%22
+%7B%0A      return name + '_' 
 + th
@@ -721,24 +721,25 @@
 alize(k,
+ 
 toUpper)
  : name+
@@ -734,16 +734,42 @@
 per)
- :
+;%0A    %7Delse %7B%0A      return
  name
-+
+ + 
 this
@@ -782,16 +782,17 @@
 alize(k,
+ 
 toUpper)
@@ -793,16 +793,26 @@
 Upper);%0A
+    %7D%0A  %7D%0A
 %7D;%0A%0AGene
@@ -1597,32 +1597,33 @@
 getName(name, k,
+ 
 this.toUpper));%0A
@@ -1774,16 +1774,17 @@
 name, k,
+ 
 this.toU

modify generator.js with gulp