2
0
mirror of https://github.com/frappe/books.git synced 2024-11-12 16:36:27 +00:00
books/fyo/demux/config.ts
18alantom 4415c04a88 chore: enable typescript eslint rules
- refactor code to satisfy rules (batch 1)
- use ensuredir in build to prevent ENOENT
2023-06-21 16:08:47 +05:30

47 lines
1.0 KiB
TypeScript

import type Store from 'electron-store';
import { ConfigMap } from 'fyo/core/types';
export class Config {
config: Map<string, unknown> | Store;
constructor(isElectron: boolean) {
this.config = new Map();
if (isElectron) {
const Config = require('electron-store') as typeof Store;
this.config = new Config();
}
}
get store() {
if (this.config instanceof Map) {
const store: Record<string, unknown> = {};
for (const key of this.config.keys()) {
store[key] = this.config.get(key);
}
return store;
} else {
return this.config;
}
}
get<K extends keyof ConfigMap>(
key: K,
defaultValue?: ConfigMap[K]
): ConfigMap[K] | undefined {
const value = this.config.get(key) as ConfigMap[K] | undefined;
return value ?? defaultValue;
}
set<K extends keyof ConfigMap>(key: K, value: ConfigMap[K]) {
this.config.set(key, value);
}
delete(key: keyof ConfigMap) {
this.config.delete(key);
}
clear() {
this.config.clear();
}
}