2020-12-09 22:20:24 +03:00
|
|
|
const assert = require('assert')
|
|
|
|
const fs = require('fs').promises
|
|
|
|
const path = require('path')
|
|
|
|
const walk = require('walk-sync')
|
2020-12-14 18:56:05 +03:00
|
|
|
const { mapLimit } = require('async')
|
2020-12-09 22:20:24 +03:00
|
|
|
const yaml = require('js-yaml')
|
|
|
|
const { isRegExp, set } = require('lodash')
|
|
|
|
const filenameToKey = require('./filename-to-key')
|
|
|
|
|
2020-12-14 18:56:05 +03:00
|
|
|
const FILE_READ_LIMIT = 100
|
|
|
|
|
2020-12-09 22:20:24 +03:00
|
|
|
module.exports = async function dataDirectory (dir, opts = {}) {
|
|
|
|
const defaultOpts = {
|
|
|
|
preprocess: (content) => { return content },
|
|
|
|
ignorePatterns: [/README\.md$/i],
|
|
|
|
extensions: [
|
|
|
|
'.json',
|
|
|
|
'.md',
|
|
|
|
'.markdown',
|
|
|
|
'.yaml',
|
|
|
|
'.yml'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = Object.assign({}, defaultOpts, opts)
|
|
|
|
|
|
|
|
// validate input
|
|
|
|
assert(Array.isArray(opts.ignorePatterns))
|
|
|
|
assert(opts.ignorePatterns.every(isRegExp))
|
|
|
|
assert(Array.isArray(opts.extensions))
|
|
|
|
assert(opts.extensions.length)
|
|
|
|
|
|
|
|
// start with an empty data object
|
|
|
|
const data = {}
|
|
|
|
|
|
|
|
// find YAML and Markdown files in the given directory, recursively
|
2020-12-14 18:56:05 +03:00
|
|
|
const filenames = walk(dir, { includeBasePath: true })
|
2020-12-09 22:20:24 +03:00
|
|
|
.filter(filename => {
|
|
|
|
// ignore files that match any of ignorePatterns regexes
|
|
|
|
if (opts.ignorePatterns.some(pattern => pattern.test(filename))) return false
|
|
|
|
|
|
|
|
// ignore files that don't have a whitelisted file extension
|
|
|
|
return opts.extensions.includes(path.extname(filename).toLowerCase())
|
|
|
|
})
|
2020-12-15 21:56:25 +03:00
|
|
|
const files = await mapLimit(
|
2020-12-14 18:56:05 +03:00
|
|
|
filenames,
|
|
|
|
FILE_READ_LIMIT,
|
2020-12-15 21:56:25 +03:00
|
|
|
async filename => [filename, await fs.readFile(filename, 'utf8')]
|
|
|
|
)
|
|
|
|
files.forEach(([filename, fileContent]) => {
|
|
|
|
// derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
|
|
|
|
const key = filenameToKey(path.relative(dir, filename))
|
|
|
|
const extension = path.extname(filename).toLowerCase()
|
2020-12-09 22:20:24 +03:00
|
|
|
|
2020-12-15 21:56:25 +03:00
|
|
|
if (opts.preprocess) fileContent = opts.preprocess(fileContent)
|
2020-12-09 22:20:24 +03:00
|
|
|
|
2020-12-15 21:56:25 +03:00
|
|
|
// add this file's data to the global data object
|
|
|
|
switch (extension) {
|
|
|
|
case '.json':
|
|
|
|
set(data, key, JSON.parse(fileContent))
|
|
|
|
break
|
|
|
|
case '.yaml':
|
|
|
|
case '.yml':
|
|
|
|
set(data, key, yaml.safeLoad(fileContent, { filename }))
|
|
|
|
break
|
|
|
|
case '.md':
|
|
|
|
case '.markdown':
|
|
|
|
set(data, key, fileContent)
|
|
|
|
break
|
2020-12-14 18:56:05 +03:00
|
|
|
}
|
2020-12-15 21:56:25 +03:00
|
|
|
}
|
2020-12-14 18:56:05 +03:00
|
|
|
)
|
2020-12-09 22:20:24 +03:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|