2013-07-05 70 views
3

我想用在那些目錄中的所有文件咕嚕字符串替換替換字符串及其子目錄咕嚕字符串替換:在目錄中的所有文件和子目錄

例如替換字符串在所有這些文件:

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: '*/' 
       } 
       ] 
      } 
     } 
    } 

但我不能讓它使用目錄中的所有文件。

回答

6

通過反覆試驗,我發現,這個工程:

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

但我不明白,爲什麼。

+0

而不是提供一個對象,嘗試提供一個數組。 ['dist /**/*.*']將查找並替換所有文件,其中'a':'b'將用新的更改寫入b。 – lededje

+1

是的,它適用於我。我之前使用'./production/html/':'* .html',它不工作,沒有錯誤,我認爲這個插件找不到這個設置的目錄。當我把它改成''./':'production/html/*。html''時,沒關係。 –

5

您應該可以使用globbing patterns來定義它處理的文件夾文件&。

又見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']); 
}; 
+0

這似乎不起作用。如果有人可以發佈一個適用於grunt-string-replace – gang

+0

@gang的實際示例,會很酷嗎?現在會發生什麼?它沒有找到這些文件,或者替換文件在這些文件上無法正常工作?你能發佈你的最新配置嗎? – explunit

+2

我收到一個錯誤:警告:無法讀取「dist/templates_and_modules /」文件(錯誤代碼:EISDIR)。使用--force繼續。 – gang

相關問題