file-system
Advanced tools
Comparing version 1.0.2 to 1.0.3
66
index.js
@@ -38,2 +38,23 @@ /** | ||
function filterToReg(filter) { | ||
if (!filter) return null; | ||
if (util.isString(filter)) { | ||
filter = [filter]; | ||
} | ||
var ret = []; | ||
filter.forEach(function(item) { | ||
item = item | ||
.replace(/\*\.([^\/]+)$/, '[^/]+.$1$') | ||
.replace('**\/', '([^/]+\/)*') | ||
.replace(/([\/\.])/g, '\\$1'); | ||
ret.push('(' + item + ')'); | ||
}); | ||
var reg = new RegExp(ret.join('|')); | ||
return reg; | ||
} | ||
/** | ||
@@ -167,19 +188,38 @@ * @description | ||
* file.recurse('path', function(filepath, filename) { }); | ||
* file.recurse('path', ['*.js', 'path/**\/*.html'], function(filepath, filename) { }); | ||
*/ | ||
exports.recurse = function(dirpath, callback) { | ||
fs.readdir(dirpath, function(err, files) { | ||
if (err) return callback(err); | ||
exports.recurse = function(dirpath, filter, callback) { | ||
if (util.isFunction(filter)) { | ||
callback = filter; | ||
filter = null; | ||
} | ||
var filterReg = filterToReg(filter); | ||
files.forEach(function(filename) { | ||
var filepath = path.join(dirpath, filename); | ||
function recurse(dirpath) { | ||
fs.readdir(dirpath, function(err, files) { | ||
if (err) return callback(err); | ||
fs.stat(filepath, function(err, stats) { | ||
if (stats.isDirectory()) { | ||
exports.recurse(filepath, callback); | ||
} else { | ||
callback(filepath, filename); | ||
} | ||
}); | ||
files.forEach(function(filename) { | ||
var filepath = path.join(dirpath, filename); | ||
fs.stat(filepath, function(err, stats) { | ||
if (stats.isDirectory()) { | ||
recurse(filepath); | ||
} else { | ||
if (filterReg) { | ||
// Normalize \\ paths to / paths. | ||
var _filepath = util.path.unixifyPath(filepath); | ||
if (filterReg.test(_filepath)) { | ||
callback(filepath, filename); | ||
} | ||
} else { | ||
callback(filepath, filename); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
recurse(dirpath); | ||
}; |
{ | ||
"name": "file-system", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Strengthen the ability of file system", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,5 +32,8 @@ ## file-system | ||
### file.recurse | ||
Recurse into a directory, executing callback for each file. | ||
Recurse into a directory, executing callback for each file. | ||
And you can pass filter params for filter file. | ||
```js | ||
file.recurse('path', function(filepath, filename) { }); | ||
file.recurse('path', ['*.js', 'path/**\/*.html'], function(filepath, filename) { }); | ||
``` |
@@ -13,3 +13,2 @@ var assert = require("assert"); | ||
var stat = fs.statSync(filepath); | ||
var mode = mask & parseInt((stat.mode & parseInt ("777", 8)).toString (8)[0]); | ||
@@ -16,0 +15,0 @@ |
@@ -12,20 +12,35 @@ var assert = require("assert"); | ||
describe('recurse', function() { | ||
var allFiles = [ | ||
[ | ||
getPath('var/recurse/simple/1/1.html'), | ||
getPath('var/recurse/simple/1/2.html'), | ||
getPath('var/recurse/simple/1.html') | ||
], | ||
[ | ||
getPath('var/recurse/filter/1/demo.js'), | ||
getPath('var/recurse/filter/1/2/demo.js'), | ||
getPath('var/recurse/filter/1/2/demo.css'), | ||
getPath('var/recurse/filter/1/2/demo.html'), | ||
getPath('var/recurse/filter/demo.html'), | ||
getPath('var/recurse/filter/demo.js'), | ||
getPath('var/recurse/filter/demo.css') | ||
] | ||
]; | ||
before(function() { | ||
file.writeFileSync(getPath('var/recurse/1/1.html')); | ||
file.writeFileSync(getPath('var/recurse/1/2.html')); | ||
file.writeFileSync(getPath('var/recurse/1.html')); | ||
allFiles.forEach(function(files) { | ||
files.forEach(function(item) { | ||
file.writeFileSync(item); | ||
}); | ||
}); | ||
}); | ||
it('recurse files', function(done) { | ||
var files = [ | ||
getPath('var/recurse/1/1.html'), | ||
getPath('var/recurse/1/2.html'), | ||
getPath('var/recurse/1.html') | ||
]; | ||
var filesPath = allFiles[0]; | ||
var count = 0; | ||
file.recurse(getPath('var/recurse'), function(filepath, filename) { | ||
assert.equal(true, files.indexOf(filepath) != -1); | ||
file.recurse(getPath('var/recurse/simple'), function(filepath, filename) { | ||
assert.equal(true, filesPath.indexOf(filepath) != -1); | ||
if (++count == files.length) { | ||
if (++count == filesPath.length) { | ||
done(); | ||
@@ -36,2 +51,23 @@ } | ||
it('filter params', function(done) { | ||
var count = 0; | ||
var filterPath = [ | ||
getPath('var/recurse/filter/1/demo.js'), | ||
getPath('var/recurse/filter/1/2/demo.js'), | ||
getPath('var/recurse/filter/1/2/demo.css'), | ||
getPath('var/recurse/filter/demo.js') | ||
]; | ||
file.recurse(getPath('var/recurse/filter'), [ | ||
'*.js', | ||
'1/**/*.css' | ||
], function(filepath, filename) { | ||
assert.equal(true, filterPath.indexOf(filepath) != -1); | ||
if (++count == filterPath.length) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
after(function() { | ||
@@ -38,0 +74,0 @@ grunt.file.delete(getPath('var/recurse/'), { |
@@ -57,2 +57,6 @@ var util = require('util'); | ||
exports.isString = function(value) { | ||
return typeof value === 'string'; | ||
}; | ||
exports.hrtime = function(time) { | ||
@@ -85,2 +89,8 @@ if (time) { | ||
}; | ||
// Normalize \\ paths to / paths. | ||
exports.path.unixifyPath = function(filepath) { | ||
return filepath.replace(/\\/g, '/'); | ||
}; | ||
} else { | ||
@@ -90,2 +100,6 @@ exports.path.isAbsolute = function(filepath) { | ||
}; | ||
exports.path.unixifyPath = function(filepath) { | ||
return filepath; | ||
}; | ||
} |
12713
405
38