Last active
October 9, 2021 10:11
-
-
Save fsfarah/49d5445aa01f829388d997a73f5a247d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { readdirSync } = require('fs'); | |
function exportAll() { | |
try { | |
const files = readdirSync(__dirname); | |
for (let i = 0; i < files.length; i++) { | |
const f = files[i]; | |
if (f !== 'index.js') { | |
f.split('.js')[0] | |
module.exports[_camelize(f.split('.js')[0])] = require('./' + f) | |
} | |
} | |
} | |
catch (err) { | |
throw err; | |
} | |
} | |
function _camelize(kebab) { | |
return kebab.replace(/-./g, x => x.toUpperCase()[1]) | |
} | |
exportAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A short hand for including all files in a dir using
index.js
instead of writing
module.exports.helper1 = require('./helper1')
etc...