UNPKG

scai

Version:

> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.

55 lines (54 loc) 2.62 kB
import fs from 'fs'; import path from 'path'; import { getIndexDir } from '../constants.js'; /** * Generate a reduced file tree centered around the focus path, including nearby sibling folders. */ export function generateFocusedFileTree(focusPath, maxDepth = 2, siblingWindow = 2) { const absoluteFocus = path.resolve(focusPath); const fileOrDir = fs.statSync(absoluteFocus); const targetDir = fileOrDir.isDirectory() ? absoluteFocus : path.dirname(absoluteFocus); const parentDir = path.dirname(targetDir); const indexDir = getIndexDir(); const siblings = fs .readdirSync(parentDir, { withFileTypes: true }) .filter(entry => entry.isDirectory()) .sort((a, b) => a.name.localeCompare(b.name)); const focusIndex = siblings.findIndex(entry => path.resolve(path.join(parentDir, entry.name)) === path.resolve(targetDir)); const start = Math.max(0, focusIndex - siblingWindow); const end = Math.min(siblings.length, focusIndex + siblingWindow + 1); const nearbySiblings = siblings.slice(start, end); const relativeTitle = path.relative(indexDir, parentDir).replace(/\\/g, '/') || '.'; let output = `📂 ${relativeTitle}\n`; nearbySiblings.forEach(entry => { const siblingPath = path.join(parentDir, entry.name); const isFocusDir = path.resolve(siblingPath) === path.resolve(targetDir); const tree = generateFileTree(siblingPath, maxDepth - 1, isFocusDir ? absoluteFocus : undefined, '│ '); output += `${isFocusDir ? '➡️ ' : ''}${entry.name}/\n${tree}`; }); return output; } function generateFileTree(dir, depth, highlightPath, prefix = '') { if (depth < 0) return ''; let output = ''; const entries = fs.readdirSync(dir, { withFileTypes: true }); const sorted = entries.sort((a, b) => Number(b.isDirectory()) - Number(a.isDirectory())); sorted.forEach((entry, index) => { const isLast = index === sorted.length - 1; const connector = isLast ? '└── ' : '├── '; const fullPath = path.join(dir, entry.name); const isHighlighted = highlightPath && path.resolve(fullPath) === path.resolve(highlightPath); if (entry.isDirectory()) { output += `${prefix}${connector}${entry.name}/\n`; output += generateFileTree(fullPath, depth - 1, highlightPath, prefix + (isLast ? ' ' : '│ ')); } else { const name = isHighlighted ? `➡️ ${entry.name}` : entry.name; output += `${prefix}${connector}${name}\n`; } }); return output; }