@@ -4,45 +4,65 @@ import path from 'path'
44import walk from 'walk-sync'
55import yaml from 'js-yaml'
66import { isRegExp , setWith } from 'lodash-es'
7- import filenameToKey from './filename-to-key.js '
7+ import filenameToKey from './filename-to-key'
88import matter from 'gray-matter'
99
10- export default function dataDirectory ( dir , opts = { } ) {
11- const defaultOpts = {
12- preprocess : ( content ) => {
10+ interface DataDirectoryOptions {
11+ preprocess ?: ( content : string ) => string
12+ ignorePatterns ?: RegExp [ ]
13+ extensions ?: string [ ]
14+ }
15+
16+ interface DataDirectoryResult {
17+ [ key : string ] : any
18+ }
19+
20+ export default function dataDirectory (
21+ dir : string ,
22+ opts : DataDirectoryOptions = { } ,
23+ ) : DataDirectoryResult {
24+ const defaultOpts : Required < DataDirectoryOptions > = {
25+ preprocess : ( content : string ) => {
1326 return content
1427 } ,
1528 ignorePatterns : [ / R E A D M E \. m d $ / i] ,
1629 extensions : [ '.json' , '.md' , '.markdown' , '.yml' ] ,
1730 }
1831
19- opts = Object . assign ( { } , defaultOpts , opts )
32+ const mergedOpts = Object . assign ( { } , defaultOpts , opts )
2033
2134 // validate input
22- assert ( Array . isArray ( opts . ignorePatterns ) )
23- assert ( opts . ignorePatterns . every ( isRegExp ) )
24- assert ( Array . isArray ( opts . extensions ) )
25- assert ( opts . extensions . length )
35+ assert ( Array . isArray ( mergedOpts . ignorePatterns ) )
36+ assert ( mergedOpts . ignorePatterns . every ( isRegExp ) )
37+ assert ( Array . isArray ( mergedOpts . extensions ) )
38+ assert ( mergedOpts . extensions . length )
2639
2740 // start with an empty data object
28- const data = { }
41+ const data : DataDirectoryResult = { }
2942
3043 // find YAML and Markdown files in the given directory, recursively
31- const filenames = walk ( dir , { includeBasePath : true } ) . filter ( ( filename ) => {
44+ const filenames = walk ( dir , { includeBasePath : true } ) . filter ( ( filename : string ) => {
3245 // ignore files that match any of ignorePatterns regexes
33- if ( opts . ignorePatterns . some ( ( pattern ) => pattern . test ( filename ) ) ) return false
46+ if ( mergedOpts . ignorePatterns . some ( ( pattern ) => pattern . test ( filename ) ) ) return false
3447
3548 // ignore files that don't have a whitelisted file extension
36- return opts . extensions . includes ( path . extname ( filename ) . toLowerCase ( ) )
49+ return mergedOpts . extensions . includes ( path . extname ( filename ) . toLowerCase ( ) )
3750 } )
3851
39- const files = filenames . map ( ( filename ) => [ filename , fs . readFileSync ( filename , 'utf8' ) ] )
52+ const files : [ string , string ] [ ] = filenames . map ( ( filename : string ) => [
53+ filename ,
54+ fs . readFileSync ( filename , 'utf8' ) ,
55+ ] )
56+
4057 files . forEach ( ( [ filename , fileContent ] ) => {
4158 // derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
4259 const key = filenameToKey ( path . relative ( dir , filename ) )
4360 const extension = path . extname ( filename ) . toLowerCase ( )
4461
45- if ( opts . preprocess ) fileContent = opts . preprocess ( fileContent )
62+ let processedContent = fileContent
63+ if ( mergedOpts . preprocess ) {
64+ processedContent = mergedOpts . preprocess ( fileContent )
65+ }
4666
4767 // Add this file's data to the global data object.
4868 // Note we want to use `setWith` instead of `set` so we can customize the type during path creation.
@@ -51,17 +71,17 @@ export default function dataDirectory(dir, opts = {}) {
5171 // See https://lodash.com/docs#set for an explanation.
5272 switch ( extension ) {
5373 case '.json' :
54- setWith ( data , key , JSON . parse ( fileContent ) , Object )
74+ setWith ( data , key , JSON . parse ( processedContent ) , Object )
5575 break
5676 case '.yml' :
57- setWith ( data , key , yaml . load ( fileContent , { filename } ) , Object )
77+ setWith ( data , key , yaml . load ( processedContent , { filename } ) , Object )
5878 break
5979 case '.md' :
6080 case '.markdown' :
6181 // Use `matter` to drop frontmatter, since localized reusable Markdown files
6282 // can potentially have frontmatter, but we want to prevent the frontmatter
6383 // from being rendered.
64- setWith ( data , key , matter ( fileContent ) . content , Object )
84+ setWith ( data , key , matter ( processedContent ) . content , Object )
6585 break
6686 }
6787 } )
0 commit comments