Created
August 9, 2015 07:47
-
-
Save mmmpa/f2ec45a58be575618152 to your computer and use it in GitHub Desktop.
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
fs = require 'fs' | |
path = require 'path' | |
changeCase = require 'change-case' | |
_ = require 'lodash' | |
module.exports = generateIndexCoffee = (dir)-> | |
container = changeCase.pascalCase(pickDirName(dir)) | |
fs.readdir(dir, (err, files)-> | |
requests = files.map((file)-> | |
full = path.join(dir, file) | |
if fs.statSync(full).isDirectory() | |
generateIndexCoffee(full) | |
fileName = file.split('.').shift() | |
return if fileName == 'index' | |
klass = changeCase.pascalCase(fileName) | |
name = [container, klass].join('.') | |
"#{name} = require './#{fileName}'" | |
) | |
requests.unshift("module.exports = #{container} = {}") | |
requests.push("# Generated by IndexCoffeeGenerator") | |
console.log 'generate', indexCoffee = path.join(dir, 'index.coffee') | |
fs.writeFileSync(indexCoffee, _.compact(requests).join("\n")); | |
) | |
pickDirName = (dir)-> | |
dirs = dir.split('/') | |
if (last = _.last(dirs)) != '' | |
last | |
else | |
dirs[dirs.length - 2] | |
if require.main == module | |
dir = process.argv[2] | |
generateIndexCoffee(dir) if dir? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment