2
0
mirror of https://github.com/frappe/books.git synced 2025-01-03 07:12:21 +00:00

refactor: use require for electron imports

This commit is contained in:
18alantom 2023-06-14 14:10:03 +05:30
parent daa7fe549f
commit 35c019897d
10 changed files with 26 additions and 41 deletions

View File

@ -1,7 +1,7 @@
import { ipcRenderer } from 'electron';
import { AuthDemuxBase } from 'utils/auth/types';
import { IPC_ACTIONS } from 'utils/messages';
import { Creds } from 'utils/types';
const { ipcRenderer } = require('electron');
export class AuthDemux extends AuthDemuxBase {
#isElectron: boolean = false;

View File

@ -1,55 +1,41 @@
import config from 'utils/config';
import type Store from 'electron-store';
export class Config {
#useElectronConfig: boolean;
fallback: Map<string, unknown> = new Map();
config: Map<string, unknown> | Store;
constructor(isElectron: boolean) {
this.#useElectronConfig = isElectron;
this.config = new Map();
if (isElectron) {
const Config: typeof Store = require('electron-store');
this.config = new Config();
}
}
get store(): Record<string, unknown> {
if (this.#useElectronConfig) {
return config.store;
} else {
get store() {
if (this.config instanceof Map) {
const store: Record<string, unknown> = {};
for (const key of this.fallback.keys()) {
store[key] = this.fallback.get(key);
for (const key of this.config.keys()) {
store[key] = this.config.get(key);
}
return store;
} else {
return this.config;
}
}
get(key: string, defaultValue?: unknown): unknown {
if (this.#useElectronConfig) {
return config.get(key, defaultValue);
} else {
return this.fallback.get(key) ?? defaultValue;
}
return this.config.get(key) ?? defaultValue;
}
set(key: string, value: unknown) {
if (this.#useElectronConfig) {
config.set(key, value);
} else {
this.fallback.set(key, value);
}
this.config.set(key, value);
}
delete(key: string) {
if (this.#useElectronConfig) {
config.delete(key);
} else {
this.fallback.delete(key);
}
this.config.delete(key);
}
clear() {
if (this.#useElectronConfig) {
config.clear();
} else {
this.fallback.clear();
}
this.config.clear();
}
}

View File

@ -1,4 +1,4 @@
import { ipcRenderer } from 'electron';
const { ipcRenderer } = require('electron');
import { DatabaseError, NotImplemented } from 'fyo/utils/errors';
import { SchemaMap } from 'schemas/types';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';

View File

@ -96,8 +96,7 @@ export class Fyo {
async setIsElectron() {
try {
const { ipcRenderer } = await import('electron');
this.isElectron = Boolean(ipcRenderer);
this.isElectron = Boolean(require('electron'));
} catch {
this.isElectron = false;
}

View File

@ -1,4 +1,3 @@
import { ipcRenderer } from 'electron';
import { t } from 'fyo';
import { ConfigKeys } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
@ -11,6 +10,7 @@ import { fyo } from './initFyo';
import router from './router';
import { getErrorMessage, stringifyCircular } from './utils';
import { DialogOptions, ToastOptions } from './utils/types';
const { ipcRenderer } = require('electron');
function shouldNotStore(error: Error) {
const shouldLog = (error as BaseError).shouldStore ?? true;

View File

@ -214,7 +214,7 @@
</template>
<script lang="ts">
import { setupDummyInstance } from 'dummy';
import { ipcRenderer } from 'electron';
const { ipcRenderer } = require('electron');
import { t } from 'fyo';
import { DateTime } from 'luxon';
import Button from 'src/components/Button.vue';

View File

@ -1,4 +1,4 @@
import { ipcRenderer } from 'electron';
const { ipcRenderer } = require('electron');
import { ConfigKeys } from 'fyo/core/types';
import { DateTime } from 'luxon';
import { CUSTOM_EVENTS, IPC_ACTIONS } from 'utils/messages';

View File

@ -1,4 +1,4 @@
import { ipcRenderer } from 'electron';
const { ipcRenderer } = require('electron');
import { handleError } from 'src/errorHandling';
import { fyo } from 'src/initFyo';
import { IPC_CHANNELS } from 'utils/messages';

View File

@ -1,7 +1,7 @@
/**
* Utils that make ipcRenderer calls.
*/
import { ipcRenderer } from 'electron';
const { ipcRenderer } = require('electron');
import { t } from 'fyo';
import { BaseError } from 'fyo/utils/errors';
import { BackendResponse } from 'utils/ipc/types';

View File

@ -1,4 +1,4 @@
import { ipcRenderer } from 'electron';
const { ipcRenderer } = require('electron');
import { DEFAULT_LANGUAGE } from 'fyo/utils/consts';
import { setLanguageMapOnTranslationString } from 'fyo/utils/translation';
import { fyo } from 'src/initFyo';