file-system
Advanced tools
Comparing version 1.0.3 to 1.0.4
37
index.js
@@ -224,1 +224,38 @@ /** | ||
}; | ||
/** | ||
* @description | ||
* Same as recurse, but it is synchronous | ||
* @returns {Array} selected files path. | ||
* @example | ||
* var filesPath = file.recurseSync('path'); | ||
* var filesPath = file.recurseSync('path', ['*.js', 'path/**\/*.html']); | ||
*/ | ||
exports.recurseSync = function(dirpath, filter) { | ||
var filterReg = filterToReg(filter); | ||
var filesPath = []; | ||
function recurse(dirpath) { | ||
fs.readdirSync(dirpath).forEach(function(file) { | ||
var filepath = path.join(dirpath, file); | ||
var stats = fs.statSync(filepath); | ||
if (stats.isDirectory()) { | ||
recurse(filepath); | ||
} else { | ||
if (filterReg) { | ||
var _filepath = util.path.unixifyPath(filepath); | ||
if (filterReg.test(_filepath)) { | ||
filesPath.push(filepath); | ||
} | ||
} else { | ||
filesPath.push(filepath); | ||
} | ||
} | ||
}); | ||
} | ||
recurse(dirpath); | ||
return filesPath; | ||
}; |
{ | ||
"name": "file-system", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Strengthen the ability of file system", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -37,3 +37,13 @@ ## file-system | ||
file.recurse('path', ['*.js', 'path/**\/*.html'], function(filepath, filename) { }); | ||
file.recurse('path', ['*.js', 'path/**/*.html'], function(filepath, filename) { }); | ||
``` | ||
### file.recurseSync | ||
```js | ||
var filesPath = file.recurseSync('path'); | ||
var filesPath = file.recurseSync('path', ['*.js', 'path/**/*.html']); | ||
filesPath.forEach(function(item) { | ||
}); | ||
``` |
@@ -50,3 +50,3 @@ var assert = require("assert"); | ||
it('filter params', function(done) { | ||
it('recurse filter files', function(done) { | ||
var count = 0; | ||
@@ -72,2 +72,23 @@ var filterPath = [ | ||
it('recurseSync files', function() { | ||
var filesPath = file.recurseSync(getPath('var/recurse/filter')); | ||
assert.equal(filesPath.length, allFiles[1].length); | ||
}); | ||
it('recurseSync filter files', function() { | ||
var filesPath = file.recurseSync(getPath('var/recurse/filter'), [ | ||
'*.js', | ||
'1/**/*.css' | ||
]); | ||
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') | ||
]; | ||
assert.deepEqual(filesPath, filesPath); | ||
}); | ||
after(function() { | ||
@@ -74,0 +95,0 @@ grunt.file.delete(getPath('var/recurse/'), { |
14412
454
48