-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileTraversal.js
69 lines (60 loc) · 2.06 KB
/
fileTraversal.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
const fs = require('fs');
const path = require('path');
const ignore = require('ignore');
function setupIgnore(config) {
const ig = ignore();
if (config.useGitignore && fs.existsSync('.gitignore')) {
const gitignoreContent = fs.readFileSync('.gitignore', 'utf8');
ig.add(gitignoreContent.split('\n').filter(Boolean));
}
ig.add(config.ignorePaths);
return ig;
}
function shouldProcessFile(file, includeExtensions, excludeExtensions) {
const ext = path.extname(file);
if (excludeExtensions.includes(ext)) return false;
if (includeExtensions.length > 0) return includeExtensions.includes(ext);
return true;
}
function traverseDir(dir, fileStructure = {}, baseDir = dir, ig, includeExtensions, excludeExtensions, config) {
let files;
try {
files = fs.readdirSync(dir);
} catch (err) {
console.error(`Error reading directory ${dir}: ${err.message}`);
return fileStructure;
}
files.forEach((file) => {
const filePath = path.join(dir, file);
const relativePath = path.relative(baseDir, filePath);
let stats;
try {
stats = fs.statSync(filePath);
} catch (err) {
console.error(`Error stating file ${filePath}: ${err.message}`);
return;
}
if (ig.ignores(relativePath) || config.ignorePaths.includes(relativePath)) return;
if (stats.isDirectory()) {
console.log(`Traversing directory: ${relativePath}`);
fileStructure[file] = {};
traverseDir(filePath, fileStructure[file], baseDir, ig, includeExtensions, excludeExtensions, config);
} else if (shouldProcessFile(file, includeExtensions, excludeExtensions)) {
console.log(`Found file: ${relativePath}`);
fileStructure[file] = null;
}
});
return fileStructure;
}
function countFiles(fileStructure) {
let count = 0;
Object.keys(fileStructure).forEach((key) => {
if (fileStructure[key] && typeof fileStructure[key] === 'object') {
count += countFiles(fileStructure[key]);
} else {
count += 1;
}
});
return count;
}
module.exports = { setupIgnore, traverseDir, shouldProcessFile, countFiles };