Recursively read a directory and return an array of all file paths
90
Build a utility that collects all files from a directory tree and organizes them in a structured way that preserves relative path information.
Create a function collectProjectFiles(projectRoot) that:
projectRoot pathprojectRootThe function should return an object with this structure:
{
allFiles: string[], // All file paths found, relative to projectRoot
filesByDepth: {
0: string[], // Files at root level
1: string[], // Files one level deep
2: string[], // Files two levels deep
// etc.
}
}Given a directory structure:
/test-project/
README.md
package.json
src/
index.js
utils/
helper.js
.gitignoreWhen calling collectProjectFiles('/test-project'), the allFiles array should contain ['README.md', 'package.json', 'src/index.js', 'src/utils/helper.js'] (paths relative to root, .gitignore excluded) @test
When calling collectProjectFiles('/test-project'), the filesByDepth should categorize files correctly: depth 0 has ['README.md', 'package.json'], depth 1 has ['src/index.js'], depth 2 has ['src/utils/helper.js'] @test
When a directory doesn't exist, collectProjectFiles('/non-existent') should return empty results: { allFiles: [], filesByDepth: {} } @test
@generates
/**
* Collects all files from a directory tree
* @param {string} projectRoot - Root directory to scan
* @returns {{allFiles: string[], filesByDepth: {[depth: number]: string[]}}} File collection organized by depth
*/
function collectProjectFiles(projectRoot);Provides recursive directory reading with relative path support.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-fs-readdir-recursive