Created
March 16, 2015 07:42
-
-
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
This file contains 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
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