简体   繁体   English

可以在 grunt.file.read 中使用 globbing 模式吗?

[英]Can a globbing pattern be used in grunt.file.read?

I have the following folder structure:我有以下文件夹结构:

Project
-build
-gruntfiles
--includes
---bar2.js
---bar3.js
--gruntfile.js
-folder1
--prefs.js
--team
---file1.jsx
-folder2
--prefs.js
--team
---file2.jsx
-folder3
--prefs.js
--team
---file3.jsx

I'm then running a grunt-replace task that goes through all folders and replaces 3 different patterns with the contents of 3 different files:然后我运行一个 grunt-replace 任务,遍历所有文件夹并用 3 个不同文件的内容替换 3 个不同的模式:

      replace: {
        dist1: {
          options: {
            patterns: [
              {
                match: 'foo1',
                replacement: '<%= grunt.file.read("../**/prefs.js") %>'
              },
              {
                match: 'foo2',
                replacement: '<%= grunt.file.read("includes/bar2.js") %>'
              },
              {
                match: 'foo3',
                replacement: '<%= grunt.file.read("includes/bar3.js") %>'
              }
            ]
          },
          files: [
            {
              expand: true, 
              flatten: true, 
              src: ['../**/team/*.jsx'], 
              dest: '../build/team/'
            }
          ]
        }

Grunt reads the files located in the includes folder no problem. Grunt 读取 includes 文件夹中的文件没问题。

My issue is that for the first replacement pattern I need to read the relative prefs.js for each folder.我的问题是,对于第一个替换模式,我需要读取每个文件夹的相关prefs.js

I've tried grunt.file.expand which returns the entire list of prefs.js filepaths, I just need a way of selecting the correct one for the current folder.我试过grunt.file.expand返回整个prefs.js文件路径列表,我只需要一种为当前文件夹选择正确路径的方法。

I managed to achieve what I wanted by using a config variable and a function for the grunt-replace replacement option.我设法通过使用配置变量和 function 作为grunt-replace替换选项来实现我想要的。

  count: 0,
  replace: {
    dist1: {
      options: {
        patterns: [
          {
            match: 'foo1',
            replacement: function(){
              var count = grunt.config.get("count");
              var paths = grunt.file.expand('../**/prefs.js');
              var current_path = paths[count];
              var content = grunt.file.read(current_path);
              count++;
              grunt.config.set("count", count);
              return content;
            }
          },
          {
            match: 'foo2',
            replacement: '<%= grunt.file.read("includes/bar2.js") %>'
          },
          {
            match: 'foo3',
            replacement: '<%= grunt.file.read("includes/bar3.js") %>'
          }
        ]
      },
      files: [
        {
          expand: true, 
          flatten: true, 
          src: ['../**/team/*.jsx'], 
          dest: '../build/team/'
        }
      ]
    }

Inside the replacement function I now use grunt.file.expand to bring up the array of directory paths, and then use the count config variable to choose the correct one.在替换 function 中,我现在使用grunt.file.expand来调出目录路径数组,然后使用 count 配置变量来选择正确的路径。

The count is then increased with each iteration ready for the next path.然后随着为下一条路径准备的每次迭代增加计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM