🔧 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
+3 -3
View File
@@ -322,12 +322,12 @@
}, },
"tree.ts": { "tree.ts": {
"script_name": "tree.ts", "script_name": "tree.ts",
"script_length": 2839, "script_length": 3356,
"script_sha256": "0dacf96ab36a38f4d82ec357e5dde466f0dffd734fd843057aa0fd51af532c93", "script_sha256": "3ff71e47f3fde268f470a37b750a8c84842e46c4d4a23c1777786da0ff6e53d8",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/tree.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/tree.ts",
"single_script_file": true, "single_script_file": true,
"publish_time": 1769361581018, "publish_time": 1769361581018,
"update_time": 1775868903980 "update_time": 1775870977279
}, },
"trim.ts": { "trim.ts": {
"script_name": "trim.ts", "script_name": "trim.ts",
+31 -13
View File
@@ -19,10 +19,15 @@ const defaultSkipDirs = [
"node_modules", "node_modules",
]; ];
interface ListDirOptions {
maxDepth: number;
hideMoreDirs: boolean;
}
async function listDir( async function listDir(
dir: string, dir: string,
depth: number, depth: number,
maxDepth: number, options: ListDirOptions,
): Promise<void> { ): Promise<void> {
const tab = " ".repeat(depth * 4); const tab = " ".repeat(depth * 4);
try { try {
@@ -32,14 +37,20 @@ async function listDir(
} }
const fullName = joinPath(dir, dirEntry.name); const fullName = joinPath(dir, dirEntry.name);
if (dirEntry.isDirectory) { if (dirEntry.isDirectory) {
const showNextDepth = (depth + 1) <= maxDepth; const showNextDepth = (depth + 1) <= options.maxDepth;
console.log( if (!options.hideMoreDirs) {
`${tab}- [${dirEntry.name}]${ console.log(
showNextDepth ? "" : term.auto("[blue][[[ \t[...more dirs...]]]][/]") `${tab}- [${dirEntry.name}]${
}`, showNextDepth
); ? ""
: term.auto(
"[blue][[[ \t[...more dirs...]]]][/]",
)
}`,
);
}
if (showNextDepth) { if (showNextDepth) {
await listDir(fullName, depth + 1, maxDepth); await listDir(fullName, depth + 1, options.maxDepth);
} }
} else { } else {
let fileDesc = ""; let fileDesc = "";
@@ -58,7 +69,9 @@ async function listDir(
} }
} }
console.log( 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> { async function main(): Promise<void> {
const flags = parseArgs(Deno.args, { const flags = parseArgs(Deno.args, {
boolean: ["help"], boolean: ["help", "hide-more-dirs"],
number: ["depth"], number: ["depth"],
}); });
if (flags.help) { if (flags.help) {
@@ -77,18 +90,23 @@ async function main(): Promise<void> {
tree.ts [parameters] <dir> tree.ts [parameters] <dir>
--depth 3 - Directory depth --depth 3 - Directory depth
<dir> - default '.' current dir`); --hide-more-dirs - Hide more dirs
<dir> - default '.' current dir`);
return; return;
} }
const maxDepth = parseIntVal(flags.depth, 10); const maxDepth = parseIntVal(flags.depth, 10);
const hideMoreDirs = flags["hide-more-dirs"];
let baseDir = "."; let baseDir = ".";
if (flags._.length > 0) { if (flags._.length > 0) {
baseDir = flags._[0]; baseDir = flags._[0];
} }
await listDir(baseDir, 0, maxDepth); await listDir(baseDir, 0, {
maxDepth,
hideMoreDirs,
});
} }
main().catch((e) => log.error(e)); main().catch((e) => log.error(e));