refactor: separate config service
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
EMPTY_PROJECT_CONFIG,
|
||||
getProjectConfigPath,
|
||||
type ProjectConfig,
|
||||
} from "@/model/projectConfig";
|
||||
import { IConfigProvider, IFileSystem } from "@/ports";
|
||||
import { parseProjectConfig } from "./parseProjectConfig";
|
||||
import { serializeProjectConfig } from "./serializeProjectConfig";
|
||||
|
||||
export class ProjectConfigService {
|
||||
#fs: IFileSystem;
|
||||
#config: IConfigProvider;
|
||||
|
||||
constructor(fs: IFileSystem, config: IConfigProvider) {
|
||||
this.#fs = fs;
|
||||
this.#config = config;
|
||||
}
|
||||
|
||||
async load(): Promise<ProjectConfig> {
|
||||
const folderPath = this.#config.getProjectTaskPath();
|
||||
if (!folderPath) return { ...EMPTY_PROJECT_CONFIG };
|
||||
|
||||
const path = getProjectConfigPath(folderPath);
|
||||
try {
|
||||
const content = await this.#fs.readFile(path);
|
||||
const parsed = parseProjectConfig(content);
|
||||
if (parsed.isErr()) return { ...EMPTY_PROJECT_CONFIG };
|
||||
return parsed.value;
|
||||
} catch {
|
||||
return { ...EMPTY_PROJECT_CONFIG };
|
||||
}
|
||||
}
|
||||
|
||||
async merge(partial: Partial<ProjectConfig>): Promise<void> {
|
||||
const folderPath = this.#config.getProjectTaskPath();
|
||||
if (!folderPath) return;
|
||||
|
||||
const current = await this.load();
|
||||
const merged: ProjectConfig = { ...current, ...partial };
|
||||
const content = serializeProjectConfig(merged);
|
||||
await this.#fs.writeFile(getProjectConfigPath(folderPath), content);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user