Javascript 使用Grunt处理JSON文件的适当方法

Javascript 使用Grunt处理JSON文件的适当方法,javascript,json,gruntjs,Javascript,Json,Gruntjs,我正在寻找使用Grunt组合json文件以维护特定结构的最佳方法 文件以如下结构放置在文件夹中: App ├── locales │   ├── en │   │   └── translation.json │   ├── es │   │   └── translation.json │   └── fr │   └── translation.json └── widgets ├── Posts │   └── locales │   ├── en

我正在寻找使用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
buildLocales: {
  locales:{
    src: [
      'www/js/app/**/locales'
    ],
    dest: PATH_BUILD_LANGUAGES
  }
},

我最终写下了自己的grunt任务:

  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);
    }
  });

到底什么更好?你可以使用
www/js/app/**locales/en/*.json
这样的工具来使你的代码更小,但我甚至不确定这是否是个问题。你是否将其作为grunt插件发布?可能对其他人有用
  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
  }
},