🔧 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

@@ -322,12 +322,12 @@
},
"tree.ts": {
"script_name": "tree.ts",
"script_length": 2839,
"script_sha256": "0dacf96ab36a38f4d82ec357e5dde466f0dffd734fd843057aa0fd51af532c93",
"script_length": 3356,
"script_sha256": "3ff71e47f3fde268f470a37b750a8c84842e46c4d4a23c1777786da0ff6e53d8",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/tree.ts",
"single_script_file": true,
"publish_time": 1769361581018,
"update_time": 1775868903980
"update_time": 1775870977279
},
"trim.ts": {
"script_name": "trim.ts",

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));