diff --git a/.gitmodules b/.gitmodules index e5067a8dc..ae53c18c5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "bada-frame"] path = ui url = https://github.com/ente-io/bada-frame - branch = main + branch = add-electron-types diff --git a/src/api/common.ts b/src/api/common.ts index ec3b7dff3..1e20bc1f1 100644 --- a/src/api/common.ts +++ b/src/api/common.ts @@ -1,7 +1,7 @@ import { ipcRenderer } from 'electron/renderer'; import { logError } from '../utils/logging'; -export const selectRootDirectory = async () => { +export const selectRootDirectory = async (): Promise => { try { return await ipcRenderer.invoke('select-dir'); } catch (e) { diff --git a/src/api/export.ts b/src/api/export.ts index 000c4714b..0fdf3677e 100644 --- a/src/api/export.ts +++ b/src/api/export.ts @@ -1,21 +1,14 @@ -import { - createDirectory, - doesPathExists, - readTextFile, - renameDirectory, - writeFile, - writeStream, -} from './../services/fs'; +import { readTextFile, writeStream } from './../services/fs'; import { ipcRenderer } from 'electron'; -import { logError } from '../utils/logging'; +import * as fs from 'promise-fs'; export const exists = (path: string) => { - return doesPathExists(path); + return fs.existsSync(path); }; export const checkExistsAndCreateCollectionDir = async (dirPath: string) => { - if (!doesPathExists(dirPath)) { - await createDirectory(dirPath); + if (!fs.existsSync(dirPath)) { + await fs.mkdir(dirPath); } }; @@ -23,8 +16,8 @@ export const checkExistsAndRename = async ( oldDirPath: string, newDirPath: string ) => { - if (doesPathExists(oldDirPath)) { - await renameDirectory(oldDirPath, newDirPath); + if (fs.existsSync(oldDirPath)) { + await fs.rename(oldDirPath, newDirPath); } }; @@ -36,24 +29,19 @@ export const saveStreamToDisk = ( }; export const saveFileToDisk = async (path: string, fileData: any) => { - await writeFile(path, fileData); + await fs.writeFile(path, fileData); }; export const getExportRecord = async (filePath: string) => { - try { - if (!(await doesPathExists(filePath))) { - return null; - } - const recordFile = await readTextFile(filePath); - return recordFile; - } catch (e) { - // ignore exportFile missing - logError(e, 'error while selecting files'); + if (!fs.existsSync(filePath)) { + return null; } + const recordFile = await readTextFile(filePath); + return recordFile; }; export const setExportRecord = async (filePath: string, data: string) => { - await writeFile(filePath, data); + await fs.writeFile(filePath, data); }; export const registerResumeExportListener = (resumeExport: () => void) => { diff --git a/src/api/safeStorage.ts b/src/api/safeStorage.ts index edfddcec3..35ce2478d 100644 --- a/src/api/safeStorage.ts +++ b/src/api/safeStorage.ts @@ -16,7 +16,7 @@ export async function setEncryptionKey(encryptionKey: string) { } } -export async function getEncryptionKey() { +export async function getEncryptionKey(): Promise { try { const b64EncryptedKey = safeStorageStore.get('encryptionKey'); if (b64EncryptedKey) { diff --git a/src/api/upload.ts b/src/api/upload.ts index 9f840b62a..90d70ae31 100644 --- a/src/api/upload.ts +++ b/src/api/upload.ts @@ -4,7 +4,6 @@ import path from 'path'; import StreamZip from 'node-stream-zip'; import { uploadStatusStore } from '../stores/upload.store'; import { ElectronFile, FILE_PATH_KEYS, FILE_PATH_TYPE } from '../types'; -import { logError } from '../utils/logging'; import { ipcRenderer } from 'electron'; async function getZipEntryAsElectronFile( @@ -90,48 +89,36 @@ export const getElectronFilesFromGoogleZip = async (filePath: string) => { return files; }; -export const showUploadDirsDialog = async () => { - try { - const filePaths: string[] = await ipcRenderer.invoke( - 'show-upload-dirs-dialog' - ); - const files = await Promise.all(filePaths.map(getElectronFile)); - return files; - } catch (e) { - logError(e, 'error while selecting folders'); - } +export const showUploadFilesDialog = async () => { + const filePaths: string[] = await ipcRenderer.invoke( + 'show-upload-files-dialog' + ); + const files = await Promise.all(filePaths.map(getElectronFile)); + return files; }; -export const showUploadFilesDialog = async () => { - try { - const filePaths: string[] = await ipcRenderer.invoke( - 'show-upload-files-dialog' - ); - const files = await Promise.all(filePaths.map(getElectronFile)); - return files; - } catch (e) { - logError(e, 'error while selecting files'); - } +export const showUploadDirsDialog = async () => { + const filePaths: string[] = await ipcRenderer.invoke( + 'show-upload-dirs-dialog' + ); + const files = await Promise.all(filePaths.map(getElectronFile)); + return files; }; export const showUploadZipDialog = async () => { - try { - const filePaths: string[] = await ipcRenderer.invoke( - 'show-upload-zip-dialog' - ); - const files: ElectronFile[] = []; + const filePaths: string[] = await ipcRenderer.invoke( + 'show-upload-zip-dialog' + ); - for (const filePath of filePaths) { - files.push(...(await getElectronFilesFromGoogleZip(filePath))); - } - - return { - zipPaths: filePaths, - files, - }; - } catch (e) { - logError(e, 'error while selecting zips'); + const files: ElectronFile[] = []; + for (const filePath of filePaths) { + files.push(...(await getElectronFilesFromGoogleZip(filePath))); } + + return { + zipPaths: filePaths, + files, + }; }; const getSavedPaths = (type: FILE_PATH_TYPE) => { diff --git a/src/services/fs.ts b/src/services/fs.ts index 8e593d32f..8f063877b 100644 --- a/src/services/fs.ts +++ b/src/services/fs.ts @@ -30,7 +30,6 @@ export const getFileStream = async (filePath: string) => { async pull(controller) { try { const buff = new Uint8Array(FILE_STREAM_CHUNK_SIZE); - // original types were not working correctly const bytesRead = (await fs.read( file, @@ -161,15 +160,6 @@ export async function doesFolderExists(dirPath: string) { .catch(() => false); } -export async function doesPathExists(dirPath: string) { - return await fs - .stat(dirPath) - .then((stats: fs.Stats) => { - return stats.isFile() || stats.isDirectory(); - }) - .catch(() => false); -} - export const convertBrowserStreamToNode = (fileStream: any) => { const reader = fileStream.getReader(); const rs = new Readable(); @@ -188,19 +178,7 @@ export const convertBrowserStreamToNode = (fileStream: any) => { return rs; }; -export async function createDirectory(dirPath: string) { - await fs.mkdir(dirPath); -} - -export async function renameDirectory(oldDirPath: string, newDirPath: string) { - await fs.rename(oldDirPath, newDirPath); -} - -export async function writeFile(filePath: string, fileData: any) { - await fs.writeFile(filePath, fileData); -} - -export function writeStream(filePath: string, fileStream: any) { +export function writeStream(filePath: string, fileStream: ReadableStream) { const writeable = fs.createWriteStream(filePath); const readable = convertBrowserStreamToNode(fileStream); readable.pipe(writeable);