Files
ts-scripts/single-scripts/tree.ts

116 lines
3.3 KiB
TypeScript
Executable File

#!/usr/bin/env runts -- --allow-all
import {
formatSize2,
joinPath,
log,
parseIntVal,
term,
} from "https://global.hatter.ink/script/get/@70/deno-commons-mod.ts";
import {parseArgs} from "jsr:@std/cli/parse-args";
const defaultSkipDirs = [
".git",
".idea",
".gradle",
"target",
".DS_Store",
".localized",
"node_modules",
];
interface ListDirOptions {
maxDepth: number;
hideMoreDirs: boolean;
}
async function listDir(
dir: string,
depth: number,
options: ListDirOptions,
): Promise<void> {
const tab = " ".repeat(depth * 4);
try {
for await (const dirEntry of Deno.readDir(dir)) {
if (defaultSkipDirs.includes(dirEntry.name)) {
continue;
}
const fullName = joinPath(dir, dirEntry.name);
if (dirEntry.isDirectory) {
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, options.maxDepth);
}
} else {
let fileDesc = "";
if (dirEntry.isSymlink) {
fileDesc = " 🔗";
} else if (dirEntry.isFile) {
const fileInfo = await Deno.stat(fullName);
if (fileInfo.size > 1024 * 1024) {
fileDesc = term.auto(
`[red][[[ - ${formatSize2(fileInfo.size)}]]]][/]`,
);
} else {
fileDesc = term.auto(
`[yellow][[[ - ${formatSize2(fileInfo.size)}]]][/]`,
);
}
}
console.log(
`${tab}- ${
term.auto("[green][[[" + dirEntry.name + "]]][/]")
} \t${fileDesc}`,
);
}
}
} catch (e) {
console.log(term.auto(`[red][[[${tab} ERROR: ${e}]]][/]`));
}
}
async function main(): Promise<void> {
const flags = parseArgs(Deno.args, {
boolean: ["help", "hide-more-dirs"],
number: ["depth"],
});
if (flags.help) {
console.log(`Help massage for tree.ts
tree.ts [parameters] <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,
hideMoreDirs,
});
}
main().catch((e) => log.error(e));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260411T093048+08:00.MEUCIQDmZojbZR+wufhjkUbn
// UpciHPInFK+T7/F4u1pYzmfuOQIgOLnp7oKSmRQOqHo3XvP+wzZYMLAMAHanA+Q5iuODPLQ=