Trying to use Folder.getFiles() as the basis for a tree menu, but I've confused myself
Hi guys. I want to make a file tree menu GUI for my script, and I found window.cep.fs.readDir strange in that it returns undefined for directories, so I decided to do this through JSX:
recurseChildren(rootFolder)
function recurseChildren(path) {
var subFolders = getChildren(path, 'Folders');
var subFiles = getChildren(path, 'Files');
var children = [];
if (subFolders) {
for (var i = 0; i < subFolders.length; i++) {
children.push(subFolders)
children.push(recurseChildren(subFolders));
}
}
if (subFiles) {
for (var i = 0; i < subFiles.length; i++) {
children.push(subFiles);
}
}
if (!children.length) {
return false;
} else {
return children;
}
}
function getChildren(path, type){
var thisFolder = Folder(path);
var children = thisFolder.getFiles();
var subs = [];
if (!children.length) {
return false;
} else {
for (var i = 0; i < children.length; i++) {
switch (type) {
case 'Folders':
if ((/\/(\w|\-|\d)*$/g.test(children)) && !(/LICENSE/.test(children)))
subs.push(children);
break;
case 'Files':
if ((/\/(\w|\d|\-)*\.(\w)*$/g.test(children)) || (/LICENSE/.test(children)))
subs.push(children);
break;
default:
subs.push(children);
break;
}
}
return subs;
}
}
So I have access to all the files in all the folders of a single root directory, including ones that are excluded from getFiles() typically (like '.git', 'LICENSE', '.debug' and more), and my plan was to use this as a basis for constructing a tree view menu itself, but now I'm confused as to the best way to organize the data. I tried appending each new directory as a property of an object with each filename as another property within that, but found that traversing a 'nesting level' within an object without knowing it's name or what nesting level it is (to know what the parent would be) was really obfuscatory and pretty hard to do. Initially I thought I should use arrays within arrays, but then I tied myself up in a lot of corners trying to go that route too, because I need to know the name of the subfolder, the subfolder's parent, and the subfolder's contents.
How would you best organize this, or rewrite the above to give a better result? I feel like dropping the file reading and returning only directories, then using window.cep.readFile on all the directories contents back in vanilla JS would be far easier than anything I'm doing here, am I wrong? Are there better ways to do this?
