UNPKG

repomix

Version:

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

81 lines (80 loc) 3.12 kB
import path from 'node:path'; import strip from '@repomix/strip-comments'; const rtrimLines = (content) => content .split('\n') .map((line) => line.trimEnd()) .join('\n'); class BaseManipulator { removeComments(content) { return content; } removeEmptyLines(content) { return content .split('\n') .filter((line) => line.trim() !== '') .join('\n'); } } class StripCommentsManipulator extends BaseManipulator { language; constructor(language) { super(); this.language = language; } removeComments(content) { const result = strip(content, { language: this.language, preserveNewlines: true, }); return rtrimLines(result); } } class CompositeManipulator extends BaseManipulator { manipulators; constructor(...manipulators) { super(); this.manipulators = manipulators; } removeComments(content) { return this.manipulators.reduce((acc, manipulator) => manipulator.removeComments(acc), content); } } const manipulators = { '.c': new StripCommentsManipulator('c'), '.h': new StripCommentsManipulator('c'), '.hpp': new StripCommentsManipulator('cpp'), '.cpp': new StripCommentsManipulator('cpp'), '.cc': new StripCommentsManipulator('cpp'), '.cxx': new StripCommentsManipulator('cpp'), '.cs': new StripCommentsManipulator('csharp'), '.css': new StripCommentsManipulator('css'), '.dart': new StripCommentsManipulator('c'), '.go': new StripCommentsManipulator('go'), '.html': new StripCommentsManipulator('html'), '.java': new StripCommentsManipulator('java'), '.js': new StripCommentsManipulator('javascript'), '.jsx': new StripCommentsManipulator('javascript'), '.kt': new StripCommentsManipulator('c'), '.less': new StripCommentsManipulator('less'), '.php': new StripCommentsManipulator('php'), '.py': new StripCommentsManipulator('python'), '.rb': new StripCommentsManipulator('ruby'), '.rs': new StripCommentsManipulator('c'), '.sass': new StripCommentsManipulator('sass'), '.scss': new StripCommentsManipulator('sass'), '.sh': new StripCommentsManipulator('perl'), '.sol': new StripCommentsManipulator('c'), '.sql': new StripCommentsManipulator('sql'), '.swift': new StripCommentsManipulator('swift'), '.ts': new StripCommentsManipulator('javascript'), '.tsx': new StripCommentsManipulator('javascript'), '.xml': new StripCommentsManipulator('xml'), '.yaml': new StripCommentsManipulator('perl'), '.yml': new StripCommentsManipulator('perl'), '.vue': new CompositeManipulator(new StripCommentsManipulator('html'), new StripCommentsManipulator('css'), new StripCommentsManipulator('javascript')), '.svelte': new CompositeManipulator(new StripCommentsManipulator('html'), new StripCommentsManipulator('css'), new StripCommentsManipulator('javascript')), }; export const getFileManipulator = (filePath) => { const ext = path.extname(filePath); return manipulators[ext] || null; };