| 1 | #!/usr/bin/env node |
| 2 | // Convierte la salida de `tree` en src/data.js |
| 3 | // para probar el visor con monorepos sin tener la carpeta real. |
| 4 | // Los tamaños se estiman por extensión (la salida de tree no los incluye). |
| 5 | // Uso: node tools/tree2data.mjs <tree.txt> |
| 6 | import { readFileSync, writeFileSync } from 'node:fs'; |
| 7 | import { join, dirname } from 'node:path'; |
| 8 | import { fileURLToPath } from 'node:url'; |
| 9 | |
| 10 | const HERE = dirname(fileURLToPath(import.meta.url)); |
| 11 | const input = process.argv[2]; |
| 12 | if (!input) { console.error('uso: node tools/tree2data.mjs <tree.txt>'); process.exit(1); } |
| 13 | |
| 14 | const EXT_SIZE = { |
| 15 | woff: 30000, woff2: 24000, ttf: 80000, png: 45000, jpg: 60000, jpeg: 60000, |
| 16 | gif: 20000, svg: 4000, ico: 8000, ts: 3500, tsx: 4200, js: 3000, jsx: 3800, |
| 17 | rb: 2800, py: 3000, md: 2500, json: 1800, yml: 900, yaml: 900, lock: 400000, |
| 18 | css: 2200, scss: 2400, html: 3000, sql: 5000, sh: 1200, |
| 19 | }; |
| 20 | function hash(s) { let h = 7; for (const c of s) h = (h * 31 + c.charCodeAt(0)) >>> 0; return h; } |
| 21 | function sizeOf(name) { |
| 22 | const ext = name.split('.').pop().toLowerCase(); |
| 23 | const base = EXT_SIZE[ext] ?? 1500; |
| 24 | return Math.round(base * (0.6 + (hash(name) % 100) / 110)); |
| 25 | } |
| 26 | |
| 27 | // algunas versiones de tree indentan con espacios no separables (U+00A0) |
| 28 | const lines = readFileSync(input, 'utf8').replace(/ /g, ' ').split('\n'); |
| 29 | const mkNode = (name) => ({ name, dirs: [], leaf: [], isDir: false }); |
| 30 | const root = mkNode(lines[0].trim() === '.' ? 'monorepo' : lines[0].trim()); |
| 31 | root.isDir = true; |
| 32 | const atDepth = [root]; |
| 33 | let skipDepth = Infinity; |
| 34 | let total = 0; |
| 35 | |
| 36 | for (let i = 1; i < lines.length; i++) { |
| 37 | const m = lines[i].match(/^((?: |│ )*)(?:├── |└── )(.*)$/); |
| 38 | if (!m) continue; // resumen final, líneas vacías… |
| 39 | const depth = m[1].length / 4 + 1; |
| 40 | let name = m[2]; |
| 41 | const arrow = name.indexOf(' -> '); |
| 42 | if (arrow !== -1) name = name.slice(0, arrow); // symlinks |
| 43 | if (depth >= skipDepth) continue; |
| 44 | skipDepth = Infinity; |
| 45 | if (name === 'node_modules' || name === '.git') { skipDepth = depth + 1; continue; } |
| 46 | const node = mkNode(name); |
| 47 | const parent = atDepth[depth - 1]; |
| 48 | if (!parent) continue; |
| 49 | parent.isDir = true; |
| 50 | parent.entries ??= []; |
| 51 | parent.entries.push(node); |
| 52 | atDepth[depth] = node; |
| 53 | atDepth.length = depth + 1; |
| 54 | total++; |
| 55 | } |
| 56 | |
| 57 | // clasifica: con hijos → dir; sin hijos → fichero (los dirs vacíos quedan como fichero de 0 B) |
| 58 | let files = 0; |
| 59 | function pack(node) { |
| 60 | const dirs = [], leaf = []; |
| 61 | for (const e of node.entries ?? []) { |
| 62 | if (e.entries) dirs.push(pack(e)); |
| 63 | else { leaf.push([e.name, sizeOf(e.name)]); files++; } |
| 64 | } |
| 65 | return [node.name, dirs, leaf]; |
| 66 | } |
| 67 | const tree = pack(root); |
| 68 | |
| 69 | const out = `// Generado por tools/tree2data.mjs desde ${input} |
| 70 | // dir = [nombre, [subdirs], [ficheros]] · fichero = [nombre, bytes, ignorado?] |
| 71 | export const REPO_TREE = ${JSON.stringify(tree)}; |
| 72 | export const REPO_META = ${JSON.stringify({ git: null, owners: [], links: {} })}; |
| 73 | `; |
| 74 | writeFileSync(join(HERE, '..', 'src', 'data.js'), out); |
| 75 | console.log(`OK: ${total} entradas, ${files} ficheros → src/data.js`); |