From 96acb1d78d4234aefb7e9dca59ad3f416054c8a4 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Fri, 24 Aug 2018 12:21:45 +0530 Subject: [PATCH] [util] Added requireDeep --- src/util/requireDeep.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/util/requireDeep.js diff --git a/src/util/requireDeep.js b/src/util/requireDeep.js new file mode 100644 index 0000000..e58d929 --- /dev/null +++ b/src/util/requireDeep.js @@ -0,0 +1,62 @@ +const fs = require(`fs`).promises; +const { + map, + pipe, + filter, + flatten, + wrapWith, + promiseAll, + containsPath, +} = require('../util'); + +// const { warn } = console; + +// const validate = config => { +// if(!config) throw new Error(`Create a config file to proceed.`); +// if(!config.testData) throw new Error(`"testData" property not found in config.js`); +// if(!config.plans) throw new Error(`"planDir" property not found in config.js`); +// if(!config.pattern) warn(`> "pattern" property not found in config.js.` +// + ` All json files in the target directory will be loaded.`); +// }; + +const isFileOrDir = path => fs.stat(path) +.then(stat => + stat.isDirectory() + ? ({ path, type: `dir` }) + : ({ path, type: `file` }) +); + +const _matchOnePattern = (pattern, path) => { + const tests = Array.isArray(pattern) ? pattern : [pattern]; + return tests.some(test => new RegExp(test).test(path)); +}; + +const _getEachFile = ({ path, exclude, pattern }) => item => + isFileOrDir(path + `/` + item) + .then(y => { + if(exclude && containsPath(exclude, y.path)) + return []; + /* eslint-disable-next-line */ + return y.type === `dir` + ? requireDeep({ plans: y.path, exclude, pattern }) + : _matchOnePattern(pattern || '.json', y.path) + // Wrapping in object to prevent accidental flattening + ? wrapWith(`module`)(require(y.path)) + : []; + }); + +const requireDeep = ({ plans: path, exclude, pattern = '.json' }) => + isFileOrDir(path) + .then(o => (o.type === `dir`) + ? pipe( + fs.readdir, + map(_getEachFile({ path, exclude, pattern })), + promiseAll, + flatten, + filter(Boolean), // Filter out undefined values + map(y => y.module || y), // Unwrap objects from their wrappers + )(o.path) + : Promise.reject(`${path} is not a directory`) + ); + +module.exports = requireDeep;