2015-05-14 44 views
0

我有一個簡單的任務,我怎樣才能改變與咕嚕另一行的內容?

當我寫的命令

「咕嚕打造」我希望這2條線改變狀態,就像這樣:

 baseUrl : 'http://localhost:3000/' 
     //baseUrl : 'http://188.166.18.108/' 

後:該命令之前

命令

 //baseUrl : 'http://localhost:3000/' 
     baseUrl : 'http://188.166.18.108/' 

構建變化的最終回:

 baseUrl : 'http://localhost:3000/' 
     //baseUrl : 'http://188.166.18.108/' 

無論如何可以用grunt來這麼做嗎?多謝你們!

+0

這是哪裏的內容在什麼位置?進入一個js文件? – jmartins

+0

是的,在一個完整的js文件目錄裏面 – totothegreat

回答

3

你應該嘗試使用grunt-string-replace,像這樣:

'string-replace': { 
    dist: { 
    files: { 
     src: 'path/to/your/file', 
     dest: 'path/to/your/file' 
    }, 
    options: { 
     replacements: [{ 
     pattern: "baseUrl : 'http://localhost:3000/'", 
     replacement: "baseUrl : 'http://188.166.18.108:3000/'" 
     }] 
    } 
    } 
} 

那麼你只能有一個在你的文件中的行,而不必評論它。

baseUrl : 'http://localhost:3000/' 

另外,如果你想更換本地主機的所有出現,你可以使用它作爲一個模式,它會取代所有您IP地址

... 
pattern: "localhost", 
replacement: "188.166.18.108" 
... 

對於變回從IP地址本地主機您可以添加一個新任務來替換字符串並在您的構建中運行這兩個任務。這將是這樣的:

'string-replace': { 
    prev: { 
    files: { 
     src: 'path/to/your/file', 
     dest: 'path/to/your/file' 
    }, 
    options: { 
     replacements: [{ 
     pattern: "baseUrl : 'http://localhost:3000/'", 
     replacement: "baseUrl : 'http://188.166.18.108:3000/'" 
     }] 
    } 
    }, 
    after: { 
    files: { 
     src: 'path/to/your/file', 
     dest: 'path/to/your/file' 
    }, 
    options: { 
     replacements: [{ 
     pattern: "baseUrl : 'http://188.166.18.108:3000/'", 
     replacement: "baseUrl : 'http://localhost:3000/'" 
     }] 
    } 
    } 
} 

,然後運行它想:

grunt.registerTask('replace', ['string-replace:prev', 'string-replace:after']); 
相關問題