'use strict';

var fs = require('fs');
var path = require('path');
var Q = require('Q');
var fsReadFile = Q.denodeify(fs.readFile);
var filewalk = require('./filewalk');

module.exports = function(dir, basename) {
    return filewalk(dir)
        .then(function(files) {

            var filePromises = files.map(function(file) {
                return fsReadFile(path.join(dir, file));
            });

            return Q.all(filePromises)
                .then(function(fileContents) {
                    return files.map(function(filename, i) {
                        return {
                            filename: path.basename(filename, basename),
                            source: fileContents[i].toString()
                        };
                    });
                });

        });
}
