Comparing version 1.6.0 to 1.7.0
@@ -24,5 +24,5 @@ // Generated by CoffeeScript 1.6.3 | ||
Finder.prototype.excludes = []; | ||
Finder.prototype.excludes = null; | ||
Finder.prototype.filters = []; | ||
Finder.prototype.filters = null; | ||
@@ -33,2 +33,4 @@ Finder.prototype.systemFiles = false; | ||
Finder.prototype.findFirst = false; | ||
function Finder(directory) { | ||
@@ -40,2 +42,4 @@ directory = _path.resolve(directory); | ||
this.directory = directory; | ||
this.excludes = []; | ||
this.filters = []; | ||
} | ||
@@ -97,2 +101,7 @@ | ||
Finder.prototype.findFirst = function(findFirst) { | ||
this.findFirst = findFirst != null ? findFirst : true; | ||
return this; | ||
}; | ||
Finder.prototype.filter = function(fn) { | ||
@@ -104,3 +113,3 @@ this.filters.push(fn); | ||
Finder.prototype.getPaths = function(dir, type, mask) { | ||
var exclude, filter, ok, path, paths, stat, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; | ||
var exclude, filter, ok, path, paths, result, stat, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; | ||
if (type == null) { | ||
@@ -152,2 +161,5 @@ type = 'all'; | ||
} | ||
if (this.findFirst === true) { | ||
return path; | ||
} | ||
paths.push(path); | ||
@@ -157,10 +169,21 @@ } | ||
if (stat.isDirectory() && this.recursive === true) { | ||
paths = paths.concat(this.getPaths(path, type, mask)); | ||
result = this.getPaths(path, type, mask); | ||
if (this.findFirst === true && typeof result === 'string') { | ||
return result; | ||
} else if (this.findFirst === true && result === null) { | ||
continue; | ||
} else { | ||
paths = paths.concat(result); | ||
} | ||
} | ||
} | ||
return paths; | ||
if (this.findFirst === true) { | ||
return null; | ||
} else { | ||
return paths; | ||
} | ||
}; | ||
Finder.prototype.getPathsFromParents = function(mask, type) { | ||
var depth, directory, i, paths, _i, _ref; | ||
var depth, directory, i, paths, result, _i, _ref; | ||
if (mask == null) { | ||
@@ -175,9 +198,23 @@ mask = null; | ||
paths = this.getPaths(directory, type, mask); | ||
if (this.findFirst === true && typeof paths === 'string') { | ||
return paths; | ||
} | ||
this.exclude(directory); | ||
for (i = _i = 0, _ref = depth - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { | ||
directory = _path.dirname(directory); | ||
paths = paths.concat(this.getPaths(directory, type, mask)); | ||
result = this.getPaths(directory, type, mask); | ||
if (this.findFirst === true && typeof result === 'string') { | ||
return result; | ||
} else if (this.findFirst === true && result === null) { | ||
} else { | ||
paths = paths.concat(result); | ||
} | ||
this.exclude(directory); | ||
} | ||
return paths; | ||
if (this.findFirst === true) { | ||
return null; | ||
} else { | ||
return paths; | ||
} | ||
}; | ||
@@ -184,0 +221,0 @@ |
{ | ||
"name": "fs-finder", | ||
"description": "File system recursive finder", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "David Kudera", |
@@ -18,5 +18,4 @@ # fs-finder | ||
var Finder = require('fs-finder'); | ||
var finder = new Finder('/var/data/base-path'); | ||
var files = finder.findFiles(); // returns array with file's names | ||
var files = Finder.in('/var/data/base-path').findFiles(); // returns array with file's names | ||
``` | ||
@@ -27,3 +26,3 @@ | ||
``` | ||
var directories = finder.findDirectories(); // returns array with directories's names | ||
var directories = Finder.in(baseDir).findDirectories(); // returns array with directories's names | ||
``` | ||
@@ -34,3 +33,3 @@ | ||
``` | ||
var paths = finder.find(); // returns array with file's and directories's names | ||
var paths = Finder.in(baseDir).find(); // returns array with file's and directories's names | ||
``` | ||
@@ -41,3 +40,3 @@ | ||
``` | ||
var paths = finder.recursively().find(); | ||
var paths = Finder.from(baseDir).find(); | ||
``` | ||
@@ -48,3 +47,3 @@ | ||
``` | ||
var files = finder.recursively().findFiles('*.coffee'); | ||
var files = Finder.from(baseDir).findFiles('*.coffee'); | ||
``` | ||
@@ -58,3 +57,3 @@ | ||
``` | ||
var files = finder.recursively().findFiles('temp/<[0-9]+>.tmp'); // files in temp directories with numbers in name and .tmp extension | ||
var files = Finder.from(baseDir).findFiles('temp/<[0-9]+>.tmp'); // files in temp directories with numbers in name and .tmp extension | ||
``` | ||
@@ -67,3 +66,3 @@ | ||
``` | ||
var files = finder.recursively().exclude(['/.git']).findFiles(); | ||
var files = Finder.from(baseDir).exclude(['/.git']).findFiles(); | ||
``` | ||
@@ -79,3 +78,3 @@ | ||
``` | ||
var files = finder.recursively().size('>=', 450).size('<=' 500).findFiles(); | ||
var files = Finder.from(baseDir).size('>=', 450).size('<=' 500).findFiles(); | ||
``` | ||
@@ -88,3 +87,3 @@ | ||
``` | ||
var files = finder.recursively().date('>', {minutes: 10}).date('<', {minutes: 1}).findFiles(); | ||
var files = Finder.from(baseDir).date('>', {minutes: 10}).date('<', {minutes: 1}).findFiles(); | ||
``` | ||
@@ -107,3 +106,3 @@ | ||
var files = finder.recursively().filter(filter).findFiles(); | ||
var files = Finder.from(baseDir).filter(filter).findFiles(); | ||
``` | ||
@@ -121,4 +120,3 @@ | ||
``` | ||
var files = finder.showSystemFiles(true).findFiles() | ||
var files = finder.showSystemFiles(false).findFiles() | ||
var files = Finder.in(dir).showSystemFiles().findFiles() | ||
``` | ||
@@ -129,40 +127,32 @@ | ||
Finder can also look for files in parent directories. There is used `exclude` method, so directories in which were your | ||
files already searched, will not be opened for searching again in their next parent directory. | ||
files already searched, will not be opened for searching again in their next parent directory if you are using `from` method. | ||
Keep in mind that one of parent directories is also your disk root directory, so you can set depth of count of these parents. | ||
Of course, you can combine it with other methods, even with `recursive`. | ||
``` | ||
var files = finder.lookUp().findFiles('5.log'); | ||
var files = Finder.in(dir).lookUp().findFiles('5.log'); | ||
// or set depth | ||
var files = finder.lookUp(3).findFiles('5.log'); | ||
``` | ||
var files = Finder.in(dir).lookUp(3).findFiles('5.log'); | ||
## Shortcuts | ||
## Find first | ||
If you want to look for files or directories recursively without any filters, you can use shorter way. | ||
When you want to find first occur of some file or directory, you can use option `findFirst`. fs-finder will not look into | ||
all directories (for recursive searching) but stop when it will find first matching path. | ||
``` | ||
var Finder = require('fs-finder'); | ||
If there is no matching path, null will be returned. | ||
var files = Finder.findFiles('/var/data/base-path/*js'); // Returns files | ||
var directories = Finder.findDirectories('/var/data/base-path'); // Returns directories | ||
var paths = Finder.find('/var/data/base-path'); // Returns files and directories | ||
``` | ||
var file = Finder.from(dir).findFirst().findFiles('<[0-9]{2}>'); | ||
``` | ||
var files = Finder.findFiles('/var/data/base-path/<(.git|.idea)*[0-9]>'); // Returns every file with .git or .idea and also with number in path | ||
``` | ||
For more advanced options you can use in and from functions. | ||
## Changelog | ||
``` | ||
var files = Finder.in('/var/data/base-path').findFiles(); // Load files only from base-path directory | ||
var files = Finder.from('/var/data/base-path').findFiles(); // Load files recursively | ||
``` | ||
* 1.7.0 | ||
+ Mistake in test | ||
+ Bug in Finder (filters were shared across all instances) | ||
+ Preferred way is to use 'static' methods | ||
+ Tests uses 'static' methods | ||
+ Added `findFirst` option | ||
## Changelog | ||
* 1.6.0 | ||
@@ -169,0 +159,0 @@ + New reporter for tests |
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var Finder, dir, finder, fs, path, should; | ||
var Finder, dir, fs, path, should; | ||
@@ -15,4 +15,2 @@ should = require('should'); | ||
finder = new Finder(dir); | ||
describe('Finder', function() { | ||
@@ -22,3 +20,3 @@ describe('base', function() { | ||
return (function() { | ||
return new finder("" + dir + "/two"); | ||
return new Finder("" + dir + "/two"); | ||
}).should["throw"](); | ||
@@ -29,3 +27,3 @@ }); | ||
return it('should return file names from root folder', function() { | ||
return finder.findFiles().should.eql(["" + dir + "/0", "" + dir + "/1", "" + dir + "/five", "" + dir + "/one", "" + dir + "/three", "" + dir + "/two"]); | ||
return Finder["in"](dir).findFiles().should.eql(["" + dir + "/0", "" + dir + "/1", "" + dir + "/five", "" + dir + "/one", "" + dir + "/three", "" + dir + "/two"]); | ||
}); | ||
@@ -35,3 +33,3 @@ }); | ||
return it('should return directory names from root folder', function() { | ||
return finder.findDirectories().should.eql(["" + dir + "/eight", "" + dir + "/seven", "" + dir + "/six"]); | ||
return Finder["in"](dir).findDirectories().should.eql(["" + dir + "/eight", "" + dir + "/seven", "" + dir + "/six"]); | ||
}); | ||
@@ -41,10 +39,31 @@ }); | ||
return it('should return file and directory names from root folder', function() { | ||
return finder.find().should.eql(["" + dir + "/0", "" + dir + "/1", "" + dir + "/eight", "" + dir + "/five", "" + dir + "/one", "" + dir + "/seven", "" + dir + "/six", "" + dir + "/three", "" + dir + "/two"]); | ||
return Finder["in"](dir).find().should.eql(["" + dir + "/0", "" + dir + "/1", "" + dir + "/eight", "" + dir + "/five", "" + dir + "/one", "" + dir + "/seven", "" + dir + "/six", "" + dir + "/three", "" + dir + "/two"]); | ||
}); | ||
}); | ||
describe('#findFirst()', function() { | ||
it('should return file path', function() { | ||
return Finder["in"](dir).findFirst().findFiles().should.be.equal("" + dir + "/0"); | ||
}); | ||
it('should return null', function() { | ||
return should.not.exists(Finder["in"](dir).findFirst().findFiles('randomName')); | ||
}); | ||
it('should return file path for first name with two numbers in name', function() { | ||
return Finder.from(dir).findFirst().findFiles('<[0-9]{2}>').should.be.equal("" + dir + "/seven/13"); | ||
}); | ||
it('should return null for recursive searching', function() { | ||
return should.not.exists(Finder.from(dir).findFirst().findFiles('randomName')); | ||
}); | ||
it('should return first path to directory', function() { | ||
return Finder.from(dir).findFirst().findDirectories('4').should.be.equal("" + dir + "/eight/3/4"); | ||
}); | ||
it('should return null when looking into parents', function() { | ||
return should.not.exists(Finder["in"]("" + dir + "/eight/3/4").lookUp(4).findFirst().findFiles('twelve')); | ||
}); | ||
return it('should return first file when looking into parents recursively', function() { | ||
return Finder.from("" + dir + "/eight/3/4").lookUp(4).findFirst().findFiles('twelve').should.equal("" + dir + "/seven/twelve"); | ||
}); | ||
}); | ||
describe('#recursive()', function() { | ||
return it('should return file names recursively from find* methods', function() { | ||
finder.recursively(true); | ||
finder.findFiles().should.eql(["" + dir + "/0", "" + dir + "/1", "" + dir + "/eight/3/4/file.json", "" + dir + "/five", "" + dir + "/one", "" + dir + "/seven/13", "" + dir + "/seven/14", "" + dir + "/seven/twelve", "" + dir + "/six/eleven", "" + dir + "/six/nine", "" + dir + "/six/ten", "" + dir + "/three", "" + dir + "/two"]); | ||
return finder.recursively(false); | ||
return Finder.from(dir).findFiles().should.eql(["" + dir + "/0", "" + dir + "/1", "" + dir + "/eight/3/4/file.json", "" + dir + "/five", "" + dir + "/one", "" + dir + "/seven/13", "" + dir + "/seven/14", "" + dir + "/seven/twelve", "" + dir + "/six/eleven", "" + dir + "/six/nine", "" + dir + "/six/ten", "" + dir + "/three", "" + dir + "/two"]); | ||
}); | ||
@@ -54,5 +73,3 @@ }); | ||
return it('should return files which has not got numbers in name', function() { | ||
finder.exclude(['<[0-9]>']); | ||
finder.findFiles().should.eql(["" + dir + "/five", "" + dir + "/one", "" + dir + "/three", "" + dir + "/two"]); | ||
return finder.excludes = []; | ||
return Finder["in"](dir).exclude(['<[0-9]>']).findFiles().should.eql(["" + dir + "/five", "" + dir + "/one", "" + dir + "/three", "" + dir + "/two"]); | ||
}); | ||
@@ -62,5 +79,3 @@ }); | ||
return it('should return also system, hide and temp files', function() { | ||
finder.showSystemFiles(true); | ||
finder.findFiles().should.eql(["" + dir + "/.cache", "" + dir + "/0", "" + dir + "/1", "" + dir + "/five", "" + dir + "/five~", "" + dir + "/one", "" + dir + "/three", "" + dir + "/two"]); | ||
return finder.showSystemFiles(false); | ||
return Finder["in"](dir).showSystemFiles().findFiles().should.eql(["" + dir + "/.cache", "" + dir + "/0", "" + dir + "/1", "" + dir + "/five", "" + dir + "/five~", "" + dir + "/one", "" + dir + "/three", "" + dir + "/two"]); | ||
}); | ||
@@ -77,9 +92,5 @@ }); | ||
describe('filters', function() { | ||
afterEach(function() { | ||
return finder.filters = []; | ||
}); | ||
describe('#size()', function() { | ||
return it('should return files with size between 2000B and 3000B', function() { | ||
finder.size('>=', 2000).size('<=', 3000); | ||
return finder.findFiles().should.eql(["" + dir + "/five"]); | ||
return Finder["in"](dir).size('>=', 2000).size('<=', 3000).findFiles().should.eql(["" + dir + "/five"]); | ||
}); | ||
@@ -90,6 +101,5 @@ }); | ||
fs.writeFileSync("" + dir + "/two", 'just some changes'); | ||
finder.date('>', { | ||
return Finder["in"](dir).date('>', { | ||
minutes: 1 | ||
}); | ||
return finder.findFiles().should.eql(["" + dir + "/two"]); | ||
}).findFiles().should.eql(["" + dir + "/two"]); | ||
}); | ||
@@ -99,8 +109,9 @@ }); | ||
return it('should return files which names are 3 chars length', function() { | ||
finder.filter(function(stat, file) { | ||
var filter; | ||
filter = function(stat, file) { | ||
var name; | ||
name = path.basename(file, path.extname(file)); | ||
return name.length === 3; | ||
}); | ||
return finder.findFiles().should.eql(["" + dir + "/one", "" + dir + "/two"]); | ||
}; | ||
return Finder["in"](dir).filter(filter).findFiles().should.eql(["" + dir + "/one", "" + dir + "/two"]); | ||
}); | ||
@@ -107,0 +118,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35939
429
185