onDirRead(null, list) }else{ fs.readdir(bufdir, {encoding: 'buffer'}, onDirRead) } } if(options.sync){ const stat = fs.statSync(bufdir); return onStat(null, stat) }else{ fs.stat(bufdir, onStat); } }; /** * find all files and subdirs in a directory (recursive) and pass them to callback fn * * @param {string} dir directory in which to recurse files or subdirs * @param {boolean} combine whether to combine both subdirs and filepaths into one array (default false) * @param {function(error, Object.<, Array.>)} callback fn to call when done * @example * dir.paths(__dirname, function (err, paths) { * if (err) throw err; * console.log('files:', paths.files); * console.log('subdirs:', paths.dirs); * }); * dir.paths(__dirname, true, function (err, paths) { * if (err) throw err; * console.log('paths:', paths); * }); */ exports.paths = function paths(dir, combine, callback) { var type; if (typeof combine === 'function') { callback = combine; combine = false; } exports.files(dir, 'all', function(err, results) { if (err) return callback(err); if (combine) { callback(null, results.files.concat(results.dirs)); } else { callback(null, results); } }); }; /** * find all subdirs (recursive) of a directory and pass them to callback fn * * @param {string} dir directory in which to find subdirs * @param {string} type type of dir entry to recurse ('file' or 'dir', defaults to 'file') * @param {function(error, )} callback fn to call when done * @example * dir.subdirs(__dirname, function (err, paths) { * if (err) throw err; * console.log('files:', paths.files); * console.log('subdirs:', paths.dirs); * }); */ exports.subdirs = function subdirs(dir, callback, type, options) { options = options || {} const iCallback = function(err, subdirs) { if (err) return callback(err); if(type=='combine'){ subdirs = subdirs.files.concat(subdirs.dirs) } if(options.sync)return subdirs callback(null, subdirs); } const res = exports.files(dir, 'dir', iCallback, options) if(options && options.sync){ return iCallback(null,res) } };