2013-04-25 2 views
4

Grunt를 사용하여 json 파일을 특정 구조로 유지하는 가장 좋은 방법을 찾고 있습니다.Grunt를 사용하여 JSON 파일을 연결하는 적절한 방법

파일은 다음과 같은 구조의 폴더에 배치됩니다

 
App 
├── locales 
│   ├── en 
│   │   └── translation.json 
│   ├── es 
│   │   └── translation.json 
│   └── fr 
│    └── translation.json 
└── widgets 
    ├── Posts 
    │   └── locales 
    │    ├── en 
    │    │   └── translation.json 
    │    ├── es 
    │    │   └── translation.json 
    │    └── fr 
    │     └── translation.json 
    ├── Comments 
    │   └── locales 
    │    ├── en 
    │    │   └── translation.json 
    │    ├── es 
    │    │   └── translation.json 
    │    └── fr 
    │     └── translation.json 
    └── Links 
     ├── locales 
     │   ├── en 
     │   │   └── translation.json 
     │   ├── es 
     │   │   └── translation.json 
     │   └── fr 
     │    └── translation.json 

그리고 파일에 원하는 출력이 될 것이다 합병 :

 
App 
│ 
├── lang 
│   ├── en 
│   │   └── translation.json 
│   ├── es 
│   │   └── translation.json 
│   └── fr 
│    └── translation.json 
├── locales 
└── widgets 

지금까지 내가 사용하는 하나 개의 솔루션 함께했다 grunt-contrib-concat이지만, 더 나은 방법이 있어야한다고 생각합니다.

concat: { 
    translateEN: { 
    src: [ 
     'www/js/app/locales/en/*.json', 
     'www/js/app/widgets/posts/locales/en/*.json', 
     'www/js/app/widgets/comments/locales/en/*.json', 
     'www/js/app/widgets/links/locales/en/*.json' 
    ], 
    dest: 'www/js/app/lang/en/translation.json', 
    options: { 
     banner: '{', 
     footer: "}", 
     separator: ',' 
    } 
    }, 
    translateES: { 
    src: [ 
     'www/js/app/locales/es/*.json', 
     'www/js/app/widgets/posts/locales/es/*.json', 
     'www/js/app/widgets/comments/locales/es/*.json', 
     'www/js/app/widgets/links/locales/es/*.json' 
    ], 
    dest: 'www/js/app/lang/es/translation.json', 
    options: { 
     banner: '{', 
     footer: "}", 
     separator: ',' 
    } 
    }, 
    translateFR: { 
    src: [ 
     'www/js/app/locales/fr/*.json', 
     'www/js/app/widgets/posts/locales/fr/*.json', 
     'www/js/app/widgets/comments/locales/fr/*.json', 
     'www/js/app/widgets/links/locales/fr/*.json' 
    ], 
    dest: 'www/js/app/lang/fr/translation.json', 
    options: { 
     banner: '{', 
     footer: "}", 
     separator: ',' 
    } 
    } 
} 
+0

정확히 무슨 더 나은해야 하는가? – jgillich

+0

'www/js/app/** locales/en/*. json'과 같은 것을 사용하면 코드를 작게 유지할 수 있지만 질문이 맞는지 확실하지 않습니다. – jgillich

답변

2

나는 이것에 대한 내 자신의 꿀꿀 거리는 소리 작업을 writting 결국 :

grunt.task.registerMultiTask('buildLocales', 'Build Locale files.', function() { 
    var that = this, 
     len = this.filesSrc.length, 
     outputDir, 
     outputFile, 
     originalFile, 
     destFile, 
     merged; 

    var jsonConcat = function(object1, object2) { 
     var key, a1, a2; 
     for (key in object2) { 
     if (object2.hasOwnProperty(key)) { 
      a2 = object2[key]; 
      a1 = object1[key]; 
      if (a1) { 
      a1.push.apply(a1, a2); 
      } else { 
      object1[key] = a2; 
      } 
     } 
     } 

     return object1; 
    }; 

    var iterateTroughFiles = function(abspath, rootdir, subdir, filename){ 
     if (abspath.indexOf('/.svn') === -1){ 
     outputDir = that.data.dest + '/' + subdir; 
     outputFile = outputDir + '/' + filename; 

     // If output dir doesnt exists, then create it 
     if (!grunt.file.exists(outputDir)) { 
      grunt.file.mkdir(outputDir); 
     } 

     originalFile = grunt.file.readJSON(abspath); 

     // if dest file doenst exist, then just copy it. 
     if (!grunt.file.exists(outputFile)) { 
      grunt.file.write(outputFile, JSON.stringify(originalFile)); 
     } else { 
      // read source file, read dest file. merge them. write it in dest file 
      destFile = grunt.file.readJSON(outputFile); 

      merged = jsonConcat(destFile, originalFile); 

      grunt.file.write(outputFile, JSON.stringify(merged)); 
     } 
     } 
    }; 

    for (var x = 0; x < len; x++) { 
     grunt.file.recurse(this.filesSrc[x], iterateTroughFiles); 
    } 
    }); 

를 그리고 구현이 같은 것입니다 :

buildLocales: { 
    locales:{ 
    src: [ 
     'www/js/app/**/locales' 
    ], 
    dest: PATH_BUILD_LANGUAGES 
    } 
}, 
+0

grunt 플러그인으로 이것을 게시 했습니까? 다른 사람들에게 유용 할 수 있습니다 – jonschlinkert

+0

예. https://github.com/sabarasaba/grunt-contrib-i18next –

+0

감사합니다. – jonschlinkert

관련 문제