All files / src/utils getParsedGitignores.js

100% Statements 15/15
100% Branches 8/8
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 303x 3x   3x   3x     3x 6x 6x 4x 4x         6x 6x 2x 2x         6x     3x  
const fs = require('fs');
const path = require('path');
 
const parseGitignore = require('parse-gitignore');
 
const getHomeDir = require('./getHomeDir');
 
// TODO: log warning when gitignore does not exist
const getParsedGitignores = ({src, localGitignore, globalGitignore}) => {
    let localGitignoreContent = [];
    if (localGitignore) {
        const localGitignorePath = path.join(src, '.gitignore');
        localGitignoreContent = fs.existsSync(localGitignorePath)
            ? parseGitignore(fs.readFileSync(localGitignorePath))
            : [];
    }
 
    let globalGitignoreContent = [];
    if (globalGitignore) {
        const globalGitignorePath = path.join(getHomeDir(), '.gitignore');
        globalGitignoreContent = fs.existsSync(globalGitignorePath)
            ? parseGitignore(fs.readFileSync(globalGitignorePath))
            : [];
    }
 
    return [...localGitignoreContent, ...globalGitignoreContent];
};
 
module.exports = getParsedGitignores;