All files / src/modules/implementations/database database.js

100% Statements 36/36
94.11% Branches 16/17
100% Functions 4/4
100% Lines 36/36

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 3754x 54x 785x 393x 393x 393x 393x 2x 2x 393x 785x 816x 132x 1x 1x 131x 131x 131x 684x 684x 684x 815x 244x 244x 244x 244x 243x 243x 244x 244x 816x 785x 785x 785x 785x 54x  
'use strict';
module.exports = ({ journal: { loadJournal, }, databaseFreshness: { checkDatabaseSync }, databaseRebuildLock: { withRebuildLock }, journalWriter: { writeJournalDatabase }, core: { publicErrors: { databaseError }, }, external: { fs, path }, }) => {
    function buildDatabase(databasePath, source) {
        try {
            return writeJournalDatabase(databasePath, loadJournal(source));
        }
        catch (error) {
            throw databaseError(error instanceof Error ? error : new Error(String(error)));
        }
    }
    function prepareDatabase(databasePath, source) {
        if (source.kind === 'stdin') {
            if (fs.existsSync(databasePath)) {
                throw databaseError(new Error('A stdin source requires a new database path'));
            }
            const summary = buildDatabase(databasePath, source);
            return { journalPath: summary.journalPath };
        }
        const normalizedSource = { kind: 'file', path: path.resolve(source.path) };
        const status = checkDatabaseSync(databasePath, normalizedSource.path);
        if (status.inSync)
            return { journalPath: status.journalPath };
        const rebuild = () => {
            const lockedStatus = checkDatabaseSync(databasePath, normalizedSource.path);
            if (lockedStatus.inSync)
                return { journalPath: lockedStatus.journalPath };
            const summary = buildDatabase(databasePath, normalizedSource);
            return { journalPath: summary.journalPath };
        };
        return withRebuildLock(databasePath, rebuild);
    }
    return {
        prepareDatabase,
        $$private: { buildDatabase, checkDatabaseSync },
    };
};