Skip to content

Instantly share code, notes, and snippets.

@fsfarah
Last active October 9, 2021 10:11
Show Gist options
  • Save fsfarah/49d5445aa01f829388d997a73f5a247d to your computer and use it in GitHub Desktop.
Save fsfarah/49d5445aa01f829388d997a73f5a247d to your computer and use it in GitHub Desktop.
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();
@fsfarah
Copy link
Author

fsfarah commented Oct 9, 2021

A short hand for including all files in a dir using index.js
instead of writing module.exports.helper1 = require('./helper1') etc...

helpers/
    /index.js
    /helper1.js
    /helper2.js
    //etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment