UNPKG

analyze-project-structure

Version:

CLI tool for analyzing and printing the folder structure of a project, extracting details about files (such as functions, variables, routes, and imports/exports), and summarizing the project's code organization.

102 lines (91 loc) 3.16 kB
const fs = require("fs"); const path = require("path"); const extractAngularFileDetails = require("../analyzers/angularAnalyzer"); const extractExpressFileDetails = require("../analyzers/expressAnalyzer"); const extractReactFileDetails = require("../analyzers/reactAnalyzer"); const printFolderStructure = ( dirPath, prefix = "", output = "", projectType, asObject = false ) => { const files = fs.readdirSync(dirPath); const entries = files .map((file) => { const fullPath = path.join(dirPath, file); return { name: file, path: fullPath, isDir: fs.statSync(fullPath).isDirectory(), }; }) .sort((a, b) => { if (a.isDir && !b.isDir) return -1; if (!a.isDir && b.isDir) return 1; return a.name.localeCompare(b.name); }); if (asObject) { // JSON object mode const children = entries.map((entry) => { if (entry.isDir) { return printFolderStructure(entry.path, "", "", projectType, true); } else { let details = { name: entry.name, type: "file" }; if (projectType === "angular" && entry.path.endsWith(".ts")) { details.details = extractAngularFileDetails(entry.path, "", "", true); } else if ( projectType === "express" && (entry.path.endsWith(".js") || entry.path.endsWith(".ts")) ) { details.details = extractExpressFileDetails(entry.path, "", "", true); } else if ( projectType === "react" && (entry.path.endsWith(".jsx") || entry.path.endsWith(".tsx")) ) { details.details = extractReactFileDetails(entry.path, "", "", true); } return details; } }); return { name: path.basename(dirPath), type: "directory", children, }; } else { // Text string mode entries.forEach((entry, index) => { const isLast = index === entries.length - 1; const connector = isLast ? "└── " : "├── "; output += prefix + connector + entry.name + "\n"; if (entry.isDir) { const newPrefix = prefix + (isLast ? " " : "│ "); output = printFolderStructure( entry.path, newPrefix, output, projectType, false ); } else { const filePrefix = prefix + (isLast ? " " : "│ "); if (projectType === "angular" && entry.path.endsWith(".ts")) { output = extractAngularFileDetails(entry.path, filePrefix, output); } else if ( projectType === "express" && (entry.path.endsWith(".js") || entry.path.endsWith(".ts")) ) { output = extractExpressFileDetails(entry.path, filePrefix, output); } else if ( projectType === "react" && (entry.path.endsWith(".jsx") || entry.path.endsWith(".tsx")) ) { output = extractReactFileDetails(entry.path, filePrefix, output); } } }); return output; } }; module.exports = printFolderStructure;