add tree.ts
This commit is contained in:
64
single-scripts/tree.ts
Executable file
64
single-scripts/tree.ts
Executable file
@@ -0,0 +1,64 @@
|
||||
#!/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"];
|
||||
|
||||
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.message}`));
|
||||
}
|
||||
}
|
||||
|
||||
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.20260126T011930+08:00.MEUCIQDGcKujeENYovKiJ/LY
|
||||
// 0hWJTrmhrGRr4lAb7OP0M3K43gIgVa3LNCnH8EYss9/rGgbQlZVEBiPVKqRh6BsMfUDs5Tc=
|
||||
Reference in New Issue
Block a user