dir-analysis-tool
Version:
A comprehensive cross-platform CLI tool for advanced directory analysis with file classification, duplicate detection, large file identification, interactive mode, HTML reports, and multiple export formats. Perfect for disk cleanup, storage audits, and pr
132 lines • 5.2 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeViewGenerator = void 0;
const path = __importStar(require("path"));
const utils_1 = require("./utils");
class TreeViewGenerator {
static generateTreeView(files, rootPath) {
const tree = this.buildTree(files, rootPath);
return this.renderTree(tree);
}
static buildTree(files, rootPath) {
const root = {
name: path.basename(rootPath) || rootPath,
path: rootPath,
isDirectory: true,
children: []
};
const nodeMap = new Map();
nodeMap.set(rootPath, root);
const sortedFiles = files.sort((a, b) => a.path.localeCompare(b.path));
for (const file of sortedFiles) {
const relativePath = path.relative(rootPath, file.path);
if (!relativePath || relativePath === '.')
continue;
const parts = relativePath.split(path.sep);
let currentPath = rootPath;
let currentNode = root;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
currentPath = path.join(currentPath, part);
if (!nodeMap.has(currentPath)) {
const dirNode = {
name: part,
path: currentPath,
isDirectory: true,
children: []
};
nodeMap.set(currentPath, dirNode);
currentNode.children.push(dirNode);
}
currentNode = nodeMap.get(currentPath);
}
const fileName = parts[parts.length - 1];
const fileNode = {
name: fileName,
path: file.path,
size: file.size,
isDirectory: false
};
currentNode.children.push(fileNode);
}
this.sortTreeNodes(root);
return root;
}
static sortTreeNodes(node) {
if (!node.children)
return;
node.children.sort((a, b) => {
if (a.isDirectory && !b.isDirectory)
return -1;
if (!a.isDirectory && b.isDirectory)
return 1;
return a.name.localeCompare(b.name);
});
for (const child of node.children) {
this.sortTreeNodes(child);
}
}
static renderTree(node, prefix = '', isLast = true, level = 0) {
const MAX_DEPTH = 10;
if (level > MAX_DEPTH)
return '';
let result = '';
const connector = isLast ? '└── ' : '├── ';
const sizeInfo = node.isDirectory ? '' : ` (${(0, utils_1.formatSize)(node.size || 0)})`;
const icon = node.isDirectory ? '📁' : '📄';
result += `${prefix}${connector}${icon} ${node.name}${sizeInfo}\n`;
if (node.children && node.children.length > 0) {
const newPrefix = prefix + (isLast ? ' ' : '│ ');
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
const childIsLast = i === node.children.length - 1;
result += this.renderTree(child, newPrefix, childIsLast, level + 1);
}
}
return result;
}
static generateCompactTreeView(files, rootPath, maxFiles = 50) {
const limitedFiles = files.slice(0, maxFiles);
const tree = this.buildTree(limitedFiles, rootPath);
let result = this.renderTree(tree);
if (files.length > maxFiles) {
result += `\n... and ${files.length - maxFiles} more files\n`;
}
return result;
}
}
exports.TreeViewGenerator = TreeViewGenerator;
//# sourceMappingURL=tree-view.js.map