You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.6 KiB
JavaScript

'use strict';
const Promise = require("bluebird");
const fs = Promise.promisifyAll(require("fs"));
const path = require("path");
const rfr = require("rfr");
const directorySorter = rfr("lib/sorting/directory")();
function statComplete(target) {
return Promise.try(() => {
return Promise.try(() => {
return fs.lstatAsync(target);
}).then((stats) => {
if (stats.isSymbolicLink()) {
return Promise.try(() => {
return fs.statAsync(target);
}).then((targetStats) => {
return {
stats: targetStats,
linkStats: stats
}
});
} else {
return {
stats: stats
}
}
});
})
}
function getType(stats) {
if (stats.isSymbolicLink()) {
return "link";
} else if (stats.isDirectory()) {
return "folder";
} else if (stats.isFile()) {
return "file";
} else {
return "other";
}
}
module.exports = function(basePath, options = {}) {
return Promise.try(() => {
return fs.readdirAsync(basePath);
}).then((entries) => {
if (options.includeParent) {
entries.unshift("..");
}
return entries;
}).map((entry) => {
let fullPath = path.join(basePath, entry);
return Promise.try(() => {
return statComplete(fullPath);
}).then((stats) => {
let type, targetType;
if (stats.linkStats != null) {
type = "link";
targetType = getType(stats.stats);
} else {
type = getType(stats.stats);
targetType = type;
}
return {
type: type,
targetType: targetType,
name: entry,
path: fullPath,
modified: stats.stats.mtime,
size: stats.stats.size
}
})
}).then((entries) => {
return entries.sort(directorySorter);
});
}