read-dir-and-stat
Version:
Reads the directory files and adds the stat info.
66 lines (60 loc) • 1.61 kB
JavaScript
;
var fs = require("fs"),
path = require("path"),
sameTime = require("same-time"),
bindy = require("bindy");
/**
* readDirAndStat
* Reads the files/directories and adds the stat information.
*
* @name readDirAndStat
* @function
* @param {String} inputPath The directory path.
* @param {Function} cb The callback function.
*/
function readDirAndStat(inputPath, cb) {
if (typeof inputPath === "function") {
cb = inputPath;
inputPath = process.cwd();
}
fs.readdir(inputPath, function (err, res) {
if (err) {
return cb(err);
}
sameTime(bindy(res, function (c, done) {
var fullPath = path.join(inputPath, c);
fs.stat(fullPath, function (err, stat) {
if (err) {
return done(err);
}
done(null, {
path: fullPath,
stat: stat
});
});
}), cb);
});
}
/**
* sync
* The sync version.
*
* @name sync
* @function
* @param {String} inputPath The directory path.
* @returns {Array} An array of objects containing:
*
* - `path` (String): The full file/directory path
* - `stat` (Object): The stat result.
*/
readDirAndStat.sync = function (inputPath) {
inputPath = inputPath || process.cwd();
return fs.readdirSync(inputPath).map(function (c) {
var fullPath = path.join(inputPath, c);
return {
path: fullPath,
stat: fs.statSync(fullPath)
};
});
};
module.exports = readDirAndStat;