用Grunt连接JSON文件的合适方法

Appropriate way to concat JSON files with Grunt

本文关键字:方法 文件 Grunt 连接 JSON      更新时间:2023-09-26

我正在寻找最好的方法来组合json文件,使用Grunt保持一定的结构。

文件按照如下结构被放置在文件夹中:

<>之前应用程序├──地区│├──en││├──translate .json│├──├││├──translate .json││││├──translate .json└──小部件├──的帖子│├──├─├│├──en││├──translate .json│├──├││├──translate .json││││├──translate .json├──评论│├──├─├│├──en││├──translate .json│├──├││├──translate .json││││├──translate .json└──链接├──地区│├──en││├──translate .json│├──├││├──translate .json││││├──translate .json之前

合并后的期望输出是:

<>之前应用程序│├──朗│├──en││├──translate .json│├──├││├──translate .json││││├──translate .json├──地区└──小部件之前

到目前为止,我想出了一个使用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: ','
    }
  }
}

最后我自己写了一个grunt task:

  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
  }
},