-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectJSON.js
More file actions
83 lines (69 loc) · 2.56 KB
/
ProjectJSON.js
File metadata and controls
83 lines (69 loc) · 2.56 KB
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
const fs = require('fs');
const path = require('path');
const directoryPath = __dirname;
const outputFileName = path.basename(directoryPath) + '_structure.json';
function isDirectory(filePath) {
return fs.lstatSync(filePath).isDirectory();
}
function shouldExclude(filePath) {
const excludedPaths = ['.vercel', '.git', 'node_modules'];
return excludedPaths.some(excluded => filePath.includes(path.join(directoryPath, excluded))) ||
path.basename(filePath) === 'package-lock.json';
}
function shouldExcludeContent(filePath) {
const binaryExtensions = ['.ico', '.jpg', '.jpeg', '.png', '.webp', '.svg', '.ttf', '.woff', '.woff2', '.eot', '.otf', '.mp3', '.mp4', '.avi', '.mov', '.pdf', '.docx', '.pptx', '.xlsx', '.bin'];
return binaryExtensions.some(ext => filePath.endsWith(ext));
}
function processFileContent(filePath) {
if (shouldExcludeContent(filePath)) {
return 'Binary file - content not shown';
} else if (filePath.endsWith('.env')) {
return fs.readFileSync(filePath, 'utf8').replace(/=.*/g, '=PLACEHOLDER');
} else {
return fs.readFileSync(filePath, 'utf8');
}
}
function constructFileTree(dir, relativePath = '') {
let tree = [];
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
const relPath = path.join(relativePath, file);
if (shouldExclude(fullPath)) {
return;
}
if (isDirectory(fullPath)) {
let subTree = constructFileTree(fullPath, relPath);
if (subTree.length > 0) {
tree.push({ [relPath + '/']: subTree });
} else {
tree.push(relPath + '/');
}
} else {
tree.push(relPath);
}
});
return tree;
}
function readDirectoryRecursively(dir, obj, relativePath = '') {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
const relPath = path.join(relativePath, file);
if (shouldExclude(fullPath)) {
return;
}
if (isDirectory(fullPath)) {
obj[relPath + '/'] = {};
readDirectoryRecursively(fullPath, obj[relPath + '/'], relPath);
} else {
obj[relPath] = processFileContent(fullPath);
}
});
}
const fileTree = constructFileTree(directoryPath);
const detailedFileTree = {};
readDirectoryRecursively(directoryPath, detailedFileTree);
const projectJson = {
fileTree: fileTree,
detailedFileTree: detailedFileTree
};
fs.writeFileSync(outputFileName, JSON.stringify(projectJson, null, 2));