From 341c1bb63953aa8169d5f6ca82831424e68a8cdc Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 19 May 2022 23:55:01 +0530 Subject: [PATCH] incr: remove fs and path calls --- main/helpers.ts | 46 +++++++++++++++++++++++++ main/registerIpcMainActionListeners.ts | 9 +++++ src/components/Controls/AttachImage.vue | 26 +++++++------- src/components/Sidebar.vue | 8 ++--- src/pages/DatabaseSelector.vue | 44 ++++++----------------- utils/messages.ts | 1 + utils/translationHelpers.ts | 16 ++++++--- 7 files changed, 95 insertions(+), 55 deletions(-) create mode 100644 main/helpers.ts diff --git a/main/helpers.ts b/main/helpers.ts new file mode 100644 index 00000000..6e674b17 --- /dev/null +++ b/main/helpers.ts @@ -0,0 +1,46 @@ +import { constants } from 'fs'; +import fs from 'fs/promises'; +import { ConfigFile, ConfigKeys } from 'fyo/core/types'; +import config from 'utils/config'; + +interface ConfigFilesWithModified extends ConfigFile { + modified: string; +} + +export async function setAndGetCleanedConfigFiles() { + const files = config.get(ConfigKeys.Files, []) as ConfigFile[]; + + const cleanedFileMap: Map = new Map(); + for (const file of files) { + const exists = await fs + .access(file.dbPath, constants.W_OK) + .then(() => true) + .catch(() => false); + + const key = `${file.companyName}-${file.dbPath}`; + if (!exists || cleanedFileMap.has(key)) { + continue; + } + + cleanedFileMap.set(key, file); + } + + const cleanedFiles = Array.from(cleanedFileMap.values()); + config.set(ConfigKeys.Files, cleanedFiles); + return cleanedFiles; +} + +export async function getConfigFilesWithModified(files: ConfigFile[]) { + const filesWithModified: ConfigFilesWithModified[] = []; + for (const { dbPath, id, companyName } of files) { + const { mtime } = await fs.stat(dbPath); + filesWithModified.push({ + id, + dbPath, + companyName, + modified: mtime.toISOString(), + }); + } + + return filesWithModified; +} diff --git a/main/registerIpcMainActionListeners.ts b/main/registerIpcMainActionListeners.ts index a866446b..ecc11d76 100644 --- a/main/registerIpcMainActionListeners.ts +++ b/main/registerIpcMainActionListeners.ts @@ -9,6 +9,10 @@ import { DatabaseResponse } from '../utils/ipc/types'; import { IPC_ACTIONS } from '../utils/messages'; import { getUrlAndTokenString, sendError } from './contactMothership'; import { getLanguageMap } from './getLanguageMap'; +import { + getConfigFilesWithModified, + setAndGetCleanedConfigFiles, +} from './helpers'; import { saveHtmlAsPdf } from './saveHtmlAsPdf'; export default function registerIpcMainActionListeners(main: Main) { @@ -120,6 +124,11 @@ export default function registerIpcMainActionListeners(main: Main) { await fs.unlink(filePath); }); + ipcMain.handle(IPC_ACTIONS.GET_DB_LIST, async (_) => { + const files = await setAndGetCleanedConfigFiles(); + return await getConfigFilesWithModified(files); + }); + /** * Database Related Actions */ diff --git a/src/components/Controls/AttachImage.vue b/src/components/Controls/AttachImage.vue index 201d976e..13f0ce9c 100644 --- a/src/components/Controls/AttachImage.vue +++ b/src/components/Controls/AttachImage.vue @@ -57,8 +57,6 @@ diff --git a/utils/messages.ts b/utils/messages.ts index ba215949..1ac7fcad 100644 --- a/utils/messages.ts +++ b/utils/messages.ts @@ -26,6 +26,7 @@ export enum IPC_ACTIONS { GET_FILE = 'get-file', GET_CREDS = 'get-creds', GET_VERSION = 'get-version', + GET_DB_LIST = 'get-db-list', DELETE_FILE = 'delete-file', // Database messages DB_CREATE = 'db-create', diff --git a/utils/translationHelpers.ts b/utils/translationHelpers.ts index a0ec73ed..c354d7e5 100644 --- a/utils/translationHelpers.ts +++ b/utils/translationHelpers.ts @@ -1,9 +1,17 @@ +/** + * Properties of a schema which are to be translated, + * irrespective of nesting. + */ export const schemaTranslateables = ['label', 'description', 'placeholder']; + export function getIndexFormat(inp: string | string[]) { - // converts: - // ['This is an ', ,' interpolated ',' string.'] and - // 'This is an ${variableA} interpolated ${variableB} string.' - // to 'This is an ${0} interpolated ${1} string.' + /** + * converts: + * ['This is an ', ,' interpolated ',' string.'] and + * 'This is an ${variableA} interpolated ${variableB} string.' + * to 'This is an ${0} interpolated ${1} string.' + */ + let string: string | undefined = undefined; let snippets: string[] | undefined = undefined;