|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { app } from "electron"; |
| 4 | + |
| 5 | +const fsPromises = fs.promises; |
| 6 | + |
| 7 | +let dataDir: string | null = null; |
| 8 | + |
| 9 | +function isFileNotFoundError(error: unknown): boolean { |
| 10 | + return (error as NodeJS.ErrnoException).code === "ENOENT"; |
| 11 | +} |
| 12 | + |
| 13 | +function logAndThrowError(message: string, error: unknown): never { |
| 14 | + console.error(message, error); |
| 15 | + throw error; |
| 16 | +} |
| 17 | + |
| 18 | +async function cleanupTempFile(filePath: string): Promise<void> { |
| 19 | + try { |
| 20 | + await fsPromises.unlink(filePath); |
| 21 | + } catch { |
| 22 | + // Ignore cleanup errors |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +export function getDataDirectory(): string { |
| 27 | + if (!dataDir) { |
| 28 | + const userDataPath = app.getPath("userData"); |
| 29 | + dataDir = path.join(userDataPath, "data"); |
| 30 | + } |
| 31 | + return dataDir; |
| 32 | +} |
| 33 | + |
| 34 | +export async function ensureDataDirectory(): Promise<void> { |
| 35 | + const dir = getDataDirectory(); |
| 36 | + try { |
| 37 | + await fsPromises.mkdir(dir, { recursive: true }); |
| 38 | + } catch (error) { |
| 39 | + logAndThrowError("Failed to create data directory:", error); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export function getDataFilePath(filename: string): string { |
| 44 | + return path.join(getDataDirectory(), filename); |
| 45 | +} |
| 46 | + |
| 47 | +export async function readDataFile<T>(filename: string): Promise<T | null> { |
| 48 | + const filePath = getDataFilePath(filename); |
| 49 | + try { |
| 50 | + const content = await fsPromises.readFile(filePath, "utf-8"); |
| 51 | + return JSON.parse(content) as T; |
| 52 | + } catch (error) { |
| 53 | + if (isFileNotFoundError(error)) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + logAndThrowError(`Failed to read data file ${filename}:`, error); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export async function writeDataFile<T>( |
| 61 | + filename: string, |
| 62 | + data: T, |
| 63 | +): Promise<void> { |
| 64 | + await ensureDataDirectory(); |
| 65 | + const filePath = getDataFilePath(filename); |
| 66 | + const tempPath = `${filePath}.tmp`; |
| 67 | + |
| 68 | + try { |
| 69 | + await fsPromises.writeFile( |
| 70 | + tempPath, |
| 71 | + JSON.stringify(data, null, 2), |
| 72 | + "utf-8", |
| 73 | + ); |
| 74 | + await fsPromises.rename(tempPath, filePath); |
| 75 | + } catch (error) { |
| 76 | + await cleanupTempFile(tempPath); |
| 77 | + logAndThrowError(`Failed to write data file ${filename}:`, error); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export async function deleteDataFile(filename: string): Promise<void> { |
| 82 | + const filePath = getDataFilePath(filename); |
| 83 | + try { |
| 84 | + await fsPromises.unlink(filePath); |
| 85 | + } catch (error) { |
| 86 | + if (!isFileNotFoundError(error)) { |
| 87 | + logAndThrowError(`Failed to delete data file ${filename}:`, error); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +export async function clearDataDirectory(): Promise<void> { |
| 93 | + const dir = getDataDirectory(); |
| 94 | + try { |
| 95 | + const files = await fsPromises.readdir(dir); |
| 96 | + await Promise.all( |
| 97 | + files.map((file) => fsPromises.unlink(path.join(dir, file))), |
| 98 | + ); |
| 99 | + } catch (error) { |
| 100 | + if (!isFileNotFoundError(error)) { |
| 101 | + logAndThrowError("Failed to clear data directory:", error); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments