all files / file-manager-js/lib/ listDeep.js

100% Statements 17/17
100% Branches 4/4
100% Functions 0/0
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                      
const list = require('./list');
 
// lists in-depth files and directories inside a directory
const listDeep = (fsReaddir, fsStat, join) => {
  const listFn = list(fsReaddir, fsStat, join);
 
  return (path) => {
    const entries = { files: [], dirs: [] };
    const listDeepFn = p => listFn(p)
      .then((currEntries) => {
        if (currEntries.files.length > 0) {
          entries.files = entries.files.concat(currEntries.files);
        }
        if (currEntries.dirs.length > 0) {
          entries.dirs = entries.dirs.concat(currEntries.dirs);
          const listing = currEntries.dirs.map(listDeepFn);
          return Promise.all(listing).then(() => entries);
        }
        return entries;
      });
    return listDeepFn(path);
  };
};
 
module.exports = listDeep;