Skip to content

Instantly share code, notes, and snippets.

@lukekarrys
Created March 16, 2015 07:42
Show Gist options
  • Save lukekarrys/a66ca299eaeb3c4fed5d to your computer and use it in GitHub Desktop.
Save lukekarrys/a66ca299eaeb3c4fed5d to your computer and use it in GitHub Desktop.
Write an index file to a directory which a require for each file in the dir
var fs = require('fs');
var path = require('path');
var filename = function (file) {
return path.basename(file, path.extname(file));
};
module.exports = function exportDirectory (dir) {
var dirPath = path.resolve(__dirname, dir);
var contents = fs.readdirSync(dirPath).filter(function (file) {
return filename(file) !== 'index' && fs.statSync(path.join(dirPath, file)).isFile();
});
var exportString = 'module.exports = {\n';
contents.forEach(function (file) {
exportString += ' ' + filename(file) + ': require(\'./' + file + '\'),\n';
});
exportString = exportString.substring(0, exportString.length - 2);
exportString += '\n};';
fs.writeFileSync(path.join(dirPath, 'index.js'), exportString);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment