Moved `fileTreeSort()` to `utils`

This commit is contained in:
emizzle 2018-10-10 15:00:40 +11:00 committed by Pascal Precht
parent 35a701fb90
commit 72a796c11b
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 23 additions and 23 deletions

View File

@ -74,26 +74,6 @@ class Pipeline {
(req, res) => { (req, res) => {
const rootPath = fs.dappPath(); const rootPath = fs.dappPath();
function isFolder(object) {
return object.children && object.children.length;
}
function isNotFolder(object){
return !isFolder(object);
}
function byName(a, b) {
return a.name.localeCompare(b.name);
}
function fileTreeSort(objects){
const folders = objects.filter(isFolder).sort(byName);
const files = objects.filter(isNotFolder).sort(byName);
return folders.concat(files);
}
const walk = (dir, filelist = []) => fs.readdirSync(dir).map(name => { const walk = (dir, filelist = []) => fs.readdirSync(dir).map(name => {
let isRoot = rootPath === dir; let isRoot = rootPath === dir;
if (fs.statSync(path.join(dir, name)).isDirectory()) { if (fs.statSync(path.join(dir, name)).isDirectory()) {
@ -103,7 +83,7 @@ class Pipeline {
dirname: dir, dirname: dir,
path: path.join(dir, name), path: path.join(dir, name),
isHidden: name.indexOf('.') === 0, isHidden: name.indexOf('.') === 0,
children: fileTreeSort(walk(path.join(dir, name), filelist))}; children: utils.fileTreeSort(walk(path.join(dir, name), filelist))};
} }
return { return {
name, name,
@ -113,7 +93,7 @@ class Pipeline {
isHidden: name.indexOf('.') === 0 isHidden: name.indexOf('.') === 0
}; };
}); });
const files = fileTreeSort(walk(fs.dappPath())); const files = utils.fileTreeSort(walk(fs.dappPath()));
res.send(files); res.send(files);
} }
); );

View File

@ -494,6 +494,25 @@ function timer(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
function isFolder(node) {
return node.children && node.children.length;
}
function isNotFolder(node){
return !isFolder(node);
}
function byName(a, b) {
return a.name.localeCompare(b.name);
}
function fileTreeSort(nodes){
const folders = nodes.filter(isFolder).sort(byName);
const files = nodes.filter(isNotFolder).sort(byName);
return folders.concat(files);
}
module.exports = { module.exports = {
joinPath, joinPath,
dirname, dirname,
@ -537,5 +556,6 @@ module.exports = {
last, last,
interceptLogs, interceptLogs,
errorMessage, errorMessage,
timer timer,
fileTreeSort
}; };