-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_modules.js
58 lines (49 loc) · 1.6 KB
/
parse_modules.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
const fs = require('fs');
const path = require('path');
function processDirectory(dirPath) {
const result = {
name: path.basename(dirPath),
type: 'directory',
children: []
};
const items = fs.readdirSync(dirPath);
for (const item of items) {
const fullPath = path.join(dirPath, item);
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
const subdirResult = processDirectory(fullPath);
if (subdirResult.children.length > 0) { // Only include directories with relevant files
result.children.push(subdirResult);
}
} else {
const ext = path.extname(item).toLowerCase();
if (['.cpp', '.hpp'].includes(ext)) {
const content = fs.readFileSync(fullPath, 'utf8');
result.children.push({
name: item,
type: 'file',
extension: ext,
content: content
});
}
}
}
// Sort children: directories first, then files
result.children.sort((a, b) => {
if (a.type === b.type) {
return a.name.localeCompare(b.name);
}
return a.type === 'directory' ? -1 : 1;
});
return result;
}
// Starting point
const rootDir = '.'; // Adjust this path as needed
const output = processDirectory(rootDir);
// Write to file
fs.writeFileSync(
'cpp_modules_structure.json',
JSON.stringify(output, null, 2),
'utf8'
);
console.log('Processing complete! Check cpp_modules_structure.json for the result.');