Skip to content

Commit

Permalink
More replace task documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyan committed Dec 31, 2013
1 parent 43d19f2 commit fc08138
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/tasks/replace.js
Expand Up @@ -17,18 +17,18 @@ exports.options = {

"search" : {
alias : 's'
,describe : 'search string'
,describe : 'A string or regular expression that will be replaced by the new value'
},

"replace" : {
alias : 'r'
,describe : 'replace string'
,describe : 'A string that replaces the search string or a function to be invoked to create the new string'
},

"flags" : {
alias : 'f'
,default : 'gm'
,describe : 'flags'
,describe : 'A String containing any combination of the RegExp flags: g - global match, i - ignore case, m - match over multiple lines. This parameter is only used if the search parameter is a string'
},

'output': {
Expand All @@ -47,10 +47,9 @@ exports.options = {
exports.run = function (options) {

//console.log(args.argv);
var src = options.src;
var dest = options.dest;
var replace = options.replace;
var search = options.search;
var replace = options.replace;
var charset = options.charset;
var flags = options.flags;
var output = options.output;
Expand All @@ -67,20 +66,20 @@ exports.run = function (options) {
}
result = exports.replace(inputFile, outputFile, search, replace, charset, flags);
});

return result;
};

exports.replace = function(inputFile, outputFile, search, replace, charset, flags){

charset = charset || "utf-8";
flags = flags || "gm"; //global multiline ignoreCase
flags = flags || "gm"; // Regexp flags: global/multiline/ignoreCase

if(_.isString(search)){
search = new RegExp(search,flags);
search = new RegExp(utils.escapeRegExp(search), flags);
}

var input = fs.readFileSync(inputFile, charset);
//var input = fs.readFileSync(inputFile).toString();
var input = file.read(inputFile, charset);
var output = input.replace(search, replace);

// have replaced
Expand All @@ -92,7 +91,7 @@ exports.replace = function(inputFile, outputFile, search, replace, charset, flag
}

if(outputFile){
fs.writeFileSync(outputFile, output, charset);
file.write(outputFile, output, charset);
}

return output;
Expand Down

0 comments on commit fc08138

Please sign in to comment.