UNPKG

@diullei/codeguardian

Version:

Open-source developer tool to validate and enforce architectural rules, especially for AI-generated code

153 lines 5.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigurationLoader = void 0; const fs_1 = require("fs"); const path = __importStar(require("path")); const glob_1 = require("glob"); const yaml = __importStar(require("yaml")); class ConfigurationLoader { static DEFAULT_PATTERNS = [ '**/*.codeguardian.yaml', '**/*.codeguardian.yml', '**/*.cg.yaml', '**/*.cg.yml', '.codeguardian.yaml', '.codeguardian.yml', '.cg.yaml', '.cg.yml', '.codeguardian/*.yaml', '.codeguardian/*.yml', '.codeguardian/*.cg.yaml', '.codeguardian/*.cg.yml', '.codeguardian/*.codeguardian.yaml', '.codeguardian/*.codeguardian.yml', ]; async getSkipDirectories(basePath) { const skipDirs = []; const cgignoreFiles = await (0, glob_1.glob)('**/.cg-ignore', { cwd: basePath, absolute: true, ignore: ['**/node_modules/**', '**/dist/**', '**/.git/**'], }); for (const cgignorePath of cgignoreFiles) { const dir = path.dirname(cgignorePath); const relativePath = path.relative(basePath, dir); skipDirs.push(`${relativePath}/**`); } return skipDirs; } async loadConfigurations(pattern, basePath = '.', excludePatterns) { const absoluteBasePath = path.resolve(basePath); let filePaths = []; const skipDirectories = await this.getSkipDirectories(absoluteBasePath); const defaultIgnore = ['**/node_modules/**', '**/dist/**', '**/.git/**']; const ignorePatterns = [...defaultIgnore, ...skipDirectories, ...(excludePatterns || [])]; if (pattern) { const absolutePattern = path.isAbsolute(pattern) ? pattern : path.join(absoluteBasePath, pattern); try { const stats = await fs_1.promises.stat(absolutePattern); if (stats.isFile()) { filePaths = [absolutePattern]; } } catch { filePaths = await (0, glob_1.glob)(pattern, { cwd: absoluteBasePath, absolute: true, ignore: ignorePatterns, }); } } else { const allFiles = []; for (const defaultPattern of ConfigurationLoader.DEFAULT_PATTERNS) { const files = await (0, glob_1.glob)(defaultPattern, { cwd: absoluteBasePath, absolute: true, ignore: ignorePatterns, }); allFiles.push(...files); } filePaths = [...new Set(allFiles)]; } if (filePaths.length === 0) { throw new Error(pattern ? `No configuration files found matching pattern: ${pattern}` : `No configuration files found. Looked for: ${ConfigurationLoader.DEFAULT_PATTERNS.join(', ')}`); } const configurations = []; for (const filePath of filePaths) { try { const content = await fs_1.promises.readFile(filePath, 'utf-8'); const config = yaml.parse(content); configurations.push({ path: filePath, content: config, }); } catch (error) { throw new Error(`Failed to load configuration from ${filePath}: ${error instanceof Error ? error.message : String(error)}`); } } return configurations; } mergeConfigurations(configurations) { if (configurations.length === 0) { throw new Error('No configurations to merge'); } if (configurations.length === 1) { const firstConfig = configurations[0]; if (!firstConfig) { throw new Error('No configuration found'); } return firstConfig.content; } const mergedRules = configurations.map(config => ({ ...config.content, type: config.content.type || 'unknown', })); return { id: 'merged-configuration', description: `Merged configuration from ${configurations.length} files`, type: 'all_of', rules: mergedRules, }; } } exports.ConfigurationLoader = ConfigurationLoader; //# sourceMappingURL=ConfigurationLoader.js.map