All files / src/modules/implementations/project cache-paths.js

100% Statements 34/34
100% Branches 17/17
100% Functions 4/4
100% Lines 34/34

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 3546x 46x 780x 680x 676x 676x 4x 4x 680x 1x 680x 780x 680x 1x 1x 679x 680x 1x 1x 677x 680x 780x 680x 680x 680x 680x 680x 680x 680x 780x 780x 780x 780x 46x  
'use strict';
module.exports = ({ core: { readConfiguration }, external: { crypto, envPaths, fs, path, processEnvironment }, }) => {
    function cacheRoot() {
        if (processEnvironment.LEDLIGHT_CACHE_HOME) {
            return path.resolve(processEnvironment.LEDLIGHT_CACHE_HOME);
        }
        const { cacheHome } = readConfiguration();
        if (cacheHome)
            return cacheHome;
        return envPaths('ledlight', { suffix: '' }).cache;
    }
    function canonicalJournalPath(journalPath) {
        if (typeof journalPath !== 'string' || journalPath.length === 0) {
            throw new TypeError('journalPath must be a non-empty string');
        }
        const resolvedPath = fs.realpathSync.native(path.resolve(journalPath));
        if (!fs.statSync(resolvedPath).isFile()) {
            throw new TypeError(`Journal path is not a file: ${resolvedPath}`);
        }
        return resolvedPath;
    }
    function pathsForJournal(journalPath) {
        const canonicalPath = canonicalJournalPath(journalPath);
        const identity = crypto.createHash('sha256').update(canonicalPath, 'utf8').digest('hex');
        return {
            journalPath: canonicalPath,
            databasePath: path.join(cacheRoot(), 'journals', identity, 'ledger.sqlite'),
        };
    }
    return {
        pathsForJournal,
        $$private: { cacheRoot, canonicalJournalPath },
    };
};