import { settingsConfig } from '@nuclear/core' ; import ElectronStore from 'electron-store' ; import { inject , injectable } from 'inversify' ; import _ from 'lodash' ; import Logger , { $mainLogger } from '../logger' ; import Config from '../config' ; see https @ injectable ( ) class Store extends ElectronStore { constructor ( @ inject ( $mainLogger ) private logger , @ inject ( Config ) private config ) { super ( ) ; if ( ! this . getOption ( 'invidious.url' ) ) { this . setOption ( 'invidious.url' , this . config . defaultInvidiousUrl ) ; } if ( ! this . getLastThumbCleanDate ( ) ) { this . setLastThumbCleanDate ( new Date ( ) ) ; } this . logger . log ( `Initialized settings store at ${ this . path }` ) ; } getOption ( key ) { const settings = this . get ( 'settings' ) || { } ; let value = settings [ key ] ; if ( typeof value === 'undefined' ) { value = ( _ . find ( settingsConfig , { name : key } ) as any ) . default ; } return value ; } setOption ( key , value ) { const settings = this . get ( 'settings' ) || { } ; this . set ( 'settings' , Object . assign ( { } , settings , { [ `${ key }` ] : value } ) ) ; } async setAvailableHttpPort ( startPort , endPort ) { const { default : getPort } = await import ( 'get-port' ) ; const availablePort = await getPort ( { port : getPort . makeRange ( startPort , endPort ) } ) ; this . setOption ( 'api.port' , availablePort ) ; } getLastThumbCleanDate ( ) { const time = this . get ( 'last-thumb-clean-date' ) ; if ( time ) { return new Date ( time ) ; } } setLastThumbCleanDate ( date ) { this . set ( 'last-thumb-clean-date' , date . getTime ( ) ) ; } } export default Store ;