-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateFileMap.js
More file actions
64 lines (55 loc) · 2.05 KB
/
generateFileMap.js
File metadata and controls
64 lines (55 loc) · 2.05 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
const fs = require("fs");
const path = require("path");
/**
* Recursively traverse the directory structure and build a file map.
* @param {string} dir - The directory to start from.
* @param {string} baseDir - The base directory for relative paths.
* @param {Object} fileMap - The object to store the file map.
* @param {Set<string>} ignoreDirs - The set of directory names to ignore.
*/
function traverseDirectory(dir, baseDir, fileMap, ignoreDirs) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const relativePath = path.relative(baseDir, filePath);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// Skip ignored directories
if (ignoreDirs.has(file)) {
return;
}
fileMap[relativePath] = "directory";
traverseDirectory(filePath, baseDir, fileMap, ignoreDirs);
} else {
fileMap[relativePath] = "file";
}
});
}
/**
* Generate a file map for a given repository.
* @param {string} repoPath - The path to the repository.
* @param {Set<string>} ignoreDirs - The set of directory names to ignore.
* @returns {Object} - The generated file map.
*/
function generateFileMap(repoPath, ignoreDirs) {
const fileMap = {};
traverseDirectory(repoPath, repoPath, fileMap, ignoreDirs);
return fileMap;
}
// Example usage:
// Replace 'c:/Users/charlie.palmer/petooly-mobile-application' with the actual path to the repository
const repoPath = "c:/Users/charlie.palmer/petooly-mobile-application"; // Use forward slashes
// Set of directories to ignore
const ignoreDirs = new Set([
"node_modules",
".git",
".husky",
"android",
"ios",
]);
const fileMap = generateFileMap(repoPath, ignoreDirs);
console.log(fileMap);
// Optionally, write the file map to a JSON file
const outputFilePath = path.join(repoPath, "fileMap.json");
fs.writeFileSync(outputFilePath, JSON.stringify(fileMap, null, 2));
console.log(`File map written to ${outputFilePath}`);