/**
 * Read a directory recursive synchronically.
 * @function lib.file.readdirRecursiveSync
 * @param {string} dirname - Directory name to read.
 * @returns {function} filenames - Filenames relative to the directory.
 * @author Taka Okunishi
 *
 */
var fs = require('fs'),
    path = require('path');

exports = module.exports = function (dirname) {
    return fs.readdirSync(dirname)
        .map(function (filename) {
            return path.resolve(dirname, filename);
        })
        .filter(function (filename) {
            return fs.existsSync(filename);
        })
        .map(function (filename) {
            var isDir = fs.statSync(filename).isDirectory();
            if (isDir) {
                var subDirname = filename;
                return exports(subDirname).map(function (filename) {
                    return path.relative(dirname, path.resolve(subDirname, filename));
                });
            } else {
                return path.relative(dirname, filename);
            }
        })
        .reduce(function (a, b) {
            return a.concat(b);
        }, []);
};