CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-fs-readdir-recursive

Recursively read a directory and return an array of all file paths

90

1.11x
Overview
Eval results
Files

task.mdevals/scenario-4/

Directory File Collector

Build a utility that collects all files from a directory tree and organizes them in a structured way that preserves relative path information.

Requirements

Create a function collectProjectFiles(projectRoot) that:

  1. Scans a directory tree starting from the given projectRoot path
  2. Returns a structured result containing:
    • A list of all file paths found
    • A categorization of files by their directory depth (0 for root-level files, 1 for files in immediate subdirectories, etc.)
  3. Preserves relative path structure - all file paths should be relative to projectRoot
  4. Excludes hidden files (files/directories starting with a dot)

The 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.
  }
}

Test Cases

Given a directory structure:

/test-project/
  README.md
  package.json
  src/
    index.js
    utils/
      helper.js
  .gitignore
  • When 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

Implementation

@generates

API

/**
 * 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);

Dependencies { .dependencies }

fs-readdir-recursive { .dependency }

Provides recursive directory reading with relative path support.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-fs-readdir-recursive

tile.json