All files / rusted-chromium/store loadStore.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 1/1
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 427x 7x   7x   7x 7x 7x   7x   7x                             7x   3x   3x       3x 3x   1x   3x    
import { readFile } from 'node:fs/promises'
import { join as pathJoin } from 'node:path'
 
import { LOCAL_STORE_FILE } from '../commons/constants'
import type { IListStore } from '../interfaces/store.interfaces'
import { logger } from '../log/logger'
import { existsAndIsFile } from '../utils/file.utils'
import { Store } from './Store'
 
const STORE_FILE = pathJoin(__dirname, '..', LOCAL_STORE_FILE)
 
const EMPTY_STORE: IListStore = {
    linux: {
        x64: [],
        x86: [],
    },
    mac: {
        x64: [],
        arm: [],
    },
    win: {
        x64: [],
        x86: [],
    },
}
 
export async function loadStore(): Promise<Store> {
 
    logger.debug(`using store file: ${STORE_FILE}`)
    
    const currentStoreJson = await existsAndIsFile(STORE_FILE)
        ? await readFile(STORE_FILE, 'utf8')
        : JSON.stringify(EMPTY_STORE)
    let currentStore: IListStore
    try {
        currentStore = JSON.parse(currentStoreJson)
    } catch {
        currentStore = EMPTY_STORE
    }
    return new Store(currentStore)
}