UNPKG

@felixgeelhaar/cclint

Version:

A comprehensive linter for CLAUDE.md files with multi-language code block validation

77 lines 2.71 kB
import { readFileSync, existsSync } from 'fs'; import { join, dirname } from 'path'; import { defaultConfig } from '../domain/Config.js'; export class ConfigLoader { static CONFIG_FILES = [ '.cclintrc.json', '.cclintrc.js', 'cclint.config.js', 'package.json', ]; static load(startDir = process.cwd()) { const configPath = this.findConfigFile(startDir); if (!configPath) { return defaultConfig; } try { const config = this.loadConfigFile(configPath); return this.mergeWithDefaults(config); } catch (error) { console.warn(`Warning: Failed to load config from ${configPath}:`, error instanceof Error ? error.message : error); return defaultConfig; } } static findConfigFile(startDir) { let currentDir = startDir; while (currentDir !== dirname(currentDir)) { for (const configFile of this.CONFIG_FILES) { const configPath = join(currentDir, configFile); if (existsSync(configPath)) { return configPath; } } currentDir = dirname(currentDir); } return null; } static loadConfigFile(configPath) { if (configPath.endsWith('package.json')) { const packageJson = JSON.parse(readFileSync(configPath, 'utf8')); return packageJson.cclint || {}; } if (configPath.endsWith('.json')) { return JSON.parse(readFileSync(configPath, 'utf8')); } // For .js files, we'd need dynamic import, but keeping it simple for now throw new Error('JavaScript config files not yet supported'); } static mergeWithDefaults(config) { const merged = { ...defaultConfig, ...config, rules: { ...defaultConfig.rules, ...config.rules, }, }; // Deep merge rule options if (config.rules) { for (const [ruleName, ruleConfig] of Object.entries(config.rules)) { if (ruleConfig && defaultConfig.rules[ruleName]) { merged.rules[ruleName] = { ...defaultConfig.rules[ruleName], ...ruleConfig, options: { ...(defaultConfig.rules[ruleName]?.options || {}), ...(ruleConfig.options || {}), }, }; } } } return merged; } } //# sourceMappingURL=ConfigLoader.js.map