Skip to content

Commit

Permalink
Merge pull request #41 from robinjmurphy/sync
Browse files Browse the repository at this point in the history
[WIP] Add findSync
  • Loading branch information
Nick Spragg committed Aug 16, 2016
2 parents 0ff1b86 + 2ba05d3 commit 03757bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/filehound.js
Expand Up @@ -43,6 +43,10 @@ class FileHound extends EventEmitter {
return files.readFiles(dir).map(files.joinWith(dir));
}

_getFilesSync(dir) {
return files.readFilesSync(dir).map(files.joinWith(dir));
}

_isMatch(file) {
let isMatch = compose(this.filters);
if (this.negateFilters) {
Expand Down Expand Up @@ -77,6 +81,19 @@ class FileHound extends EventEmitter {
});
}

_searchSync(root, dir) {
if (this._shouldFilterDirectory(root, dir)) return [];

return this._getFilesSync(dir)
.map((file) => {
return files.isDirectory(file) ? this._search(root, file) : file;
})
.reduce(flatten, [])
.filter((file) => {
return this._isMatch(file);
});
}

getSearchPaths() {
const excludeSubDirs = files.reducePaths(this.searchPaths);
return arrays.copy(excludeSubDirs);
Expand Down Expand Up @@ -179,6 +196,15 @@ class FileHound extends EventEmitter {
this.emit('end');
});
}

findSync() {
const paths = this.getSearchPaths();
const results = paths.map((path) => {
return this._searchSync(path, path);
});

return results.reduce(flatten);
}
}

module.exports = FileHound;
4 changes: 4 additions & 0 deletions src/files.js
Expand Up @@ -44,6 +44,10 @@ export function readFiles(dir) {
return bluebird.resolve(fsp.readdirAsync(dir));
}

export function readFilesSync(dir) {
return fs.readdirSync(dir);
}

export function findSubDirectories(paths) {
return paths
.map((path) => {
Expand Down
10 changes: 10 additions & 0 deletions test/filehound.js
Expand Up @@ -268,6 +268,16 @@ describe('FileHound', () => {
});
});

describe('.findSync', () => {
it('returns an array of matching files', () => {
const files = FileHound.create()
.paths(fixtureDir + '/justFiles')
.findSync();

assert.deepEqual(files, justFiles);
});
});

describe('.ext', () => {
it('returns files for a given ext', () => {
const query = FileHound.create()
Expand Down

0 comments on commit 03757bc

Please sign in to comment.