2
0
mirror of https://github.com/frappe/books.git synced 2024-09-19 19:19:02 +00:00

incr: remove fs and path calls

This commit is contained in:
18alantom 2022-05-19 23:55:01 +05:30
parent 0951e11cb1
commit 341c1bb639
7 changed files with 95 additions and 55 deletions

46
main/helpers.ts Normal file
View File

@ -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<string, ConfigFile> = 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;
}

View File

@ -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
*/

View File

@ -57,8 +57,6 @@
<script>
import { ipcRenderer } from 'electron';
import fs from 'fs';
import path from 'path';
import { fyo } from 'src/initFyo';
import { IPC_ACTIONS } from 'utils/messages';
import Base from './Base';
@ -82,24 +80,28 @@ export default {
],
};
const { filePaths } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_OPEN_FILEPATH,
const { name, success, data } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_FILE,
options
);
if (filePaths && filePaths[0]) {
let dataURL = await this.getDataURL(filePaths[0]);
this.triggerChange(dataURL);
if (!success) {
return;
}
const dataURL = await this.getDataURL(name, data);
this.triggerChange(dataURL);
},
getDataURL(filePath) {
let typedArray = fs.readFileSync(filePath);
let extension = path.extname(filePath).slice(1);
let blob = new Blob([typedArray.buffer], { type: 'image/' + extension });
getDataURL(name, data) {
const extension = name.split('.').at(-1);
const blob = new Blob([data], { type: 'image/' + extension });
return new Promise((resolve) => {
let fr = new FileReader();
const fr = new FileReader();
fr.addEventListener('loadend', () => {
resolve(fr.result);
});
fr.readAsDataURL(blob);
});
},

View File

@ -102,7 +102,6 @@
</div>
</template>
<script>
import path from 'path';
import Button from 'src/components/Button.vue';
import { reportIssue } from 'src/errorHandling';
import { fyo } from 'src/initFyo';
@ -126,10 +125,6 @@ export default {
appVersion() {
return fyo.store.appVersion;
},
dbPath() {
const splits = fyo.db.dbPath.split(path.sep);
return path.join(...splits.slice(splits.length - 2));
},
},
components: {
WindowControls,
@ -178,7 +173,8 @@ export default {
itemActiveClass(item) {
let { path: currentRoute, params } = this.$route;
let routeMatch = currentRoute === item.route;
let schemaNameMatch = item.schemaName && params.schemaName === item.schemaName;
let schemaNameMatch =
item.schemaName && params.schemaName === item.schemaName;
return routeMatch || schemaNameMatch ? 'bg-white text-blue-500' : '';
},
isActiveGroup(group) {

View File

@ -95,7 +95,7 @@
{{ file.companyName }}
</p>
<p class="text-sm text-gray-600">
{{ file.modified }}
{{ formatDate(file.modified) }}
</p>
</div>
<button
@ -148,7 +148,6 @@
<script>
import { setupDummyInstance } from 'dummy';
import { ipcRenderer } from 'electron';
import fs from 'fs';
import { t } from 'fyo';
import { ConfigKeys } from 'fyo/core/types';
import { addNewFile } from 'fyo/telemetry/helpers';
@ -175,14 +174,17 @@ export default {
files: [],
};
},
mounted() {
this.setFiles();
async mounted() {
await this.setFiles();
if (fyo.store.isDevelopment) {
window.ds = this;
}
},
methods: {
formatDate(isoDate) {
return DateTime.fromISO(isoDate).toRelative();
},
async deleteDb(i) {
const file = this.files[i];
const vm = this;
@ -195,7 +197,7 @@ export default {
label: this.t`Yes`,
async action() {
await deleteDb(file.dbPath);
vm.setFiles();
await vm.setFiles();
},
},
{
@ -230,18 +232,14 @@ export default {
fyo.config.get(ConfigKeys.Files, []),
filePath
);
fyo.purgeCache();
this.setFiles();
await this.setFiles();
this.creatingDemo = false;
},
setFiles() {
this.files = setAndGetCleanedConfigFiles();
for (const file of this.files) {
const stats = fs.statSync(file.dbPath);
file.modified = DateTime.fromJSDate(stats.mtime).toRelative();
}
async setFiles() {
this.files = await ipcRenderer.invoke(IPC_ACTIONS.GET_DB_LIST);
},
async newDatabase() {
if (this.creatingDemo) {
@ -297,24 +295,4 @@ export default {
FeatherIcon,
},
};
function setAndGetCleanedConfigFiles() {
const files = fyo.config
.get('files', [])
.filter(({ dbPath }) => fs.existsSync(dbPath));
const deduper = [];
const deduped = files.filter(({ companyName, dbPath }) => {
const key = `${companyName}-${dbPath}`;
if (deduper.includes(key)) {
return false;
}
deduper.push(key);
return true;
});
fyo.config.set(ConfigKeys.Files, deduped);
return deduped;
}
</script>

View File

@ -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',

View File

@ -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;