mirror of
https://github.com/artemkorsakov/project-euler-stats.git
synced 2026-07-22 05:42:55 +00:00
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
|
|
import { App, Plugin, TFile } from 'obsidian';
|
||
|
|
import { CacheData } from './types';
|
||
|
|
|
||
|
|
export async function saveDataToFile(filePath: string, data: CacheData): Promise<void> {
|
||
|
|
try {
|
||
|
|
const jsonData = JSON.stringify(data, null, 2);
|
||
|
|
await this.app.vault.adapter.write(filePath, jsonData);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Failed to save data:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function loadDataFromFile(filePath: string): Promise<CacheData | null> {
|
||
|
|
try {
|
||
|
|
const jsonData = await this.app.vault.adapter.read(filePath);
|
||
|
|
const rawData = JSON.parse(jsonData);
|
||
|
|
return CacheData.fromObject(rawData);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Failed to load data:', error);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function checkFileExists(filePath: string): Promise<boolean> {
|
||
|
|
return await this.app.vault.adapter.exists(filePath);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function ensureFolderExists(folderPath: string): Promise<void> {
|
||
|
|
if (!await this.app.vault.adapter.exists(folderPath)) {
|
||
|
|
await this.app.vault.adapter.mkdir(folderPath);
|
||
|
|
}
|
||
|
|
}
|