UNPKG

repomix

Version:

A tool to pack repository contents to single file for AI consumption

63 lines (62 loc) 2.35 kB
import path from 'node:path'; import readline from 'node:readline/promises'; import { RepomixError } from '../../shared/errorHandle.js'; import { logger } from '../../shared/logger.js'; export const filterValidLines = (lines) => { return lines.map((line) => line.trim()).filter((line) => line && !line.startsWith('#')); }; export const resolveAndDeduplicatePaths = (lines, cwd) => { const resolvedPaths = lines.map((line) => { const filePath = path.isAbsolute(line) ? path.normalize(line) : path.normalize(path.resolve(cwd, line)); logger.trace(`Resolved path: ${line} -> ${filePath}`); return filePath; }); return [...new Set(resolvedPaths)]; }; export const readLinesFromStream = async (input, createInterface = readline.createInterface) => { const rl = createInterface({ input }); const lines = []; try { for await (const line of rl) { lines.push(line); } return lines; } finally { if (rl && typeof rl.close === 'function') { rl.close(); } } }; export const readFilePathsFromStdin = async (cwd, deps = { stdin: process.stdin, createReadlineInterface: readline.createInterface, }) => { logger.trace('Reading file paths from stdin...'); try { const { stdin, createReadlineInterface } = deps; if (stdin.isTTY) { throw new RepomixError('No data provided via stdin. Please pipe file paths to repomix when using --stdin flag.'); } const rawLines = await readLinesFromStream(stdin, createReadlineInterface); const validLines = filterValidLines(rawLines); if (validLines.length === 0) { throw new RepomixError('No valid file paths found in stdin input.'); } const filePaths = resolveAndDeduplicatePaths(validLines, cwd); logger.trace(`Found ${filePaths.length} file paths from stdin`); return { filePaths, emptyDirPaths: [], }; } catch (error) { if (error instanceof RepomixError) { throw error; } if (error instanceof Error) { throw new RepomixError(`Failed to read file paths from stdin: ${error.message}`); } throw new RepomixError('An unexpected error occurred while reading from stdin.'); } };