🔧 Enhance tree.ts script with configurable options and update metadata

This commit is contained in:
2026-04-11 09:29:47 +08:00
parent 511540f966
commit ba6019ad3b
2 changed files with 34 additions and 16 deletions

View File

@@ -19,10 +19,15 @@ const defaultSkipDirs = [
"node_modules",
];
interface ListDirOptions {
maxDepth: number;
hideMoreDirs: boolean;
}
async function listDir(
dir: string,
depth: number,
maxDepth: number,
options: ListDirOptions,
): Promise<void> {
const tab = " ".repeat(depth * 4);
try {
@@ -32,14 +37,20 @@ async function listDir(
}
const fullName = joinPath(dir, dirEntry.name);
if (dirEntry.isDirectory) {
const showNextDepth = (depth + 1) <= maxDepth;
console.log(
`${tab}- [${dirEntry.name}]${
showNextDepth ? "" : term.auto("[blue][[[ \t[...more dirs...]]]][/]")
}`,
);
const showNextDepth = (depth + 1) <= options.maxDepth;
if (!options.hideMoreDirs) {
console.log(
`${tab}- [${dirEntry.name}]${
showNextDepth
? ""
: term.auto(
"[blue][[[ \t[...more dirs...]]]][/]",
)
}`,
);
}
if (showNextDepth) {
await listDir(fullName, depth + 1, maxDepth);
await listDir(fullName, depth + 1, options.maxDepth);
}
} else {
let fileDesc = "";
@@ -58,7 +69,9 @@ async function listDir(
}
}
console.log(
`${tab}- ${term.auto("[green][[["+dirEntry.name+"]]][/]")} \t${fileDesc}`,
`${tab}- ${
term.auto("[green][[[" + dirEntry.name + "]]][/]")
} \t${fileDesc}`,
);
}
}
@@ -69,7 +82,7 @@ async function listDir(
async function main(): Promise<void> {
const flags = parseArgs(Deno.args, {
boolean: ["help"],
boolean: ["help", "hide-more-dirs"],
number: ["depth"],
});
if (flags.help) {
@@ -77,18 +90,23 @@ async function main(): Promise<void> {
tree.ts [parameters] <dir>
--depth 3 - Directory depth
<dir> - default '.' current dir`);
--depth 3 - Directory depth
--hide-more-dirs - Hide more dirs
<dir> - default '.' current dir`);
return;
}
const maxDepth = parseIntVal(flags.depth, 10);
const hideMoreDirs = flags["hide-more-dirs"];
let baseDir = ".";
if (flags._.length > 0) {
baseDir = flags._[0];
}
await listDir(baseDir, 0, maxDepth);
await listDir(baseDir, 0, {
maxDepth,
hideMoreDirs,
});
}
main().catch((e) => log.error(e));