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 { AuthDemuxBase } from 'utils/auth/types';
import { IPC_ACTIONS } from 'utils/messages'; import { IPC_ACTIONS } from 'utils/messages';
import { Creds } from 'utils/types'; import { Creds } from 'utils/types';
const { ipcRenderer } = require('electron');
export class AuthDemux extends AuthDemuxBase { export class AuthDemux extends AuthDemuxBase {
#isElectron: boolean = false; #isElectron: boolean = false;

View File

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

View File

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

View File

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

View File

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

View File

@ -214,7 +214,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { setupDummyInstance } from 'dummy'; import { setupDummyInstance } from 'dummy';
import { ipcRenderer } from 'electron'; const { ipcRenderer } = require('electron');
import { t } from 'fyo'; import { t } from 'fyo';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import Button from 'src/components/Button.vue'; 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 { ConfigKeys } from 'fyo/core/types';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { CUSTOM_EVENTS, IPC_ACTIONS } from 'utils/messages'; 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 { handleError } from 'src/errorHandling';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import { IPC_CHANNELS } from 'utils/messages'; import { IPC_CHANNELS } from 'utils/messages';

View File

@ -1,7 +1,7 @@
/** /**
* Utils that make ipcRenderer calls. * Utils that make ipcRenderer calls.
*/ */
import { ipcRenderer } from 'electron'; const { ipcRenderer } = require('electron');
import { t } from 'fyo'; import { t } from 'fyo';
import { BaseError } from 'fyo/utils/errors'; import { BaseError } from 'fyo/utils/errors';
import { BackendResponse } from 'utils/ipc/types'; 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 { DEFAULT_LANGUAGE } from 'fyo/utils/consts';
import { setLanguageMapOnTranslationString } from 'fyo/utils/translation'; import { setLanguageMapOnTranslationString } from 'fyo/utils/translation';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';