diff --git a/server/lib/getFolders.js b/server/lib/getFolders.js index 5e2d08e6..bfdb823b 100644 --- a/server/lib/getFolders.js +++ b/server/lib/getFolders.js @@ -1,35 +1,10 @@ -/* eslint-disable promise/no-nesting */ const path = require('path') const fs = require('fs') const { promisify } = require('util') const readdir = promisify(fs.readdir) -const stat = promisify(fs.stat) -// based on http://stackoverflow.com/a/38314404/2533525 -const getFolders = function (dir, filterFn) { - // default filter function accepts all folders - filterFn = filterFn || function () { return true } - - return readdir(dir).then(list => { - return Promise.all(list.map(file => { - file = path.resolve(dir, file) - return stat(file).then(stat => { - if (stat.isDirectory()) { - return filterFn(file) ? file : '' - } - }).catch(err => { - // stat failed for this folder - console.error(err.message) - }) - })).then(results => { - return results.filter(f => { - return !!f - }) - }) - }).then(results => { - // flatten the array of arrays - return Array.prototype.concat.apply([], results) - }) -} +const getFolders = dir => readdir(dir, { withFileTypes: true }) + .then(list => Promise.all(list.map(ent => ent.isDirectory() ? path.resolve(dir, ent.name) : null))) + .then(list => list.filter(f => !!f).sort()) module.exports = getFolders