grunt-string-replace:替换目录及其子目录中所有文件中的字符串

时间:2013-07-05 16:13:12

标签: gruntjs

我想在目录及其子目录中的所有文件中使用grunt-string-replace替换字符串

例如,在所有这些文件中:

dist/templates_and_modules/templates/template1/template1.php
dist/templates_and_modules/templates/template2/template2.php
dist/templates_and_modules/modules/module1.php
dist/templates_and_modules/modules/module1.php

我想替换

/*remove->*/

使用:

/*

/*<-remove*/

*/

使用明确定义的文件,它可以工作:

strrep: {
    dist: {
        files: {
            '<%= yeoman.dist %>/templates_and_modules/templates/template1/template1.php':
            '<%= yeoman.dist %>/templates_and_modules/templates/template1/template1.php'
         },
         options: {
             replacements: [
                 {
                     pattern: '/*remove->*/',
                     replacement: '/*'
                 },

                 {
                     pattern: '/*<-remove*/',
                     replacement: '*/'
                 }
                ]
            }
        }
    }

但我不能让它与目录中的所有文件一起使用。

2 个答案:

答案 0 :(得分:7)

通过反复试验,我发现这有效:

files: {
    './': 'dist/**/*.*'
}

但我不明白,为什么。

答案 1 :(得分:5)

您应该可以使用globbing patterns来定义哪些文件&amp;它处理的文件夹。

另请参阅Files section of Configuring Tasks

这样的事情:

  src: [ '<%= yeoman.app %>/templates_and_modules/**/*.php' ]
  dest: '<%= yeoman.dist %>/templates_and_modules/'

但是,该插件似乎是为了将文件复制到新目的地而不是像您尝试的那样就地修改它们。这是一个将它们复制到新位置的完整示例:

module.exports = function(grunt) {
  grunt.initConfig({
    'string-replace': {
      dist: {
        src: './app/mylibs/**/*.js',
        dest: './dist/mylibs/',
        options: {
          replacements: [{
            pattern: 'old',
            replacement: 'new'
          }]
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-string-replace');
  grunt.registerTask('default', ['string-replace']);
};
相关问题