Created
May 23, 2021 15:13
-
-
Save fal-works/c1bfe2f1ccd593645bc58275353a415f to your computer and use it in GitHub Desktop.
Print file tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haxe.io.Path; | |
import sys.FileSystem; | |
function main() | |
printFileTree(getFileTree("cwd", "relPath")); // change arguments | |
typedef FileOrDir = {name:String, children:Array<FileOrDir>}; | |
function getFileTree(cwd:String, relPath:String):FileOrDir { | |
final path = Path.join([cwd, relPath]); | |
final children = if (!FileSystem.isDirectory(path)) [] else { | |
FileSystem.readDirectory(path).map(getFileTree.bind(path)); | |
}; | |
return {name: relPath, children: children}; | |
} | |
function printFileTree(fileOrDir:FileOrDir, depth:Int = 0) { | |
final indent = [for (_ in 0...depth) "\t"].join(""); | |
Sys.println('${indent}${fileOrDir.name}'); | |
for (child in fileOrDir.children) printFileTree(child, depth + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment