forked from module-federation/module-federation-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateReadmeExampleList.js
112 lines (89 loc) · 3.69 KB
/
updateReadmeExampleList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Import required modules
const fs = require('fs');
const path = require('path');
// Function to recursively search for configuration files
function searchConfigFiles(dirPath) {
// List files to search for
const configFiles = ['rspack.config.js', 'rspack.config.ts','rspack.dev.js','rspack.prod.js', 'rsbuild.config.js', 'rsbuild.config.ts', 'webpack.config.js','webpack.client.js','modern.config.js','next.config.js'];
const foundFiles = { rspack: false, webpack: false };
// Traverse the directory tree
const files = fs.readdirSync(dirPath);
for (let i = 0; i < files.length; i++) {
const filePath = path.resolve(dirPath, files[i]);
const stat = fs.statSync(filePath);
// If the file is a directory and not 'node_modules', search it for configuration files
if (stat.isDirectory() && files[i] !== 'node_modules') {
const found = searchConfigFiles(filePath);
foundFiles.rspack = foundFiles.rspack || found.rspack;
foundFiles.webpack = foundFiles.webpack || found.webpack;
} else if (configFiles.includes(files[i])) {
// If the file is a configuration file, update foundFiles
if (files[i].includes('rspack') || files[i].includes('rsbuild')) {
foundFiles.rspack = true;
} else if (files[i].includes('webpack') || files[i].includes('next.config')) {
foundFiles.webpack = true;
}
}
}
return foundFiles;
}
// Function to get directory tree
function getDirectoryTree(dirPath, rootPath, depth = 0) {
// If there's no 'package.json' in the directory, return null
if(!fs.existsSync(path.join(dirPath, 'package.json'))){
return null;
}
const result = { name: path.basename(dirPath), path: path.relative(rootPath, dirPath), children: [], description: 'No description', rspack: false, webpack: false };
// Check if 'package.json' exists in the directory
const packageJson = require(path.join(dirPath, 'package.json'));
if(packageJson.ignored === true) return null;
if(!packageJson.description && depth > 1) {
console.log('depth', depth, 'no description', dirPath);
return null;
}
result.name = packageJson.name || result.name;
result.description = packageJson.description || result.description;
// Check for 'rspack' or 'webpack' configuration files
const foundFiles = searchConfigFiles(dirPath);
result.rspack = foundFiles.rspack;
result.webpack = foundFiles.webpack;
const files = fs.readdirSync(dirPath);
// Loop through each file in the directory
files.forEach((file) => {
const filePath = path.resolve(dirPath, file);
const stat = fs.statSync(filePath);
// If the file is a directory, not 'node_modules', not '.git', not '.next', and contains 'package.json'
if (stat.isDirectory() && file !== 'node_modules' && file !== '.git' && file !== '.next') {
const child = getDirectoryTree(filePath, rootPath, depth + 1);
if(child){
result.children.push(child);
}
}
});
return result;
}
// Function to convert tree to markdown
function treeToMarkdown(tree, depth = 0) {
if(!tree){
return '';
}
let markdown = `${' '.repeat(depth)}- [${tree.name}](${tree.path})`;
if (tree.rspack || tree.webpack) {
markdown += ` -- ${tree.rspack ? '✅ rspack' : '❌ rspack'} | ${tree.webpack ? '✅ webpack' : '❌ webpack'}`;
}
markdown += ` <br> ${tree.description} \n`;
tree.children.forEach((child) => {
markdown += treeToMarkdown(child, depth + 1);
});
return markdown;
}
// Main function
function main() {
const rootPath = path.resolve('./');
const tree = getDirectoryTree(rootPath, rootPath);
const markdown = treeToMarkdown(tree);
// Write the markdown to 'output.md'
fs.writeFileSync('output.md', markdown);
}
// Call the main function
main();