@2501-ai/cli
Version:
[](https://www.npmjs.com/package/@2501-ai/cli) [](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic
96 lines (95 loc) • 3.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IgnoreManager = void 0;
const fs_1 = __importDefault(require("fs"));
const constants_1 = require("../constants");
const logger_1 = __importDefault(require("./logger"));
class IgnoreManager {
static getInstance() {
if (!IgnoreManager.instance) {
IgnoreManager.instance = new IgnoreManager();
}
return IgnoreManager.instance;
}
constructor(basePatterns = constants_1.IGNORED_FILE_PATTERNS) {
this.exactMatches = new Set();
this.regexPatterns = [];
this.gitignoreCache = new Map();
this.addPatterns(basePatterns);
}
parsePattern(pattern) {
if (!pattern || pattern.startsWith('#')) {
return { exactMatch: null, regex: null };
}
pattern = pattern.replace(/^\.\//, '').replace(/\/$/, '');
if (!pattern.includes('*') &&
!pattern.includes('?') &&
!pattern.includes('(?:')) {
return { exactMatch: pattern, regex: null };
}
const regexPattern = pattern
.replace(/\./g, '\\.')
.replace(/\*/g, '.*')
.replace(/\?/g, '.');
return { exactMatch: null, regex: new RegExp(`^${regexPattern}$`) };
}
addPatterns(patterns, relativePath = '') {
const normalizedRelativePath = relativePath
? relativePath.replace(/\\/g, '/').replace(/\/?$/, '/')
: '';
const uniquePatterns = [...new Set(patterns)];
uniquePatterns.forEach((pattern) => {
if (!pattern)
return;
const fullPattern = !pattern.startsWith('/')
? `${normalizedRelativePath}${pattern}`
: pattern.slice(1);
const parsed = this.parsePattern(fullPattern);
if (parsed.exactMatch) {
this.exactMatches.add(parsed.exactMatch);
}
else if (parsed.regex) {
const regexStr = parsed.regex.toString();
if (!this.regexPatterns.some((r) => r.toString() === regexStr)) {
this.regexPatterns.push(parsed.regex);
}
}
});
}
loadGitignore(gitignorePath, relativePath) {
try {
const content = fs_1.default.readFileSync(gitignorePath, 'utf8');
const patterns = content
.split('\n')
.map((line) => line.trim())
.filter((line) => line !== '' && !line.startsWith('#'));
this.addPatterns(patterns, relativePath);
}
catch (e) {
logger_1.default.error(`Error reading .gitignore at ${gitignorePath}:`, e);
}
}
isIgnored(filepath) {
const normalizedPath = filepath.replace(/\\/g, '/').replace(/^\.\//, '');
const cacheResult = this.gitignoreCache.get(normalizedPath);
if (cacheResult !== undefined) {
return cacheResult;
}
if (this.exactMatches.has(normalizedPath)) {
this.gitignoreCache.set(normalizedPath, true);
return true;
}
const isIgnored = this.regexPatterns.some((regex) => regex.test(normalizedPath));
this.gitignoreCache.set(normalizedPath, isIgnored);
return isIgnored;
}
clear() {
this.exactMatches.clear();
this.regexPatterns = [];
this.gitignoreCache.clear();
}
}
exports.IgnoreManager = IgnoreManager;