From 9aca37a30c1ee106a0ad7c1f4aab5b7f96b87edd Mon Sep 17 00:00:00 2001 From: hiro Date: Mon, 4 Dec 2023 12:09:49 +0700 Subject: [PATCH] chore: Add fs abstraction for checkFileExists --- core/src/fs.ts | 35 ++++++++++++++++++----------------- electron/handlers/fs.ts | 13 +++++++++++++ electron/invokers/fs.ts | 6 ++++++ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/core/src/fs.ts b/core/src/fs.ts index 4013479dd..99c8cbccf 100644 --- a/core/src/fs.ts +++ b/core/src/fs.ts @@ -5,52 +5,52 @@ * @returns {Promise} A Promise that resolves when the file is written successfully. */ const writeFile: (path: string, data: string) => Promise = (path, data) => - global.core.api?.writeFile(path, data); + global.core.api?.writeFile(path, data) /** * Checks whether the path is a directory. * @param path - The path to check. * @returns {boolean} A boolean indicating whether the path is a directory. */ -const isDirectory = (path: string): Promise => - global.core.api?.isDirectory(path); +const isDirectory = (path: string): Promise => global.core.api?.isDirectory(path) /** * Reads the contents of a file at the specified path. * @param {string} path - The path of the file to read. * @returns {Promise} A Promise that resolves with the contents of the file. */ -const readFile: (path: string) => Promise = (path) => - global.core.api?.readFile(path); +const readFile: (path: string) => Promise = (path) => global.core.api?.readFile(path) +/** + * Check whether the file exists + * @param {string} path + * @returns {boolean} A boolean indicating whether the path is a file. + */ +const checkFileExists = (path: string): Promise => global.core.api?.checkFileExists(path) /** * List the directory files * @param {string} path - The path of the directory to list files. * @returns {Promise} A Promise that resolves with the contents of the directory. */ -const listFiles: (path: string) => Promise = (path) => - global.core.api?.listFiles(path); +const listFiles: (path: string) => Promise = (path) => global.core.api?.listFiles(path) /** * Creates a directory at the specified path. * @param {string} path - The path of the directory to create. * @returns {Promise} A Promise that resolves when the directory is created successfully. */ -const mkdir: (path: string) => Promise = (path) => - global.core.api?.mkdir(path); +const mkdir: (path: string) => Promise = (path) => global.core.api?.mkdir(path) /** * Removes a directory at the specified path. * @param {string} path - The path of the directory to remove. * @returns {Promise} A Promise that resolves when the directory is removed successfully. */ -const rmdir: (path: string) => Promise = (path) => - global.core.api?.rmdir(path); +const rmdir: (path: string) => Promise = (path) => global.core.api?.rmdir(path) /** * Deletes a file from the local file system. * @param {string} path - The path of the file to delete. * @returns {Promise} A Promise that resolves when the file is deleted. */ -const deleteFile: (path: string) => Promise = (path) => - global.core.api?.deleteFile(path); +const deleteFile: (path: string) => Promise = (path) => global.core.api?.deleteFile(path) /** * Appends data to a file at the specified path. @@ -58,10 +58,10 @@ const deleteFile: (path: string) => Promise = (path) => * @param data data to append */ const appendFile: (path: string, data: string) => Promise = (path, data) => - global.core.api?.appendFile(path, data); + global.core.api?.appendFile(path, data) const copyFile: (src: string, dest: string) => Promise = (src, dest) => - global.core.api?.copyFile(src, dest); + global.core.api?.copyFile(src, dest) /** * Reads a file line by line. @@ -69,12 +69,13 @@ const copyFile: (src: string, dest: string) => Promise = (src, dest) => * @returns {Promise} A promise that resolves to the lines of the file. */ const readLineByLine: (path: string) => Promise = (path) => - global.core.api?.readLineByLine(path); + global.core.api?.readLineByLine(path) export const fs = { isDirectory, writeFile, readFile, + checkFileExists, listFiles, mkdir, rmdir, @@ -82,4 +83,4 @@ export const fs = { appendFile, readLineByLine, copyFile, -}; +} diff --git a/electron/handlers/fs.ts b/electron/handlers/fs.ts index 16cef6eb6..1e2df5c59 100644 --- a/electron/handlers/fs.ts +++ b/electron/handlers/fs.ts @@ -50,6 +50,19 @@ export function handleFsIPCs() { }) }) + /** + * Checks whether a file exists in the user data directory. + * @param event - The event object. + * @param path - The path of the file to check. + * @returns A promise that resolves with a boolean indicating whether the file exists. + */ + ipcMain.handle('checkFileExists', async (_event, path: string) => { + return new Promise((resolve, reject) => { + const fullPath = join(userSpacePath, path) + fs.existsSync(fullPath) ? resolve(true) : resolve(false) + }) + }) + /** * Writes data to a file in the user data directory. * @param event - The event object. diff --git a/electron/invokers/fs.ts b/electron/invokers/fs.ts index 309562ad6..e59eb4c86 100644 --- a/electron/invokers/fs.ts +++ b/electron/invokers/fs.ts @@ -27,6 +27,12 @@ export function fsInvokers() { */ readFile: (path: string) => ipcRenderer.invoke('readFile', path), + /** + * Reads a file at the specified path. + * @param {string} path - The path of the file to read. + */ + checkFileExists: (path: string) => ipcRenderer.invoke('checkFileExists', path), + /** * Writes data to a file at the specified path. * @param {string} path - The path of the file to write to.