This commit is contained in:
99
src/config/configLoader.ts
Normal file
99
src/config/configLoader.ts
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
import { ContextConfig } from '../types';
|
||||||
|
|
||||||
|
export class ConfigLoader {
|
||||||
|
private static defaultConfig: ContextConfig = {
|
||||||
|
includes: [
|
||||||
|
'**/*.ts',
|
||||||
|
'**/*.js',
|
||||||
|
'**/*.py',
|
||||||
|
'**/*.go',
|
||||||
|
'**/*.rs',
|
||||||
|
'**/*.java',
|
||||||
|
'**/*.c',
|
||||||
|
'**/*.cpp',
|
||||||
|
'**/*.h',
|
||||||
|
'**/*.hpp',
|
||||||
|
],
|
||||||
|
excludes: [
|
||||||
|
'node_modules/**',
|
||||||
|
'dist/**',
|
||||||
|
'build/**',
|
||||||
|
'.git/**',
|
||||||
|
'.DS_Store',
|
||||||
|
'*.log',
|
||||||
|
],
|
||||||
|
outputFormat: 'json',
|
||||||
|
template: 'default',
|
||||||
|
outputFile: 'ai-context.json',
|
||||||
|
analyzeConventions: true,
|
||||||
|
includeDevDependencies: false,
|
||||||
|
respectGitignore: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
static async load(
|
||||||
|
configPath?: string
|
||||||
|
): Promise<ContextConfig> {
|
||||||
|
if (!configPath) {
|
||||||
|
const localConfig = path.join(process.cwd(), '.ai-context-config.json');
|
||||||
|
if (await this.fileExists(localConfig)) {
|
||||||
|
return this.loadFromFile(localConfig);
|
||||||
|
}
|
||||||
|
return { ...this.defaultConfig };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await this.fileExists(configPath)) {
|
||||||
|
return this.loadFromFile(configPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Config file not found: ${configPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async loadFromFile(
|
||||||
|
configPath: string
|
||||||
|
): Promise<ContextConfig> {
|
||||||
|
const content = await fs.promises.readFile(configPath, 'utf-8');
|
||||||
|
const userConfig = JSON.parse(content);
|
||||||
|
|
||||||
|
return this.mergeConfig(userConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static mergeConfig(
|
||||||
|
userConfig: Partial<ContextConfig>
|
||||||
|
): ContextConfig {
|
||||||
|
return {
|
||||||
|
includes: userConfig.includes ?? this.defaultConfig.includes,
|
||||||
|
excludes: userConfig.excludes ?? this.defaultConfig.excludes,
|
||||||
|
outputFormat:
|
||||||
|
userConfig.outputFormat ?? this.defaultConfig.outputFormat,
|
||||||
|
template: userConfig.template ?? this.defaultConfig.template,
|
||||||
|
outputFile:
|
||||||
|
userConfig.outputFile ?? this.defaultConfig.outputFile,
|
||||||
|
analyzeConventions:
|
||||||
|
userConfig.analyzeConventions ?? this.defaultConfig.analyzeConventions,
|
||||||
|
includeDevDependencies:
|
||||||
|
userConfig.includeDevDependencies ??
|
||||||
|
this.defaultConfig.includeDevDependencies,
|
||||||
|
respectGitignore:
|
||||||
|
userConfig.respectGitignore ?? this.defaultConfig.respectGitignore,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async fileExists(filePath: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await fs.promises.access(filePath, fs.constants.F_OK);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static save(
|
||||||
|
config: ContextConfig,
|
||||||
|
outputPath: string
|
||||||
|
): void {
|
||||||
|
const content = JSON.stringify(config, null, 2);
|
||||||
|
fs.writeFileSync(outputPath, content, 'utf-8');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user