Skip to content

Commit

Permalink
chore: Add fs abstraction for checkFileExists
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro-v committed Dec 8, 2023
1 parent 56b7786 commit 9aca37a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
35 changes: 18 additions & 17 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,82 @@
* @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/
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.
* @param path - The path to check.
* @returns {boolean} A boolean indicating whether the path is a directory.
*/
const isDirectory = (path: string): Promise<boolean> =>
global.core.api?.isDirectory(path);
const isDirectory = (path: string): Promise<boolean> => 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<any>} A Promise that resolves with the contents of the file.
*/
const readFile: (path: string) => Promise<any> = (path) =>
global.core.api?.readFile(path);
const readFile: (path: string) => Promise<any> = (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
* @param {string} path - The path of the directory to list files.
* @returns {Promise<any>} A Promise that resolves with the contents of the directory.
*/
const listFiles: (path: string) => Promise<any> = (path) =>
global.core.api?.listFiles(path);
const listFiles: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdir: (path: string) => Promise<any> = (path) =>
global.core.api?.mkdir(path);
const mkdir: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the directory is removed successfully.
*/
const rmdir: (path: string) => Promise<any> = (path) =>
global.core.api?.rmdir(path);
const rmdir: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the file is deleted.
*/
const deleteFile: (path: string) => Promise<any> = (path) =>
global.core.api?.deleteFile(path);
const deleteFile: (path: string) => Promise<any> = (path) => global.core.api?.deleteFile(path)

/**
* Appends data to a file at the specified path.
* @param path path to the file
* @param data data to append
*/
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) =>
global.core.api?.copyFile(src, dest);
global.core.api?.copyFile(src, dest)

/**
* Reads a file line by line.
* @param {string} path - The path of the file to read.
* @returns {Promise<any>} A promise that resolves to the lines of the file.
*/
const readLineByLine: (path: string) => Promise<any> = (path) =>
global.core.api?.readLineByLine(path);
global.core.api?.readLineByLine(path)

export const fs = {
isDirectory,
writeFile,
readFile,
checkFileExists,
listFiles,
mkdir,
rmdir,
deleteFile,
appendFile,
readLineByLine,
copyFile,
};
}
13 changes: 13 additions & 0 deletions electron/handlers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions electron/invokers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9aca37a

Please sign in to comment.