import { app , remote } from 'electron' ; import * as fs from 'fs' ; import * as path from 'path' ; export interface StoreConfigOptions { configName : string ; defaults : any ; } export class Store { private path : string ; private data : any ; constructor ( opts ) { const userDataPath = ( app || remote . app ) . getPath ( 'userData' ) ; this . path = path . join ( userDataPath , opts . configName + '.json' ) ; this . data = parseDataFile ( this . path , opts . defaults ) ; } get ( key ) { return this . data [ key ] ; } set ( key , val ) { this . data [ key ] = val ; fs . writeFileSync ( this . path , JSON . stringify ( this . data ) ) ; } } function parseDataFile ( filePath , defaults ) { try { return JSON . parse ( fs . readFileSync ( filePath ) . toString ( ) ) ; } catch ( error ) { return defaults ; } }