UNPKG

gitlify

Version:

A powerful CLI tool to analyze uncommitted git changes with detailed reports, function detection, and beautiful terminal output

130 lines (103 loc) 3.39 kB
const path = require('path'); class Config { constructor() { // File system limits this.maxFileSize = 10 * 1024 * 1024; // 10MB this.maxFilesToAnalyze = 100; this.supportedExtensions = ['.js', '.ts', '.jsx', '.tsx']; // Git settings this.gitTimeout = 5000; // 5 seconds this.gitMaxOutputSize = 50 * 1024 * 1024; // 50MB // Security settings this.allowedPathPatterns = [/^[a-zA-Z0-9\/\._-]+$/]; this.blockedPathPatterns = [/\.\./, /~/]; // Performance settings this.concurrentFileLimit = 5; this.cacheTTL = 5 * 60 * 1000; // 5 minutes // Output settings this.maxLineLength = 1000; this.maxFunctionNameLength = 100; // Load environment variables this.loadFromEnv(); // Validate configuration this.validate(); } loadFromEnv() { // Override with environment variables if present if (process.env.GITDIFFERS_MAX_FILE_SIZE) { this.maxFileSize = parseInt(process.env.GITDIFFERS_MAX_FILE_SIZE); } if (process.env.GITDIFFERS_GIT_TIMEOUT) { this.gitTimeout = parseInt(process.env.GITDIFFERS_GIT_TIMEOUT); } if (process.env.GITDIFFERS_CONCURRENT_LIMIT) { this.concurrentFileLimit = parseInt(process.env.GITDIFFERS_CONCURRENT_LIMIT); } if (process.env.GITDIFFERS_CACHE_TTL) { this.cacheTTL = parseInt(process.env.GITDIFFERS_CACHE_TTL); } } validate() { const errors = []; if (this.maxFileSize <= 0) { errors.push('maxFileSize must be positive'); } if (this.gitTimeout <= 0) { errors.push('gitTimeout must be positive'); } if (this.concurrentFileLimit <= 0) { errors.push('concurrentFileLimit must be positive'); } if (this.cacheTTL <= 0) { errors.push('cacheTTL must be positive'); } if (this.maxFilesToAnalyze <= 0) { errors.push('maxFilesToAnalyze must be positive'); } if (!Array.isArray(this.supportedExtensions)) { errors.push('supportedExtensions must be an array'); } if (errors.length > 0) { throw new Error(`Configuration validation failed: ${errors.join(', ')}`); } } isFileSupported(filePath) { const extension = path.extname(filePath).toLowerCase(); return this.supportedExtensions.includes(extension); } isPathAllowed(filePath) { // Check blocked patterns first for (const pattern of this.blockedPathPatterns) { if (pattern.test(filePath)) { return false; } } // Check allowed patterns for (const pattern of this.allowedPathPatterns) { if (pattern.test(filePath)) { return true; } } return false; } getGitOptions() { return { timeout: this.gitTimeout, maxBuffer: this.gitMaxOutputSize, encoding: 'utf8' }; } toJSON() { return { maxFileSize: this.maxFileSize, maxFilesToAnalyze: this.maxFilesToAnalyze, supportedExtensions: this.supportedExtensions, gitTimeout: this.gitTimeout, concurrentFileLimit: this.concurrentFileLimit, cacheTTL: this.cacheTTL }; } } // Singleton instance const config = new Config(); module.exports = config;