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

gan*_*ang 3 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
Run Code Online (Sandbox Code Playgroud)

我想替换

/*remove->*/
Run Code Online (Sandbox Code Playgroud)

有:

/*
Run Code Online (Sandbox Code Playgroud)

/*<-remove*/
Run Code Online (Sandbox Code Playgroud)

*/
Run Code Online (Sandbox Code Playgroud)

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

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: '*/'
                 }
                ]
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

gan*_*ang 7

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

files: {
    './': 'dist/**/*.*'
}
Run Code Online (Sandbox Code Playgroud)

但我不明白,为什么.


exp*_*nit 5

您应该能够使用通配模式来定义它处理的文件和文件夹.

另请参阅配置任务的" 文件"部分

像这样的东西:

  src: [ '<%= yeoman.app %>/templates_and_modules/**/*.php' ]
  dest: '<%= yeoman.dist %>/templates_and_modules/'
Run Code Online (Sandbox Code Playgroud)

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

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']);
};
Run Code Online (Sandbox Code Playgroud)

  • 我收到错误:警告:无法读取"dist/templates_and_modules /"文件(错误代码:EISDIR).使用--force继续. (2认同)