All files / rusted-chromium/store importStore.ts

85.71% Statements 18/21
71.42% Branches 5/7
100% Functions 2/2
85.71% Lines 18/21

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 42 433x 3x   3x     3x 3x 3x 3x     3x   3x 2x       2x   2x       2x 2x   2x   2x   2x               2x    
import { readFile, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
 
import { LOCAL_STORE_FILE } from '../commons/constants'
import type { IStoreConfig } from '../interfaces/interfaces'
import type { StoreSize } from '../interfaces/store.interfaces'
import { DebugMode, logger } from '../log/logger'
import { existsAndIsFile } from '../utils/file.utils'
import { downloadStore } from './downloadStore'
import { readStoreFile } from './readStore'
import type { Store } from './Store'
 
const localStoreFilePath = join(__dirname, '..', LOCAL_STORE_FILE)
 
export async function importAndMergeLocalstore(config: IStoreConfig): Promise<StoreSize> {
    Iif(config.debug) {
        logger.setDebugMode(DebugMode.DEBUG)
    }
 
    const isURL = config.url.startsWith('http://') || config.url.startsWith('https://')
 
    const store = isURL
        ? await downloadStore(config, LOCAL_STORE_FILE)
        : await readStoreFile(config)
        
    if (await existsAndIsFile(localStoreFilePath)) {
        const localStore = await readFile(LOCAL_STORE_FILE, { encoding: 'utf-8' })
 
        const sortedStore = store.merge(JSON.parse(localStore))
 
        await storeStoreFile(sortedStore)
 
        return sortedStore.size()
    } else E{
        await storeStoreFile(store)
        return store.size()
    }
}
 
async function storeStoreFile(store: Store): Promise<void> {
    return writeFile(localStoreFilePath, store.toMinimalFormattedString())
}