@@ -2,10 +2,13 @@ const assert = require('assert')
22const fs = require ( 'fs' ) . promises
33const path = require ( 'path' )
44const walk = require ( 'walk-sync' )
5+ const { mapLimit } = require ( 'async' )
56const yaml = require ( 'js-yaml' )
67const { isRegExp, set } = require ( 'lodash' )
78const filenameToKey = require ( './filename-to-key' )
89
10+ const FILE_READ_LIMIT = 100
11+
912module . exports = async function dataDirectory ( dir , opts = { } ) {
1013 const defaultOpts = {
1114 preprocess : ( content ) => { return content } ,
@@ -31,15 +34,18 @@ module.exports = async function dataDirectory (dir, opts = {}) {
3134 const data = { }
3235
3336 // find YAML and Markdown files in the given directory, recursively
34- await Promise . all ( walk ( dir , { includeBasePath : true } )
37+ const filenames = walk ( dir , { includeBasePath : true } )
3538 . filter ( filename => {
3639 // ignore files that match any of ignorePatterns regexes
3740 if ( opts . ignorePatterns . some ( pattern => pattern . test ( filename ) ) ) return false
3841
3942 // ignore files that don't have a whitelisted file extension
4043 return opts . extensions . includes ( path . extname ( filename ) . toLowerCase ( ) )
4144 } )
42- . map ( async filename => {
45+ await mapLimit (
46+ filenames ,
47+ FILE_READ_LIMIT ,
48+ async filename => {
4349 // derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
4450 const key = filenameToKey ( path . relative ( dir , filename ) )
4551 const extension = path . extname ( filename ) . toLowerCase ( )
@@ -62,7 +68,8 @@ module.exports = async function dataDirectory (dir, opts = {}) {
6268 set ( data , key , fileContent )
6369 break
6470 }
65- } ) )
71+ }
72+ )
6673
6774 return data
6875}
0 commit comments