简体   繁体   中英

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:

      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.

My issue is that for the first replacement pattern I need to read the relative prefs.js for each folder.

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.

I managed to achieve what I wanted by using a config variable and a function for the grunt-replace replacement option.

  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.

The count is then increased with each iteration ready for the next path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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