codejump
EasyPro challengeJavaScriptTypeScript

Node.js — List a Folder Tree

Node.jsBackendFilesRecursion

Before serving a folder, tinyserve prints what it is about to publish. That means walking the
folder recursively — and deciding what not to publish: .git, .env and other hidden
entries.

Your task

Write async listFiles(root). Return an array of { path, size } for every file under
root, at any depth:

  • path is relative to root, with / as separator: 'css/app.css'.
  • size is the file size in bytes (from fs.stat).
  • Skip every entry whose name starts with . — for a folder, skip everything inside it too.
  • Empty folders produce nothing.
  • Sort the result by path with plain string comparison (a.path < b.path), so 'B.txt'

comes before 'a.txt'.

Use [`fs.readdir(dir, { withFileTypes: true })`](https://nodejs.org/api/fs.html#fspromisesreaddirpath-options):
it returns Dirent objects with isFile() and isDirectory(), saving a stat call per
entry.

Example

site/
├── .env            "SECRET=1"
├── index.html      "x"
└── css/app.css     "body{}"

→ [{ path: 'css/app.css', size: 6 }, { path: 'index.html', size: 1 }]

Sample tests

Test #1One file
Input: [{"index.html":"<h1>Hi</h1>"}]
Output: [{"path":"index.html","size":11}]
Test #2Nested folders, sorted
Input: [{"js/app.js":"1;","index.html":"x","css/app.css":"body{}"}]
Output: [{"path":"css/app.css","size":6},{"path":"index.html","size":1},{"path":"js/app.js","size":2}]
Test #3Hidden entries skipped, size in bytes
Input: [{".env":"SECRET=1",".git/HEAD":"ref","about.txt":"café"}]
Output: [{"path":"about.txt","size":5}]
Test #4Empty folder
Input: [{}]
Output: []