|
| 1 | +import { app, ipcMain } from "electron"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import { join } from "path"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Handles file system operations. |
| 7 | + */ |
| 8 | +export function handleFs() { |
| 9 | + /** |
| 10 | + * Reads a file from the user data directory. |
| 11 | + * @param event - The event object. |
| 12 | + * @param path - The path of the file to read. |
| 13 | + * @returns A promise that resolves with the contents of the file. |
| 14 | + */ |
| 15 | + ipcMain.handle("readFile", async (event, path: string): Promise<string> => { |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + fs.readFile(join(app.getPath("userData"), path), "utf8", (err, data) => { |
| 18 | + if (err) { |
| 19 | + reject(err); |
| 20 | + } else { |
| 21 | + resolve(data); |
| 22 | + } |
| 23 | + }); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + /** |
| 28 | + * Writes data to a file in the user data directory. |
| 29 | + * @param event - The event object. |
| 30 | + * @param path - The path of the file to write to. |
| 31 | + * @param data - The data to write to the file. |
| 32 | + * @returns A promise that resolves when the file has been written. |
| 33 | + */ |
| 34 | + ipcMain.handle( |
| 35 | + "writeFile", |
| 36 | + async (event, path: string, data: string): Promise<void> => { |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + fs.writeFile( |
| 39 | + join(app.getPath("userData"), path), |
| 40 | + data, |
| 41 | + "utf8", |
| 42 | + (err) => { |
| 43 | + if (err) { |
| 44 | + reject(err); |
| 45 | + } else { |
| 46 | + resolve(); |
| 47 | + } |
| 48 | + } |
| 49 | + ); |
| 50 | + }); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a directory in the user data directory. |
| 56 | + * @param event - The event object. |
| 57 | + * @param path - The path of the directory to create. |
| 58 | + * @returns A promise that resolves when the directory has been created. |
| 59 | + */ |
| 60 | + ipcMain.handle("mkdir", async (event, path: string): Promise<void> => { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + fs.mkdir( |
| 63 | + join(app.getPath("userData"), path), |
| 64 | + { recursive: true }, |
| 65 | + (err) => { |
| 66 | + if (err) { |
| 67 | + reject(err); |
| 68 | + } else { |
| 69 | + resolve(); |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + /** |
| 77 | + * Lists the files in a directory in the user data directory. |
| 78 | + * @param event - The event object. |
| 79 | + * @param path - The path of the directory to list files from. |
| 80 | + * @returns A promise that resolves with an array of file names. |
| 81 | + */ |
| 82 | + ipcMain.handle( |
| 83 | + "listFiles", |
| 84 | + async (event, path: string): Promise<string[]> => { |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + fs.readdir(join(app.getPath("userData"), path), (err, files) => { |
| 87 | + if (err) { |
| 88 | + reject(err); |
| 89 | + } else { |
| 90 | + resolve(files); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + } |
| 95 | + ); |
| 96 | +} |
0 commit comments