Skip to content

Commit

Permalink
Fix files-helpers for directories with subdirectories
Browse files Browse the repository at this point in the history
- When using "glob", the filter for "files-only" was accidentally
  omitted
- This commit adds a test to check this, because this only appeared
  when running the test of 'customize-engine-handlebars'
  • Loading branch information
nknapp committed Apr 8, 2017
1 parent 195a34c commit 0eea4eb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
21 changes: 13 additions & 8 deletions helpers-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 17 additions & 0 deletions test/files-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/recursiveFiles/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file1
1 change: 1 addition & 0 deletions test/fixtures/recursiveFiles/subdir/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file2

0 comments on commit 0eea4eb

Please sign in to comment.