mirror of
https://github.com/andy-stack/vaultkeeper-ai.git
synced 2026-07-22 06:42:03 +00:00
Add Exception helper class for consistent error handling and logging. Replace throw statements and console.error calls with Exception methods. Update service methods to return Error | T instead of mixed success/failure objects. Improve type safety in Claude.extractContents with explicit return type. Add WikiLinks helper to VaultCacheService for managing wiki link references. Update unit tests.
90 lines
No EOL
4.4 KiB
TypeScript
90 lines
No EOL
4.4 KiB
TypeScript
import { TAbstractFile, TFile, TFolder } from "obsidian";
|
|
import { Resolve } from "./DependencyService";
|
|
import { Services } from "./Services";
|
|
import type { VaultService } from "./VaultService";
|
|
import type { ISearchMatch } from "../Helpers/SearchTypes";
|
|
import { Exception } from "Helpers/Exception";
|
|
|
|
export class FileSystemService {
|
|
|
|
private readonly vaultService: VaultService;
|
|
|
|
public constructor() {
|
|
this.vaultService = Resolve<VaultService>(Services.VaultService);
|
|
}
|
|
|
|
public async readFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<string | Error> {
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
|
if (file && file instanceof TFile) {
|
|
return await this.vaultService.read(file, allowAccessToPluginRoot);
|
|
}
|
|
return Exception.new(`Path is a folder, not a file: ${filePath}`);
|
|
}
|
|
|
|
public async writeFile(filePath: string, content: string, allowAccessToPluginRoot: boolean = false): Promise<TFile | Error> {
|
|
try {
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
|
if (file == null || !(file instanceof TFile)) {
|
|
return await this.vaultService.create(filePath, content, allowAccessToPluginRoot);
|
|
}
|
|
return await this.vaultService.modify(file, content, allowAccessToPluginRoot);
|
|
}
|
|
catch (error) {
|
|
Exception.log(error);
|
|
return Exception.new(error);
|
|
}
|
|
}
|
|
|
|
public async deleteFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<Error | void> {
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
|
|
|
if (!file) {
|
|
return Exception.new(`File does not exist: ${filePath}`);
|
|
}
|
|
|
|
return await this.vaultService.delete(file, allowAccessToPluginRoot);
|
|
}
|
|
|
|
public async moveFile(sourcePath: string, destinationPath: string, allowAccessToPluginRoot: boolean = false): Promise<void | Error> {
|
|
return await this.vaultService.move(sourcePath, destinationPath, allowAccessToPluginRoot);
|
|
}
|
|
|
|
public async listFilesInDirectory(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TFile[]> {
|
|
return await this.vaultService.listFilesInDirectory(dirPath, recursive, allowAccessToPluginRoot);
|
|
}
|
|
|
|
public async listFoldersInDirectory(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TFolder[]> {
|
|
return await this.vaultService.listFoldersInDirectory(dirPath, recursive, allowAccessToPluginRoot);
|
|
}
|
|
|
|
public async listDirectoryContents(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TAbstractFile[]> {
|
|
return await this.vaultService.listDirectoryContents(dirPath, recursive, allowAccessToPluginRoot);
|
|
}
|
|
|
|
public async readObjectFromFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<object | Error> {
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
|
if (file && file instanceof TFile) {
|
|
const result = await this.vaultService.read(file, allowAccessToPluginRoot);
|
|
return typeof result === "string" ? JSON.parse(result) as object : result;
|
|
}
|
|
return Exception.new(`File not found: ${filePath}`);
|
|
}
|
|
|
|
public async writeObjectToFile(filePath: string, data: object, allowAccessToPluginRoot: boolean = false): Promise<TFile | Error> {
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
|
|
|
let result: TFile | Error;
|
|
if (file && file instanceof TFile) {
|
|
result = await this.vaultService.modify(file, JSON.stringify(data, null, 4), allowAccessToPluginRoot);
|
|
}
|
|
else {
|
|
result = await this.vaultService.create(filePath, JSON.stringify(data, null, 4), allowAccessToPluginRoot);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async searchVaultFiles(searchTerm: string, allowAccessToPluginRoot: boolean = false): Promise<ISearchMatch[]> {
|
|
return await this.vaultService.searchVaultFiles(searchTerm, allowAccessToPluginRoot);
|
|
}
|
|
} |