From 3eebd144ee504e88fc5921a22ddba9f12b5389d0 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 26 Jan 2026 01:19:49 +0800 Subject: [PATCH] add tree.ts --- script-meta-v2.json | 9 ++++++ single-scripts/tree.ts | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 single-scripts/tree.ts diff --git a/script-meta-v2.json b/script-meta-v2.json index 77f9f6e..a291517 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -209,6 +209,15 @@ "publish_time": 1768111677531, "update_time": 1769359601445 }, + "tree.ts": { + "script_name": "tree.ts", + "script_length": 2191, + "script_sha256": "de953afc79f0e3c8419e16d9ed0efb00b04ae59688a8ee34c4013b0279b877ca", + "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": 1769361581018 + }, "wget.ts": { "script_name": "wget.ts", "script_length": 4076, diff --git a/single-scripts/tree.ts b/single-scripts/tree.ts new file mode 100755 index 0000000..532bada --- /dev/null +++ b/single-scripts/tree.ts @@ -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 { + 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 { + 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=