UNPKG

@zapier/stubtree

Version:

CLI tool to generate ASCII tree of project structure with symbol stubs using universal-ctags

66 lines 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseTagLine = parseTagLine; exports.buildTreeFromTags = buildTreeFromTags; const path_1 = require("path"); function parseTagLine(line, rootPath) { try { const data = JSON.parse(line); const path = (0, path_1.relative)(rootPath, data.path); const tag = { path, name: data.name, kind: data.kind, }; if (data.signature) { tag.signature = data.signature; } return tag; } catch (err) { return null; } } function buildTreeFromTags(tags) { const root = { name: '.', type: 'directory', children: [] }; const tagsByFile = new Map(); for (const tag of tags) { if (!tagsByFile.has(tag.path)) { tagsByFile.set(tag.path, []); } tagsByFile.get(tag.path).push(tag); } for (const [filePath, fileTags] of tagsByFile) { const parts = filePath.split('/'); let current = root; for (let i = 0; i < parts.length - 1; i++) { const part = parts[i]; let child = current.children?.find(c => c.name === part && c.type === 'directory'); if (!child) { child = { name: part, type: 'directory', children: [] }; if (!current.children) current.children = []; current.children.push(child); } current = child; } const fileName = parts[parts.length - 1]; if (!current.children) current.children = []; current.children.push({ name: fileName, type: 'file', tags: fileTags }); } return root; } //# sourceMappingURL=parser.js.map