MCPcopy Create free account
hub / github.com/module-federation/module-federation-examples / getDirectoryTree

Function getDirectoryTree

updateReadmeExampleList.js:47–97  ·  view source on GitHub ↗
(dirPath, rootPath, depth = 0)

Source from the content-addressed store, hash-verified

45
46// Function to get directory tree
47function getDirectoryTree(dirPath, rootPath, depth = 0) {
48 // If there's no 'package.json' in the directory, return null
49 if (!fs.existsSync(path.join(dirPath, 'package.json'))) {
50 return null;
51 }
52
53 const result = {
54 name: path.basename(dirPath),
55 path: path.relative(rootPath, dirPath),
56 children: [],
57 description: 'No description',
58 rspack: false,
59 webpack: false,
60 };
61
62 // Check if 'package.json' exists in the directory
63 const packageJson = require(path.join(dirPath, 'package.json'));
64
65 if (packageJson.ignored === true) return null;
66
67 if (!packageJson.description && depth > 1) {
68 console.log('depth', depth, 'no description', dirPath);
69 return null;
70 }
71
72 result.name = packageJson.name || result.name;
73 result.description = packageJson.description || result.description;
74
75 // Check for 'rspack' or 'webpack' configuration files
76 const foundFiles = searchConfigFiles(dirPath);
77 result.rspack = foundFiles.rspack;
78 result.webpack = foundFiles.webpack;
79
80 const files = fs.readdirSync(dirPath);
81
82 // Loop through each file in the directory
83 files.forEach(file => {
84 const filePath = path.resolve(dirPath, file);
85 const stat = fs.statSync(filePath);
86
87 // If the file is a directory, not 'node_modules', not '.git', not '.next', and contains 'package.json'
88 if (stat.isDirectory() && file !== 'node_modules' && file !== '.git' && file !== '.next') {
89 const child = getDirectoryTree(filePath, rootPath, depth + 1);
90 if (child) {
91 result.children.push(child);
92 }
93 }
94 });
95
96 return result;
97}
98
99// Function to convert tree to markdown
100function treeToMarkdown(tree, depth = 0) {

Callers 1

mainFunction · 0.85

Calls 1

searchConfigFilesFunction · 0.85

Tested by

no test coverage detected