Added recursiveness capabilities. Closes #1.
Indexing now relies on recursive-readdir, which is also the library used by Metalsmith.
This commit is contained in:
parent
f3a94af636
commit
8151ed90cf
@ -87,4 +87,7 @@ is metadata that is added to the created index file.
|
||||
alphabetic order is used. The function is used to sort an array of
|
||||
strings and must behave like a [compareFunction][].
|
||||
|
||||
- `recursive` is false by default: the plugin ignores sub-directories.
|
||||
When set to true, they are indexed recursively.
|
||||
|
||||
[compareFunction]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
||||
|
14
lib/index.js
14
lib/index.js
@ -1,5 +1,5 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const readdir = require("recursive-readdir")
|
||||
|
||||
/**
|
||||
* Expose `plugin`.
|
||||
@ -56,17 +56,25 @@ function plugin (indexes) {
|
||||
|
||||
Object.keys(indexes).forEach(function (dirname) {
|
||||
var root = metalsmith.path(path.join(metalsmith.source(), dirname))
|
||||
var ignored = []
|
||||
|
||||
indexes[dirname].filter = indexes[dirname].filter || defaultFilter
|
||||
indexes[dirname].format = indexes[dirname].format || defaultFormat
|
||||
indexes[dirname].compare = indexes[dirname].sort || defaultCompare
|
||||
|
||||
fs.readdir(root, (err, children) => {
|
||||
if (indexes[dirname].recursive != true) {
|
||||
ignored.push((file, stats) => {
|
||||
return stats.isDirectory()
|
||||
})
|
||||
}
|
||||
|
||||
readdir(root, ignored, function (err, children) {
|
||||
setImmediate(done)
|
||||
if (err) {
|
||||
console.log(root + ': ' + err.code)
|
||||
} else {
|
||||
createIndex(files, dirname, children)
|
||||
createIndex(files, dirname, children.map(
|
||||
f => path.relative(root, f)))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
53
package-lock.json
generated
53
package-lock.json
generated
@ -49,14 +49,12 @@
|
||||
"balanced-match": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
|
||||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
|
||||
"dev": true
|
||||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
|
||||
},
|
||||
"brace-expansion": {
|
||||
"version": "1.1.8",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz",
|
||||
"integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=",
|
||||
"dev": true,
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"requires": {
|
||||
"balanced-match": "1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
@ -126,16 +124,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"commander": {
|
||||
"version": "2.14.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-2.14.0.tgz",
|
||||
"integrity": "sha512-okPpdvdJr6mUGi2XzupC+irQxzwGLVaBzacFC14hjLv8NColXEsxsU+QaeuSSXpQUak5g2K0vQ7WjA1e8svczg==",
|
||||
"version": "2.14.1",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-2.14.1.tgz",
|
||||
"integrity": "sha512-+YR16o3rK53SmWHU3rEM3tPAh2rwb1yPcQX5irVn7mb0gXbwuCCrnkbV5+PBfETdfg1vui07nM6PCG1zndcjQw==",
|
||||
"dev": true
|
||||
},
|
||||
"concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
|
||||
"dev": true
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
|
||||
},
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
@ -216,6 +213,17 @@
|
||||
"minimatch": "3.0.4",
|
||||
"once": "1.4.0",
|
||||
"path-is-absolute": "1.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"minimatch": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
|
||||
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"brace-expansion": "1.1.11"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"graceful-fs": {
|
||||
@ -342,7 +350,7 @@
|
||||
"chalk": "1.1.3",
|
||||
"clone": "1.0.3",
|
||||
"co-fs-extra": "1.2.1",
|
||||
"commander": "2.14.0",
|
||||
"commander": "2.14.1",
|
||||
"gray-matter": "2.1.1",
|
||||
"has-generators": "1.0.1",
|
||||
"is": "3.2.1",
|
||||
@ -357,12 +365,11 @@
|
||||
}
|
||||
},
|
||||
"minimatch": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
|
||||
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
|
||||
"dev": true,
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.3.tgz",
|
||||
"integrity": "sha1-Kk5AkLlrLbBqnX3wEFWmKnfJt3Q=",
|
||||
"requires": {
|
||||
"brace-expansion": "1.1.8"
|
||||
"brace-expansion": "1.1.11"
|
||||
}
|
||||
},
|
||||
"minimist": {
|
||||
@ -440,20 +447,8 @@
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/recursive-readdir/-/recursive-readdir-2.2.1.tgz",
|
||||
"integrity": "sha1-kO8jHQd4xc4JPJpI105cVCLROpk=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"minimatch": "3.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"minimatch": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.3.tgz",
|
||||
"integrity": "sha1-Kk5AkLlrLbBqnX3wEFWmKnfJt3Q=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"brace-expansion": "1.1.8"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"rimraf": {
|
||||
|
@ -21,5 +21,8 @@
|
||||
"eol": "^0.9.1",
|
||||
"metalsmith": "^2.3.0",
|
||||
"mocha": "^4.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"recursive-readdir": "^2.2.1"
|
||||
}
|
||||
}
|
||||
|
0
test/fixtures/src/recursivity/level_1.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_1.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2/level_2a.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2/level_2a.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2/level_2b.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2/level_2b.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2/level_3/level_3.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2/level_3/level_3.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2bis/level_2c.txt
vendored
Normal file
0
test/fixtures/src/recursivity/level_2bis/level_2c.txt
vendored
Normal file
@ -2,6 +2,8 @@ const assert = require('assert')
|
||||
const eol = require('eol')
|
||||
const fs = require('fs')
|
||||
const metalsmith = require('metalsmith')
|
||||
const path = require('path')
|
||||
const readdir = require("recursive-readdir")
|
||||
const index = require('..')
|
||||
|
||||
|
||||
@ -146,5 +148,37 @@ describe('metalsmith-index', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should ignore subdirectories by default', function(done) {
|
||||
testIndex({
|
||||
'recursivity': {}
|
||||
}, function(err, files) {
|
||||
if (err) return done(err)
|
||||
assert(files['recursivity/index.list'], 'recursivity files were not indexed')
|
||||
readdir('test/fixtures/src/recursivity', [(filename, stats) => {
|
||||
return stats.isDirectory()
|
||||
}], function (err, readFiles) {
|
||||
assert(areArraysMatching(
|
||||
eol.split(files['recursivity/index.list'].contents.toString('utf-8')),
|
||||
readFiles.map(f => path.relative('test/fixtures/src/recursivity', f))))
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should index in depth when asked to', function(done) {
|
||||
testIndex({
|
||||
'recursivity': { recursive: true }
|
||||
}, function(err, files) {
|
||||
if (err) return done(err)
|
||||
assert(files['recursivity/index.list'], 'recursivity files were not indexed')
|
||||
readdir('test/fixtures/src/recursivity', [], function (err, readFiles) {
|
||||
assert(areArraysMatching(
|
||||
eol.split(files['recursivity/index.list'].contents.toString('utf-8')),
|
||||
readFiles.map(f => path.relative('test/fixtures/src/recursivity', f))))
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user