All files / src/utils getFilterFile.js

100% Statements 24/24
100% Branches 8/8
100% Functions 3/3
100% Lines 24/24

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 4x 2x   2x 4x 2x     2x   2x   2x 1x 1x     2x   2x   2x 1x   1x   1x     2x     3x  
const fs = require('fs');
const path = require('path');
 
const md5 = require('md5');
 
const getHomeDir = require('./getHomeDir');
const log = require('./log');
 
const getFilterFile = ({include, exclude, name}) => {
    if (include.length === 0 && exclude.length === 0) {
        return null;
    }
    const content = [
        exclude.map(rule => `- ${rule}`).join('\n'),
        include.map(rule => `+ ${rule}`).join('\n'),
    ].join('\n');
 
    const hash = md5(content);
 
    const filterDirPath = path.resolve(getHomeDir(), '.synd');
 
    if (!fs.existsSync(filterDirPath)) {
        log(".synd dir not present, so let's create it");
        fs.mkdirSync(filterDirPath);
    }
 
    const filterFileName = `${name}.${hash}.filter`;
 
    const filterFilePath = path.resolve(filterDirPath, filterFileName);
 
    if (!fs.existsSync(filterFilePath)) {
        log(`filter file not preset, creating ${filterFilePath}`);
 
        fs.writeFileSync(filterFilePath, content);
    } else {
        log('found filter file, using it', filterFilePath);
    }
 
    return filterFilePath;
};
 
module.exports = getFilterFile;