|
| 1 | +import * as fs from 'fs/promises' |
| 2 | +import * as yaml from 'js-yaml' |
| 3 | +import * as path from 'path' |
| 4 | + |
| 5 | +/** |
| 6 | + * Check if a file exists |
| 7 | + * |
| 8 | + * @param filePath - Path to the file to check |
| 9 | + * @returns True if the file exists, false otherwise |
| 10 | + */ |
| 11 | +async function fileExists(filePath: string): Promise<boolean> { |
| 12 | + try { |
| 13 | + await fs.access(filePath) |
| 14 | + return true |
| 15 | + } catch { |
| 16 | + return false |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Interface for the coding guidelines configuration |
| 22 | + */ |
| 23 | +export interface CodingGuidelinesConfig { |
| 24 | + codingGuidelines: string[] |
| 25 | + reviewSettings: Record<string, unknown> |
| 26 | + [key: string]: unknown // Allow for future configuration options |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Default coding guidelines configuration |
| 31 | + */ |
| 32 | +const DEFAULT_CONFIG: CodingGuidelinesConfig = { |
| 33 | + codingGuidelines: [ |
| 34 | + 'Test coverage: Critical code requires 100% test coverage; non-critical paths require 60% coverage.', |
| 35 | + 'Naming: Use semantically significant names for functions, classes, and parameters.', |
| 36 | + 'Comments: Add comments only for complex code; simple code should be self-explanatory.', |
| 37 | + 'Documentation: Public functions must have concise docstrings explaining purpose and return values.' |
| 38 | + ], |
| 39 | + reviewSettings: { |
| 40 | + // Future configuration options can be added here |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Reads the .revu.yml configuration file from the specified path |
| 46 | + * |
| 47 | + * @param configPath - Path to the .revu.yml configuration file |
| 48 | + * @returns The parsed configuration object |
| 49 | + */ |
| 50 | +export async function readConfig( |
| 51 | + configPath = path.join(process.cwd(), '.revu.yml') |
| 52 | +): Promise<CodingGuidelinesConfig> { |
| 53 | + try { |
| 54 | + // Check if the config file exists |
| 55 | + const exists = await fileExists(configPath) |
| 56 | + if (!exists) { |
| 57 | + console.log('No .revu.yml configuration file found, using defaults') |
| 58 | + return DEFAULT_CONFIG |
| 59 | + } |
| 60 | + |
| 61 | + // Read and parse the YAML file |
| 62 | + const configContent = await fs.readFile(configPath, 'utf-8') |
| 63 | + const config = yaml.load(configContent) as CodingGuidelinesConfig |
| 64 | + |
| 65 | + // Merge with default config to ensure all required fields exist |
| 66 | + return mergeConfigs(DEFAULT_CONFIG, config) |
| 67 | + } catch (error) { |
| 68 | + console.error('Error reading .revu.yml configuration file:', error) |
| 69 | + return DEFAULT_CONFIG |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Gets the default configuration |
| 75 | + * |
| 76 | + * @returns The default configuration object |
| 77 | + */ |
| 78 | +export function getDefaultConfig(): CodingGuidelinesConfig { |
| 79 | + return { ...DEFAULT_CONFIG } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Gets the coding guidelines from the configuration |
| 84 | + * |
| 85 | + * @param config - The configuration object |
| 86 | + * @returns The coding guidelines as a formatted string |
| 87 | + */ |
| 88 | +export function formatCodingGuidelines(config: CodingGuidelinesConfig): string { |
| 89 | + if ( |
| 90 | + !config.codingGuidelines || |
| 91 | + !Array.isArray(config.codingGuidelines) || |
| 92 | + config.codingGuidelines.length === 0 |
| 93 | + ) { |
| 94 | + return 'No specific coding guidelines defined.' |
| 95 | + } |
| 96 | + |
| 97 | + return config.codingGuidelines |
| 98 | + .map((guideline, index) => `${index + 1}. ${guideline}`) |
| 99 | + .join('\n') |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Merges two configuration objects, with the user config overriding the default config |
| 104 | + * for any keys that exist in both. |
| 105 | + * |
| 106 | + * @param defaultConfig - The default configuration object |
| 107 | + * @param userConfig - The user configuration object |
| 108 | + * @returns The merged configuration object |
| 109 | + */ |
| 110 | +export function mergeConfigs( |
| 111 | + defaultConfig: CodingGuidelinesConfig, |
| 112 | + userConfig: Partial<CodingGuidelinesConfig> |
| 113 | +): CodingGuidelinesConfig { |
| 114 | + // Start with a copy of the default config |
| 115 | + const result = { ...defaultConfig } |
| 116 | + |
| 117 | + // If userConfig is not an object or is null, return the default |
| 118 | + if (!userConfig || typeof userConfig !== 'object') { |
| 119 | + return result |
| 120 | + } |
| 121 | + |
| 122 | + // Iterate through the keys in the user config |
| 123 | + for (const key in userConfig) { |
| 124 | + const userValue = userConfig[key] |
| 125 | + const defaultValue = defaultConfig[key] |
| 126 | + |
| 127 | + // If both values are objects, recursively merge them |
| 128 | + if ( |
| 129 | + userValue && |
| 130 | + typeof userValue === 'object' && |
| 131 | + !Array.isArray(userValue) && |
| 132 | + defaultValue && |
| 133 | + typeof defaultValue === 'object' && |
| 134 | + !Array.isArray(defaultValue) |
| 135 | + ) { |
| 136 | + // Use type assertion to handle nested objects |
| 137 | + result[key] = mergeConfigs( |
| 138 | + defaultValue as CodingGuidelinesConfig, |
| 139 | + userValue as Partial<CodingGuidelinesConfig> |
| 140 | + ) |
| 141 | + } else { |
| 142 | + // Otherwise, use the user value |
| 143 | + result[key] = userValue |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return result |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Gets the coding guidelines from the configuration |
| 152 | + * |
| 153 | + * @param repoPath - Optional path to the repository |
| 154 | + * @returns The coding guidelines as a formatted string |
| 155 | + */ |
| 156 | +export async function getCodingGuidelines(repoPath?: string): Promise<string> { |
| 157 | + let configPath = path.join(process.cwd(), '.revu.yml') |
| 158 | + |
| 159 | + // If a repository path is provided, check for a .revu.yml file there |
| 160 | + if (repoPath) { |
| 161 | + const repoConfigPath = path.join(repoPath, '.revu.yml') |
| 162 | + const exists = await fileExists(repoConfigPath) |
| 163 | + if (exists) { |
| 164 | + configPath = repoConfigPath |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + const config = await readConfig(configPath) |
| 169 | + return formatCodingGuidelines(config) |
| 170 | +} |
0 commit comments