【问题标题】:Appropriate way to concat JSON files with Grunt使用 Grunt 连接 JSON 文件的适当方法
【发布时间】:2013-04-25 18:21:04
【问题描述】:

我正在寻找使用 Grunt 组合保持特定结构的 json 文件的最佳方法。

文件以这样的结构放置在文件夹中:

应用程序 ├── 语言环境 │   ├── zh │   │   └── translation.json │   ├── es │   │   └── translation.json │   └── fr │   └── translation.json └── 小部件 ├── 帖子 │   └── 语言环境 │   ├── zh │   │   └── translation.json │   ├── es │   │   └── translation.json │   └── fr │   └── translation.json ├── 评论 │   └── 语言环境 │   ├── zh │   │   └── translation.json │   ├── es │   │   └── translation.json │   └── fr │   └── translation.json └── 友情链接 ├── 语言环境 │   ├── zh │   │   └── translation.json │   ├── es │   │   └── translation.json │   └── fr │   └── translation.json

合并文件后所需的输出将是:

应用程序 │ ├── 郎 │   ├── zh │   │   └── translation.json │   ├── es │   │   └── translation.json │   └── fr │   └── translation.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: ','
    }
  }
}

【问题讨论】:

  • 究竟什么应该更好?
  • 您可以使用 www/js/app/**locales/en/*.json 之类的东西来保持您的代码更小,但我什至不确定这是否是问题所在。

标签: javascript json gruntjs


【解决方案1】:

我最终为此编写了自己的繁重任务:

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

【讨论】:

  • 你把它作为一个 grunt 插件发布了吗?可能对其他人有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多