chore: Add fs abstraction for checkFileExists

This commit is contained in:
hiro 2023-12-04 12:09:49 +07:00
parent 56b778675f
commit 9aca37a30c
3 changed files with 37 additions and 17 deletions

View File

@ -5,52 +5,52 @@
* @returns {Promise<any>} A Promise that resolves when the file is written successfully. * @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/ */
const writeFile: (path: string, data: string) => Promise<any> = (path, data) => const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
global.core.api?.writeFile(path, data); global.core.api?.writeFile(path, data)
/** /**
* Checks whether the path is a directory. * Checks whether the path is a directory.
* @param path - The path to check. * @param path - The path to check.
* @returns {boolean} A boolean indicating whether the path is a directory. * @returns {boolean} A boolean indicating whether the path is a directory.
*/ */
const isDirectory = (path: string): Promise<boolean> => const isDirectory = (path: string): Promise<boolean> => global.core.api?.isDirectory(path)
global.core.api?.isDirectory(path);
/** /**
* Reads the contents of a file at the specified path. * Reads the contents of a file at the specified path.
* @param {string} path - The path of the file to read. * @param {string} path - The path of the file to read.
* @returns {Promise<any>} A Promise that resolves with the contents of the file. * @returns {Promise<any>} A Promise that resolves with the contents of the file.
*/ */
const readFile: (path: string) => Promise<any> = (path) => const readFile: (path: string) => Promise<any> = (path) => global.core.api?.readFile(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<boolean> => global.core.api?.checkFileExists(path)
/** /**
* List the directory files * List the directory files
* @param {string} path - The path of the directory to list files. * @param {string} path - The path of the directory to list files.
* @returns {Promise<any>} A Promise that resolves with the contents of the directory. * @returns {Promise<any>} A Promise that resolves with the contents of the directory.
*/ */
const listFiles: (path: string) => Promise<any> = (path) => const listFiles: (path: string) => Promise<any> = (path) => global.core.api?.listFiles(path)
global.core.api?.listFiles(path);
/** /**
* Creates a directory at the specified path. * Creates a directory at the specified path.
* @param {string} path - The path of the directory to create. * @param {string} path - The path of the directory to create.
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully. * @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
*/ */
const mkdir: (path: string) => Promise<any> = (path) => const mkdir: (path: string) => Promise<any> = (path) => global.core.api?.mkdir(path)
global.core.api?.mkdir(path);
/** /**
* Removes a directory at the specified path. * Removes a directory at the specified path.
* @param {string} path - The path of the directory to remove. * @param {string} path - The path of the directory to remove.
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully. * @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
*/ */
const rmdir: (path: string) => Promise<any> = (path) => const rmdir: (path: string) => Promise<any> = (path) => global.core.api?.rmdir(path)
global.core.api?.rmdir(path);
/** /**
* Deletes a file from the local file system. * Deletes a file from the local file system.
* @param {string} path - The path of the file to delete. * @param {string} path - The path of the file to delete.
* @returns {Promise<any>} A Promise that resolves when the file is deleted. * @returns {Promise<any>} A Promise that resolves when the file is deleted.
*/ */
const deleteFile: (path: string) => Promise<any> = (path) => const deleteFile: (path: string) => Promise<any> = (path) => global.core.api?.deleteFile(path)
global.core.api?.deleteFile(path);
/** /**
* Appends data to a file at the specified path. * Appends data to a file at the specified path.
@ -58,10 +58,10 @@ const deleteFile: (path: string) => Promise<any> = (path) =>
* @param data data to append * @param data data to append
*/ */
const appendFile: (path: string, data: string) => Promise<any> = (path, data) => const appendFile: (path: string, data: string) => Promise<any> = (path, data) =>
global.core.api?.appendFile(path, data); global.core.api?.appendFile(path, data)
const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) => const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
global.core.api?.copyFile(src, dest); global.core.api?.copyFile(src, dest)
/** /**
* Reads a file line by line. * Reads a file line by line.
@ -69,12 +69,13 @@ const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
* @returns {Promise<any>} A promise that resolves to the lines of the file. * @returns {Promise<any>} A promise that resolves to the lines of the file.
*/ */
const readLineByLine: (path: string) => Promise<any> = (path) => const readLineByLine: (path: string) => Promise<any> = (path) =>
global.core.api?.readLineByLine(path); global.core.api?.readLineByLine(path)
export const fs = { export const fs = {
isDirectory, isDirectory,
writeFile, writeFile,
readFile, readFile,
checkFileExists,
listFiles, listFiles,
mkdir, mkdir,
rmdir, rmdir,
@ -82,4 +83,4 @@ export const fs = {
appendFile, appendFile,
readLineByLine, readLineByLine,
copyFile, copyFile,
}; }

View File

@ -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. * Writes data to a file in the user data directory.
* @param event - The event object. * @param event - The event object.

View File

@ -27,6 +27,12 @@ export function fsInvokers() {
*/ */
readFile: (path: string) => ipcRenderer.invoke('readFile', path), 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. * Writes data to a file at the specified path.
* @param {string} path - The path of the file to write to. * @param {string} path - The path of the file to write to.