fu-orm-code-generator
Version:
A Model Context Protocol (MCP) server for MyBatis code generation with enterprise-grade layered architecture
113 lines (97 loc) • 2.95 kB
JavaScript
/**
* 配置仓储具体实现
*/
import { ConfigRepository } from '../../domain/repositories/ConfigRepository.js';
import { Config } from '../../domain/entities/Config.js';
import { config } from '../../config/index.js';
import { FileSystemError } from '../../common/errors/index.js';
export class ConfigRepositoryImpl extends ConfigRepository {
constructor(fileSystem) {
super();
this.fileSystem = fileSystem;
this.configPath = config.paths.config;
}
async read() {
try {
if (!await this.fileSystem.exists(this.configPath)) {
return Config.createDefault();
}
const content = await this.fileSystem.readFile(this.configPath);
const data = JSON.parse(content);
return Config.fromJSON(data);
} catch (error) {
if (error.code === 'ENOENT') {
return Config.createDefault();
}
throw new FileSystemError('readConfig', this.configPath, error);
}
}
async save(configEntity) {
try {
const data = configEntity.toJSON();
const content = JSON.stringify(data, null, 2);
await this.fileSystem.writeFile(this.configPath, content, { overwrite: true });
} catch (error) {
throw new FileSystemError('saveConfig', this.configPath, error);
}
}
async exists() {
try {
return await this.fileSystem.exists(this.configPath);
} catch (error) {
throw new FileSystemError('checkConfigExists', this.configPath, error);
}
}
async initialize() {
try {
const defaultConfig = Config.createDefault();
await this.save(defaultConfig);
return defaultConfig;
} catch (error) {
throw new FileSystemError('initializeConfig', this.configPath, error);
}
}
async backup(backupPath) {
throw new Error('Backup functionality not implemented yet');
}
async restore(backupPath) {
throw new Error('Restore functionality not implemented yet');
}
async reset() {
try {
const defaultConfig = Config.createDefault();
await this.save(defaultConfig);
return defaultConfig;
} catch (error) {
throw new FileSystemError('resetConfig', this.configPath, error);
}
}
async validate() {
try {
const configEntity = await this.read();
return configEntity instanceof Config;
} catch (error) {
return false;
}
}
async getInfo() {
try {
if (!await this.exists()) {
return null;
}
const stats = await this.fileSystem.getStats(this.configPath);
return {
path: this.configPath,
size: stats.size,
createdAt: stats.birthtime,
modifiedAt: stats.mtime
};
} catch (error) {
throw new FileSystemError('getConfigInfo', this.configPath, error);
}
}
async watch(callback) {
// 简化实现,实际项目中可以使用 fs.watchFile
throw new Error('Watch functionality not implemented yet');
}
}