Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
use minimatch package for include and exclude matching
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed Feb 19, 2013
1 parent a8f46fd commit 605706d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ replace '(\w+)_(\w+)' '$1-$2' *
Replace only in files with names matching *.js:

```
replace 'foo' 'bar' . -r --include="*.js"
replace 'foo' 'bar' . -r --include=*.js
```

Don't replace in files with names matching *.min.js and *.py:
Expand Down
8 changes: 4 additions & 4 deletions defaultignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Repositories #
*.git*
*.hg*
*.svn*
.git
.hg
.svn

# Font files #
*.ttf
Expand Down Expand Up @@ -59,6 +59,6 @@
*.sqlite

# OS generated files #
*.DS_Store*
.DS_Store
ehthumbs.db
Thumbs.db
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"dependencies": {
"nomnom": "1.5.x",
"colors": "0.5.x"
"colors": "0.5.x",
"minimatch": "~0.2.9"
},
"devDependencies": {
"tape": "~0.2.2"
Expand Down
85 changes: 36 additions & 49 deletions replace.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
var fs = require("fs"),
path = require("path"),
colors = require("colors");

module.exports = function(opts) {
var excludes = [],
includes,
regex,
canReplace,
replaceFunc,
lineCount = 0,
limit = 400, // chars per line
options;

options = opts;
if (!options.color) options.color = "cyan";
colors = require("colors"),
minimatch = require("minimatch");

module.exports = function(options) {
var lineCount = 0,
limit = 400; // chars per line

if (!options.color) {
options.color = "cyan";
}

var flags = "g"; // global multiline
if (options.ignoreCase) {
flags += "i";
Expand All @@ -22,60 +19,50 @@ module.exports = function(opts) {
flags += "m";
}

var regex;
if (options.regex instanceof RegExp) {
regex = options.regex;
}
else {
regex = new RegExp(options.regex, flags);
}
canReplace = !options.preview && options.replacement !== undefined;
var canReplace = !options.preview && options.replacement !== undefined;

var includes;
if (options.include) {
includes = options.include.split(",").map(patternToRegex);
}
var excludes = [];
if (options.exclude) {
excludes = options.exclude.split(",");
}
var listFile = options.excludeList || path.join(__dirname, '/defaultignore');
var list = fs.readFileSync(listFile, "utf-8").split("\n");
excludes = excludes.concat(list)
.filter(function(line) {
return line && line.indexOf("#");
})
.map(patternToRegex);
var ignoreFile = options.excludeList || path.join(__dirname, '/defaultignore');
var ignores = fs.readFileSync(ignoreFile, "utf-8").split("\n");
excludes = excludes.concat(ignores);

var replaceFunc;
if (options.funcFile) {
eval('replaceFunc = ' + fs.readFileSync(options.funcFile, "utf-8"));
eval('replaceFunc = ' + fs.readFileSync(options.funcFile, "utf-8"));
}

for (var i = 0; i < options.paths.length; i++) {
if(options.async) {
if (options.async) {
replacizeFile(options.paths[i]);
}
else {
replacizeFileSync(options.paths[i]);
}
}

function patternToRegex(pattern) {
return new RegExp("^" + pattern.replace(/\./g, '\\.').replace(/\*/g, '.*').trim() + "$");
}
function canSearch(file) {
var inIncludes = includes && includes.some(function(include) {
return minimatch(file, include, { matchBase: true });
})
var inExcludes = excludes && excludes.some(function(exclude) {
return minimatch(file, exclude, { matchBase: true });
})

function includeFile(file) {
if (includes) {
for (var i = 0; i < includes.length; i++) {
if (file.match(includes[i]))
return true;
}
return false;
}
else {
for (var i = 0; i < excludes.length; i++) {
if (file.match(excludes[i]))
return false;
}
return true;
}
return ((!includes || inIncludes) && (!excludes || !inExcludes));
}

function replacizeFile(file) {
Expand All @@ -86,10 +73,10 @@ module.exports = function(opts) {
// don't follow symbolic links for now
return;
}
if (!canSearch(file)) {
return;
}
if (stats.isFile()) {
if (!includeFile(file)) {
return;
}
fs.readFile(file, "utf-8", function(err, text) {
if (err) {
if (err.code == 'EMFILE') {
Expand All @@ -100,7 +87,7 @@ module.exports = function(opts) {
}

text = replacizeText(text, file);
if(canReplace) {
if (canReplace) {
fs.writeFile(file, text, function(err) {
if (err) throw err;
});
Expand All @@ -124,10 +111,10 @@ module.exports = function(opts) {
// don't follow symbolic links for now
return;
}
if (!canSearch(file)) {
return;
}
if (stats.isFile()) {
if (!includeFile(file)) {
return;
}
var text = fs.readFileSync(file, "utf-8");

text = replacizeText(text, file);
Expand Down

0 comments on commit 605706d

Please sign in to comment.