2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 02:49:03 +00:00

refactor: switch to preload script

- delete ipcCalls
This commit is contained in:
18alantom 2023-07-10 13:33:52 +05:30
parent 675066d6f2
commit e2b9ea1f49
19 changed files with 37 additions and 208 deletions

View File

@ -1,12 +1,11 @@
import { t } from 'fyo';
import { Action } from 'fyo/model/types';
import { Verb } from 'fyo/telemetry/types';
import { saveData } from 'src/utils/ipcCalls';
import { getSavePath, showExportInFolder } from 'src/utils/ui';
import { getIsNullOrUndef } from 'utils';
import { generateCSV } from 'utils/csvParser';
import { Report } from './Report';
import { ExportExtention, ReportCell } from './types';
import { getSavePath, showExportInFolder } from 'src/utils/ui';
interface JSONExport {
columns: { fieldname: string; label: string }[];
@ -184,7 +183,7 @@ export async function saveExportData(
filePath: string,
message?: string
) {
await saveData(data, filePath);
await ipc.saveData(data, filePath);
message ??= t`Export Successful`;
showExportInFolder(message, filePath);
}

View File

@ -53,7 +53,6 @@ import { connectToDatabase, dbErrorActionSymbols } from './utils/db';
import { initializeInstance } from './utils/initialization';
import * as injectionKeys from './utils/injectionKeys';
import { showDialog } from './utils/interactive';
import { checkDbAccess, checkForUpdates } from './utils/ipcCalls';
import { setLanguageMap } from './utils/language';
import { updateConfigFiles } from './utils/misc';
import { updatePrintTemplates } from './utils/printTemplates';
@ -147,7 +146,7 @@ export default defineComponent({
this.activeScreen = Screen.Desk;
await this.setDeskRoute();
await fyo.telemetry.start(true);
await checkForUpdates();
await ipc.checkForUpdates();
await setLanguageMap();
this.dbPath = filePath;
this.companyName = (await fyo.getValue(
@ -164,7 +163,7 @@ export default defineComponent({
return;
}
if (filePath !== ':memory:' && !(await checkDbAccess(filePath))) {
if (filePath !== ':memory:' && !(await ipc.checkDbAccess(filePath))) {
await showDialog({
title: this.t`Cannot open file`,
type: 'error',

View File

@ -57,7 +57,6 @@
<script lang="ts">
import { Field } from 'schemas/types';
import { fyo } from 'src/initFyo';
import { selectFile } from 'src/utils/ipcCalls';
import { getDataURL } from 'src/utils/misc';
import { defineComponent, PropType } from 'vue';
import FeatherIcon from '../FeatherIcon.vue';
@ -105,7 +104,7 @@ export default defineComponent({
],
};
const { name, success, data } = await selectFile(options);
const { name, success, data } = await ipc.selectFile(options);
if (!success) {
return;

View File

@ -87,6 +87,7 @@
</template>
<script lang="ts">
import { t } from 'fyo';
import { Verb } from 'fyo/telemetry/types';
import { Field, FieldTypeEnum } from 'schemas/types';
import { fyo } from 'src/initFyo';
import {
@ -95,17 +96,15 @@ import {
getExportTableFields,
getJsonExportData,
} from 'src/utils/export';
import { saveData } from 'src/utils/ipcCalls';
import { ExportField, ExportFormat, ExportTableField } from 'src/utils/types';
import { getSavePath, showExportInFolder } from 'src/utils/ui';
import { QueryFilter } from 'utils/db/types';
import { defineComponent, PropType } from 'vue';
import { PropType, defineComponent } from 'vue';
import Button from './Button.vue';
import Check from './Controls/Check.vue';
import Int from './Controls/Int.vue';
import Select from './Controls/Select.vue';
import FormHeader from './FormHeader.vue';
import { Verb } from 'fyo/telemetry/types';
import { getSavePath, showExportInFolder } from 'src/utils/ui';
interface ExportWizardData {
useListFilters: boolean;
@ -258,7 +257,7 @@ export default defineComponent({
return;
}
await saveData(data, filePath);
await ipc.saveData(data, filePath);
this.fyo.telemetry.log(Verb.Exported, this.schemaName, {
extension: this.exportFormat,
});

View File

@ -9,7 +9,6 @@
</button>
</template>
<script>
import { openLink } from 'src/utils/ipcCalls';
import FeatherIcon from './FeatherIcon.vue';
export default {
@ -23,7 +22,7 @@ export default {
},
methods: {
openHelpLink() {
openLink(this.link);
ipc.openLink(this.link);
},
},
};

View File

@ -197,13 +197,12 @@
import { fyo } from 'src/initFyo';
import { getBgTextColorClass } from 'src/utils/colors';
import { searcherKey, shortcutsKey } from 'src/utils/injectionKeys';
import { openLink } from 'src/utils/ipcCalls';
import { docsPathMap } from 'src/utils/misc';
import {
getGroupLabelMap,
SearchGroup,
searchGroups,
SearchItems,
getGroupLabelMap,
searchGroups,
} from 'src/utils/search';
import { defineComponent, inject, nextTick } from 'vue';
import Button from './Button.vue';
@ -305,7 +304,7 @@ export default defineComponent({
},
methods: {
openDocs() {
openLink('https://docs.frappebooks.com/' + docsPathMap.Search);
ipc.openLink('https://docs.frappebooks.com/' + docsPathMap.Search);
},
getShortcuts() {
const ifOpen = (cb: Function) => () => this.openModal && cb();

View File

@ -182,7 +182,6 @@
import { reportIssue } from 'src/errorHandling';
import { fyo } from 'src/initFyo';
import { languageDirectionKey, shortcutsKey } from 'src/utils/injectionKeys';
import { openLink } from 'src/utils/ipcCalls';
import { docsPathRef } from 'src/utils/refs';
import { getSidebarConfig } from 'src/utils/sidebarConfig';
import { SidebarConfig, SidebarItem, SidebarRoot } from 'src/utils/types';
@ -255,7 +254,7 @@ export default defineComponent({
reportIssue,
toggleSidebar,
openDocumentation() {
openLink('https://docs.frappebooks.com/' + docsPathRef.value);
ipc.openLink('https://docs.frappebooks.com/' + docsPathRef.value);
},
setActiveGroup() {
const { fullPath } = this.$router.currentRoute.value;

View File

@ -7,11 +7,6 @@ import { showDialog } from 'src/utils/interactive';
import { fyo } from './initFyo';
import router from './router';
import { getErrorMessage, stringifyCircular } from './utils';
import {
sendError as ipcSendError,
openExternalUrl,
showError,
} from './utils/ipcCalls';
import type { DialogOptions, ToastOptions } from './utils/types';
function shouldNotStore(error: Error) {
@ -46,7 +41,7 @@ export async function sendError(errorLogObj: ErrorLog) {
console.log('sendError', body);
}
await ipcSendError(JSON.stringify(body));
await ipc.sendError(JSON.stringify(body));
}
function getToastProps(errorLogObj: ErrorLog) {
@ -157,7 +152,7 @@ export async function showErrorDialog(title?: string, content?: string) {
// To be used for show stopper errors
title ??= t`Error`;
content ??= t`Something has gone terribly wrong. Please check the console and raise an issue.`;
await showError(title, content);
await ipc.showError(title, content);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@ -234,7 +229,7 @@ function getIssueUrlQuery(errorLogObj?: ErrorLog): string {
export function reportIssue(errorLogObj?: ErrorLog) {
const urlQuery = getIssueUrlQuery(errorLogObj);
openExternalUrl(urlQuery);
ipc.openExternalUrl(urlQuery);
}
function getErrorLabel(error: Error) {

View File

@ -246,7 +246,6 @@ import Loading from 'src/components/Loading.vue';
import Modal from 'src/components/Modal.vue';
import { fyo } from 'src/initFyo';
import { showDialog } from 'src/utils/interactive';
import { getDbList } from 'src/utils/ipcCalls';
import { updateConfigFiles } from 'src/utils/misc';
import { deleteDb, getSavePath, getSelectedFilePath } from 'src/utils/ui';
import type { ConfigFilesWithModified } from 'utils/types';
@ -359,7 +358,7 @@ export default defineComponent({
this.creatingDemo = false;
},
async setFiles() {
const dbList = await getDbList();
const dbList = await ipc.getDbList();
this.files = dbList?.sort(
(a, b) => Date.parse(b.modified) - Date.parse(a.modified)
);

View File

@ -76,10 +76,8 @@ import Icon from 'src/components/Icon.vue';
import PageHeader from 'src/components/PageHeader.vue';
import { fyo } from 'src/initFyo';
import { getGetStartedConfig } from 'src/utils/getStartedConfig';
import { openLink } from 'src/utils/ipcCalls';
import { GetStartedConfigItem } from 'src/utils/types';
import { Component } from 'vue';
import { defineComponent, h } from 'vue';
import { Component, defineComponent, h } from 'vue';
type ListItem = GetStartedConfigItem['items'][number];
@ -106,7 +104,7 @@ export default defineComponent({
methods: {
async handleDocumentation({ key, documentation }: ListItem) {
if (documentation) {
openLink(documentation);
ipc.openLink(documentation);
}
switch (key) {

View File

@ -388,7 +388,6 @@ import PageHeader from 'src/components/PageHeader.vue';
import { Importer, TemplateField, getColumnLabel } from 'src/importer';
import { fyo } from 'src/initFyo';
import { showDialog } from 'src/utils/interactive';
import { saveData } from 'src/utils/ipcCalls';
import { docsPathMap } from 'src/utils/misc';
import { docsPathRef } from 'src/utils/refs';
import { getSavePath, selectTextFile } from 'src/utils/ui';
@ -795,7 +794,7 @@ export default defineComponent({
return;
}
await saveData(template, filePath);
await ipc.saveData(template, filePath);
},
async preImportValidations(): Promise<boolean> {
const title = this.t`Cannot Import`;

View File

@ -80,7 +80,6 @@ import { getErrorMessage } from 'src/utils';
import { evaluateHidden } from 'src/utils/doc';
import { shortcutsKey } from 'src/utils/injectionKeys';
import { showDialog } from 'src/utils/interactive';
import { reloadWindow } from 'src/utils/ipcCalls';
import { docsPathMap } from 'src/utils/misc';
import { docsPathRef } from 'src/utils/refs';
import { UIGroupedFields } from 'src/utils/types';
@ -222,7 +221,7 @@ export default defineComponent({
{
label: this.t`Yes`,
isPrimary: true,
action: reloadWindow,
action: ipc.reloadWindow.bind(ipc),
},
{
label: this.t`No`,

View File

@ -10,7 +10,6 @@ import { outsideClickDirective } from './renderer/helpers';
import registerIpcRendererListeners from './renderer/registerIpcRendererListeners';
import router from './router';
import { stringifyCircular } from './utils';
import { getEnv } from './utils/ipcCalls';
import { setLanguageMap } from './utils/language';
// eslint-disable-next-line @typescript-eslint/no-floating-promises
@ -22,7 +21,7 @@ import { setLanguageMap } from './utils/language';
fyo.store.language = language || 'English';
registerIpcRendererListeners();
const { isDevelopment, platform, version } = await getEnv();
const { isDevelopment, platform, version } = await ipc.getEnv();
fyo.store.isDevelopment = isDevelopment;
fyo.store.appVersion = version;

View File

@ -1,12 +1,8 @@
import { handleError } from 'src/errorHandling';
import { fyo } from 'src/initFyo';
import {
registerConsoleLogListener,
registerMainProcessErrorListener,
} from 'src/utils/ipcCalls';
export default function registerIpcRendererListeners() {
registerMainProcessErrorListener(
ipc.registerMainProcessErrorListener(
(_, error: unknown, more?: Record<string, unknown>) => {
if (!(error instanceof Error)) {
throw error;
@ -28,7 +24,7 @@ export default function registerIpcRendererListeners() {
}
);
registerConsoleLogListener((_, ...stuff: unknown[]) => {
ipc.registerConsoleLogListener((_, ...stuff: unknown[]) => {
if (!fyo.store.isDevelopment) {
return;
}

2
src/shims-tsx.d.ts vendored
View File

@ -1,8 +1,8 @@
import type { IPC } from 'main/preload';
import Vue, { VNode } from 'vue';
declare const ipc: IPC;
declare global {
const ipc: IPC;
namespace JSX {
type Element = VNode;
type ElementClass = Vue;

View File

@ -1,142 +0,0 @@
/**
* Utils that make ipcRenderer calls.
*/
const { ipcRenderer } = require('electron');
import type {
OpenDialogOptions,
OpenDialogReturnValue,
SaveDialogOptions,
SaveDialogReturnValue,
} from 'electron';
import type { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages';
import type {
ConfigFilesWithModified,
LanguageMap,
SelectFileOptions,
SelectFileReturn,
TemplateFile,
} from 'utils/types';
export function reloadWindow() {
return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW);
}
export async function getLanguageMap(code: string) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_LANGUAGE_MAP, code)) as {
languageMap: LanguageMap;
success: boolean;
message: string;
};
}
export async function getOpenFilePath(options: OpenDialogOptions) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_OPEN_FILEPATH,
options
)) as OpenDialogReturnValue;
}
export async function getTemplates(): Promise<TemplateFile[]> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_TEMPLATES
)) as TemplateFile[];
}
export async function selectFile(
options: SelectFileOptions
): Promise<SelectFileReturn> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.SELECT_FILE,
options
)) as SelectFileReturn;
}
export async function checkDbAccess(filePath: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.CHECK_DB_ACCESS,
filePath
)) as boolean;
}
export async function checkForUpdates() {
await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES);
}
export function openLink(link: string) {
ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, link);
}
export async function deleteFile(filePath: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DELETE_FILE,
filePath
)) as BackendResponse;
}
export async function saveData(data: string, savePath: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SAVE_DATA, data, savePath);
}
export function showItemInFolder(filePath: string) {
ipcRenderer.send(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, filePath);
}
export async function makePDF(
html: string,
savePath: string,
width: number,
height: number
): Promise<boolean> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.SAVE_HTML_AS_PDF,
html,
savePath,
width,
height
)) as boolean;
}
export async function getSaveFilePath(options: SaveDialogOptions) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_SAVE_FILEPATH,
options
)) as SaveDialogReturnValue;
}
export async function getDbList() {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_DB_LIST
)) as ConfigFilesWithModified[];
}
export async function getEnv() {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_ENV)) as {
isDevelopment: boolean;
platform: string;
version: string;
};
}
export function openExternalUrl(url: string) {
ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, url);
}
export async function showError(title: string, content: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content });
}
export async function sendError(body: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, body);
}
type IPCRendererListener = Parameters<typeof ipcRenderer.on>[1];
export function registerMainProcessErrorListener(
listener: IPCRendererListener
) {
ipcRenderer.on(IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, listener);
}
export function registerConsoleLogListener(listener: IPCRendererListener) {
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, listener);
}

View File

@ -1,7 +1,6 @@
import { DEFAULT_LANGUAGE } from 'fyo/utils/consts';
import { setLanguageMapOnTranslationString } from 'fyo/utils/translation';
import { fyo } from 'src/initFyo';
import { getLanguageMap, reloadWindow } from './ipcCalls';
import { systemLanguageRef } from './refs';
// Language: Language Code in books/translations
@ -45,7 +44,7 @@ export async function setLanguageMap(
}
if (!dontReload && success && initLanguage !== oldLanguage) {
reloadWindow();
ipc.reloadWindow();
}
return success;
}
@ -63,7 +62,7 @@ function getLanguageCode(initLanguage: string, oldLanguage: string) {
}
async function fetchAndSetLanguageMap(code: string) {
const { success, message, languageMap } = await getLanguageMap(code);
const { success, message, languageMap } = await ipc.getLanguageMap(code);
if (!success) {
const { showToast } = await import('src/utils/interactive');

View File

@ -6,7 +6,6 @@ import { FieldTypeEnum, Schema, TargetField } from 'schemas/types';
import { getValueMapFromList } from 'utils/index';
import { TemplateFile } from 'utils/types';
import { showToast } from './interactive';
import { getTemplates, makePDF } from './ipcCalls';
import { PrintValues } from './types';
import {
getDocFromNameIfExistsElseNew,
@ -234,7 +233,7 @@ export async function getPathAndMakePDF(
}
const html = constructPrintDocument(innerHTML);
const success = await makePDF(html, savePath, width, height);
const success = await ipc.makePDF(html, savePath, width, height);
if (success) {
showExportInFolder(t`Save as PDF Successful`, savePath);
} else {
@ -277,7 +276,7 @@ function getAllCSSAsStyleElem() {
}
export async function updatePrintTemplates(fyo: Fyo) {
const templateFiles = await getTemplates();
const templateFiles = await ipc.getTemplates();
const existingTemplates = (await fyo.db.getAll(ModelNameEnum.PrintTemplate, {
fields: ['name', 'modified'],
filters: { isCustom: false },

View File

@ -28,13 +28,6 @@ import { SelectFileOptions } from 'utils/types';
import { RouteLocationRaw } from 'vue-router';
import { evaluateHidden } from './doc';
import { showDialog, showToast } from './interactive';
import {
deleteFile,
getOpenFilePath,
getSaveFilePath,
selectFile,
showItemInFolder,
} from './ipcCalls';
import { showSidebar } from './refs';
import {
ActionGroup,
@ -445,7 +438,9 @@ export async function selectTextFile(filters?: SelectFileOptions['filters']) {
title: t`Select File`,
filters,
};
const { success, canceled, filePath, data, name } = await selectFile(options);
const { success, canceled, filePath, data, name } = await ipc.selectFile(
options
);
if (canceled || !success) {
showToast({
@ -971,13 +966,13 @@ export function showExportInFolder(message: string, filePath: string) {
actionText: t`Open Folder`,
type: 'success',
action: () => {
showItemInFolder(filePath);
ipc.showItemInFolder(filePath);
},
});
}
export async function deleteDb(filePath: string) {
const { error } = await deleteFile(filePath);
const { error } = await ipc.deleteFile(filePath);
if (error?.code === 'EBUSY') {
await showDialog({
@ -1006,7 +1001,7 @@ export async function deleteDb(filePath: string) {
}
export async function getSelectedFilePath() {
return getOpenFilePath({
return ipc.getOpenFilePath({
title: t`Select file`,
properties: ['openFile'],
filters: [{ name: 'SQLite DB File', extensions: ['db'] }],
@ -1014,7 +1009,7 @@ export async function getSelectedFilePath() {
}
export async function getSavePath(name: string, extention: string) {
const response = await getSaveFilePath({
const response = await ipc.getSaveFilePath({
title: t`Select folder`,
defaultPath: `${name}.${extention}`,
});