All files directory.js

100% Statements 26/26
88.88% Branches 8/9
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50    1x 1x   1x 1x     12x       19x 19x   1x 1x     18x 19x   13x   13x 13x   1x 1x     12x 9x       17x 12x 7x         12x 12x     1x  
'use strict'
 
const fs = require('node:fs')
const path = require('node:path')
 
const LOCK_FILES = new Set(['yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'package-lock.json'])
const SKIP_DIRS = new Set(['node_modules', '.git'])
 
function findPackageDirs(baseDir = '.') {
  const results = []
 
  function walk(dir) {
    let entries
    try {
      entries = fs.readdirSync(dir, { withFileTypes: true })
    } catch {
      console.warn(`Failed to read directory ${dir}, skipping.`)
      return
    }
 
    for (const entry of entries) {
      if (!entry.isDirectory() || SKIP_DIRS.has(entry.name)) continue
 
      const fullPath = path.join(dir, entry.name)
      let children
      try {
        children = fs.readdirSync(fullPath)
      } catch {
        console.warn(`Failed to read directory ${fullPath}, skipping.`)
        continue
      }
 
      if (children.includes('package.json')) {
        results.push(fullPath)
      }
 
      // Don't recurse into projects that have their own lock file (workspace roots)
      const hasLockFile = children.some(child => LOCK_FILES.has(child))
      if (!hasLockFile) {
        walk(fullPath)
      }
    }
  }
 
  walk(baseDir)
  return results
}
 
module.exports = { findPackageDirs }