| 22 | } |
| 23 | |
| 24 | class Preference extends TypedEmitter<PreferenceEvents> { |
| 25 | public readonly preferencesPath: string |
| 26 | public readonly hasPreferencesFile: boolean |
| 27 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 28 | public readonly store: Store<any> |
| 29 | public readonly staticPath: string |
| 30 | |
| 31 | /** |
| 32 | * @param paths The path instance. |
| 33 | * |
| 34 | * NOTE: This throws an exception when validation fails. |
| 35 | */ |
| 36 | constructor(paths: AppPaths) { |
| 37 | // TODO: Preferences should not loaded if global.MARKTEXT_SAFE_MODE is set. |
| 38 | super() |
| 39 | |
| 40 | const { preferencesPath } = paths |
| 41 | this.preferencesPath = preferencesPath |
| 42 | this.hasPreferencesFile = fs.existsSync( |
| 43 | path.join(this.preferencesPath, `./${PREFERENCES_FILE_NAME}.json`) |
| 44 | ) |
| 45 | this.store = new Store({ |
| 46 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 47 | schema: schema as any, |
| 48 | name: PREFERENCES_FILE_NAME, |
| 49 | migrations: { |
| 50 | '0.18.6': (store) => { |
| 51 | if (store.get('startUpAction') === 'lastState') { |
| 52 | store.set('startUpAction', 'openLastFolder') |
| 53 | } |
| 54 | } |
| 55 | }, |
| 56 | beforeEachMigration: (_store, context) => { |
| 57 | log.info(`Preferences migration: ${context.fromVersion} -> ${context.toVersion}`) |
| 58 | } |
| 59 | }) |
| 60 | |
| 61 | this.staticPath = path.join(global.__static, 'preference.json') |
| 62 | this.init() |
| 63 | } |
| 64 | |
| 65 | init = (): void => { |
| 66 | let defaultSettings: Record<string, unknown> | null = null |
| 67 | try { |
| 68 | defaultSettings = JSON.parse(fs.readFileSync(this.staticPath, { encoding: 'utf8' }) || '{}') |
| 69 | |
| 70 | // Set best theme on first application start. |
| 71 | if (nativeTheme.shouldUseDarkColors) { |
| 72 | defaultSettings!.theme = 'dark' |
| 73 | } |
| 74 | |
| 75 | // Set system language on first application start |
| 76 | if (!this.hasPreferencesFile) { |
| 77 | const systemLanguage = this._getSystemLanguage() |
| 78 | if (systemLanguage) { |
| 79 | defaultSettings!.language = systemLanguage |
| 80 | } |
| 81 | } |
nothing calls this directly
no test coverage detected