datoit
Version:
An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MongoDB, MySQL, MariaDB, PostgreSQL, SQLite
28 lines (21 loc) • 657 B
JavaScript
;
const assert = require('assert');
const path = require('path');
const fse = require('fs-extra');
module.exports = function walk(dir, { loader } = {}) {
assert(typeof loader === 'function', 'opts.loader must be a function');
const root = {};
const paths = fse.readdirSync(dir, { withFileTypes: true });
for (let fd of paths) {
const { name } = fd;
const fullPath = dir + path.sep + name;
if (fd.isDirectory()) {
root[name] = walk(fullPath, { loader });
} else {
const ext = path.extname(name);
const key = path.basename(name, ext);
root[key] = loader(fullPath);
}
}
return root;
};