UNPKG

@trusthab/composable-resources

Version:

migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable

34 lines (25 loc) 920 B
const fs = require('fs'); const path = require('path'); const { underscore, camelize } = require('inflected'); const resources = {}; const src = path.join(path.resolve(__dirname), 'resources'); module.exports = function populateResources(prefix) { let modSrc = src; if (prefix) { modSrc = path.join(src, prefix); } fs.readdirSync(modSrc).forEach(file => { let moduleName = path.basename(file, '.js'); const modPath = path.join(modSrc, file); const type = !!file.match('.js') ? 'file' : 'dir'; if (type === 'dir') { return populateResources(file); } let module_path = modPath.replace(`${src}/`, '').replace(`${src}\\`, ''); module_path = camelize(module_path ); module_path = module_path.replace(/\.js|\.json/g, ''); module_path = module_path.replace(/\//g, '.').replace(/\\/g, '.'); resources[module_path] = require(modPath); }); return resources; }