Files
ts-scripts/single-scripts/tree.ts
2026-01-27 00:54:25 +08:00

78 lines
2.2 KiB
TypeScript
Executable File

#!/usr/bin/env runts -- --allow-read --allow-import
import {
formatSize2,
joinPath,
log,
term,
} from "https://global.hatter.ink/script/get/@16/deno-commons-mod.ts";
const defaultSkipDirs = [
".git",
".idea",
".gradle",
"target",
".DS_Store",
".localized",
"node_modules",
];
async function listDir(
dir: string,
depth: number,
maxDepth: number,
): 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 <= maxDepth;
console.log(
`${tab}- [${dirEntry.name}]${
showNextDepth ? "" : " ...more dirs..."
}`,
);
if (showNextDepth) {
await listDir(fullName, depth + 1, 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.red(` - ${formatSize2(fileInfo.size)}`);
} else {
fileDesc = term.yellow(
` - ${formatSize2(fileInfo.size)}`,
);
}
}
console.log(
`${tab}- ${term.green(dirEntry.name)} ${fileDesc}`,
);
}
}
} catch (e) {
console.log(term.red(`${tab} ERROR: ${e}`));
}
}
async function main(): Promise<void> {
let baseDir = ".";
if (Deno.args.length > 0) {
baseDir = Deno.args[0];
}
await listDir(baseDir, 0, 10);
}
main().catch((e) => log.error(e));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260127T005403+08:00.MEUCIFwdYkm52+V1DPMbAfoe
// b9/2d4Lnf0lfVqFbE3S17N+FAiEA8ySrI+cc6aKtr2eyy1veHPbTxPHoh57Cg3ozdYrzJfI=