diff --git a/helpers-io.js b/helpers-io.js index 40a5b95..71424ad 100644 --- a/helpers-io.js +++ b/helpers-io.js @@ -39,17 +39,22 @@ function readFiles (directoryPath, options) { } var _options = options || {} // Collect all files - var result = util.asPromise((cb) => glob(_options.glob || '**', {cwd: directoryPath}, cb)) + var result = util.asPromise((cb) => glob(_options.glob || '**', {cwd: directoryPath, mark: true}, cb)) .then(function (relativePaths) { - // Convert to a set based on relative paths (i.e. {'dir/file.txt': 'dir/file.txt'} - var set = relativePaths.reduce((set, relativePath) => { - set[relativePath] = relativePath - return set - }, {}) + var set = relativePaths + // Ignore directories + .filter((relativePath) => !relativePath.match(/\/$/)) + // Convert to a set based on relative paths + // (i.e. {'dir/file.txt': 'dir/file.txt'} + .reduce((set, relativePath) => { + set[relativePath] = relativePath + return set + }, {}) + + // Create lazy promises (only resolve when .then() is called) acting + // as leafs (do not dive inside when merging) return util.mapValues(set, (relativePath) => { var fullPath = path.resolve(directoryPath, relativePath) - // Return a leaf (do not dive inside when merging) and a lazy Promise - // (only resolve when .then() is called) return leaf(lazy(() => { return { path: path.relative(process.cwd(), fullPath), diff --git a/test/files-spec.js b/test/files-spec.js index 24cdee2..63a6e43 100644 --- a/test/files-spec.js +++ b/test/files-spec.js @@ -36,6 +36,23 @@ describe('the files-function', function () { }) }) + it('should not return and read directories', function () { + return files('test/fixtures/recursiveFiles') + .then(deep) + .then(function (result) { + return expect(result).to.deep.equal({ + 'file1.txt': { + 'contents': 'file1\n', + 'path': 'test/fixtures/recursiveFiles/file1.txt' + }, + 'subdir/file2.txt': { + 'contents': 'file2\n', + 'path': 'test/fixtures/recursiveFiles/subdir/file2.txt' + } + }) + }) + }) + it('should create mergeable files', function () { var x = files('test/fixtures/testPartials1') return Promise.all([ diff --git a/test/fixtures/recursiveFiles/file1.txt b/test/fixtures/recursiveFiles/file1.txt new file mode 100644 index 0000000..e212970 --- /dev/null +++ b/test/fixtures/recursiveFiles/file1.txt @@ -0,0 +1 @@ +file1 diff --git a/test/fixtures/recursiveFiles/subdir/file2.txt b/test/fixtures/recursiveFiles/subdir/file2.txt new file mode 100644 index 0000000..6c493ff --- /dev/null +++ b/test/fixtures/recursiveFiles/subdir/file2.txt @@ -0,0 +1 @@ +file2