-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-shake.ts
37 lines (27 loc) · 1.08 KB
/
simple-shake.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// simple-shake 🫱🏻🫲🏿
// deno run --allow-read --allow-write simple-shake.ts
import { ensureDir, ensureFile } from "https://deno.land/std@0.197.0/fs/mod.ts";
const BASE_PATH = "./output";
async function processTreeFile(inputPath: string, outputPath: string) {
const content = await Deno.readTextFile(inputPath);
const lines = content.split("\n");
const pathStack: string[] = [];
for (const line of lines) {
if (!line.trim()) continue;
const indentMatch = line.match(/^[\s│]*(?:[├└]──\s*)?/);
const depth = indentMatch ? Math.floor(indentMatch[0].length / 4) : 0;
pathStack.length = depth;
const name = line.replace(/^[\s│]*[├└]──\s*/, "");
const isFolder = name.endsWith("/");
const cleanName = isFolder ? name.slice(0, -1) : name;
const fullPath = [outputPath, ...pathStack, cleanName].join("/");
if (isFolder) {
await ensureDir(fullPath);
pathStack.push(cleanName);
} else {
await ensureFile(fullPath);
}
}
}
await ensureDir(BASE_PATH);
await processTreeFile("data/tree1.txt", BASE_PATH);