-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const fs = require('fs'); | ||
Check failure on line 1 in listFiles.js GitHub Actions / linux-tests (12)
Check failure on line 1 in listFiles.js GitHub Actions / linux-tests (12)
Check failure on line 1 in listFiles.js GitHub Actions / linux-tests (16)
Check failure on line 1 in listFiles.js GitHub Actions / linux-tests (16)
Check failure on line 1 in listFiles.js GitHub Actions / linux-tests (18)
|
||
const path = require('path'); | ||
Check failure on line 2 in listFiles.js GitHub Actions / linux-tests (12)
Check failure on line 2 in listFiles.js GitHub Actions / linux-tests (16)
|
||
|
||
/** | ||
* Recursively collects all file paths in a directory. | ||
* @param {string} dir - The directory to scan. | ||
* @param {string} baseDir - The base directory for relative paths. | ||
* @param {Array<string>} fileList - Array to collect file paths. | ||
* @returns {Array<string>} The list of file paths. | ||
*/ | ||
function getAllFiles(dir, baseDir = dir, fileList = []) { | ||
const files = fs.readdirSync(dir); | ||
Check warning on line 12 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 12 in listFiles.js GitHub Actions / linux-tests (16)
|
||
|
||
files.forEach((file) => { | ||
Check warning on line 14 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 14 in listFiles.js GitHub Actions / linux-tests (16)
|
||
const filePath = path.join(dir, file); | ||
Check warning on line 15 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 15 in listFiles.js GitHub Actions / linux-tests (16)
|
||
const stats = fs.statSync(filePath); | ||
Check warning on line 16 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 16 in listFiles.js GitHub Actions / linux-tests (16)
|
||
|
||
if (stats.isDirectory()) { | ||
Check warning on line 18 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 18 in listFiles.js GitHub Actions / linux-tests (16)
|
||
getAllFiles(filePath, baseDir, fileList); // Recurse into subdirectory | ||
Check warning on line 19 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 19 in listFiles.js GitHub Actions / linux-tests (16)
|
||
} else { | ||
Check warning on line 20 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 20 in listFiles.js GitHub Actions / linux-tests (16)
|
||
fileList.push('"src/lib/' + path.relative(baseDir, filePath) + '",'); // Store relative path | ||
Check warning on line 21 in listFiles.js GitHub Actions / linux-tests (12)
Check failure on line 21 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 21 in listFiles.js GitHub Actions / linux-tests (16)
Check failure on line 21 in listFiles.js GitHub Actions / linux-tests (16)
Check warning on line 21 in listFiles.js GitHub Actions / linux-tests (18)
|
||
} | ||
Check warning on line 22 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 22 in listFiles.js GitHub Actions / linux-tests (16)
|
||
}); | ||
Check warning on line 23 in listFiles.js GitHub Actions / linux-tests (12)
Check warning on line 23 in listFiles.js GitHub Actions / linux-tests (16)
|
||
|
||
return fileList; | ||
} | ||
|
||
// Usage | ||
const targetDir = process.argv[2]; // Directory passed as argument | ||
if (!targetDir) { | ||
console.error('Please provide a directory path.'); | ||
process.exit(1); | ||
} | ||
|
||
try { | ||
const relativeFilePaths = getAllFiles(targetDir); | ||
relativeFilePaths.forEach((filePath) => console.log(filePath)); | ||
} catch (error) { | ||
console.error(`Error reading directory: ${error.message}`); | ||
} |