forked from Plant-for-the-Planet-org/planet-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolderCrawler.js
32 lines (28 loc) · 935 Bytes
/
folderCrawler.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
const fs = require('fs');
const path = require('path');
function crawlFolderStructure(rootDir) {
const files = [];
function crawlDirectory(dir) {
const items = fs.readdirSync(dir);
for (const item of items) {
const itemPath = path.join(dir, item);
const stats = fs.statSync(itemPath);
if (stats.isDirectory()) {
crawlDirectory(itemPath); // Recursive call for subdirectories
} else {
files.push(`/${path.relative(rootDir, itemPath).replace(/\[.*?\]/g, ':path').replace(/\/?index\.tsx?$/, '').replace(/\.tsx?$/, '')}`); // Add modified file path to the array
}
}
}
try {
crawlDirectory(rootDir);
return files;
} catch (error) {
console.error('Error while crawling:', error.message);
return [];
}
}
const scriptDir = __dirname;
const rootPath = path.join(scriptDir, 'pages');
const fileList = crawlFolderStructure(rootPath);
console.log(fileList);