All files licenseCheckerHelpers.js

52.08% Statements 25/48
34.38% Branches 11/32
37.5% Functions 3/8
53.19% Lines 25/47

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 9213x 13x 13x 13x   13x   13x 10x     13x                         13x 10x   10x                 10x     13x 10x 10x 10x   10x                                     10x 5x     5x       5x       5x       5x       5x     13x          
const chalk = require('chalk');
const cloneDeep = require('lodash.clonedeep');
const fs = require('fs');
const path = require('path');
 
const licenseChecker = require('./index');
 
const shouldColorizeOutput = function shouldColorizeOutput(args) {
    return args.color && !args.out && !args.files && !(args.csv || args.json || args.markdown || args.plainVertical);
};
 
const colorizeOutput = function colorizeOutput(json) {
    Object.keys(json).forEach((key) => {
        const index = key.lastIndexOf('@');
        const colorizedKey =
            chalk.white.bgKeyword('darkslategrey')(key.slice(0, index + 1)) +
            chalk.dim('@') +
            chalk.white.bgKeyword('green')(key.slice(index + 1));
        json[colorizedKey] = json[key];
 
        delete json[key];
    });
};
 
const filterJson = function filterJson(limitAttributes, json) {
    let filteredJson = json;
 
    Iif (limitAttributes) {
        filteredJson = {};
        const attributes = limitAttributes.split(',').map((attribute) => attribute.trim());
 
        Object.keys(json).forEach((dependency) => {
            filteredJson[dependency] = licenseChecker.filterAttributes(attributes, json[dependency]);
        });
    }
 
    return filteredJson;
};
 
const getFormattedOutput = function getFormattedOutput(modulesWithVersions, args) {
    let filteredJson = filterJson(args.limitAttributes, modulesWithVersions);
    const jsonCopy = cloneDeep(filteredJson);
    filteredJson = null;
 
    Iif (args.files) {
        Object.keys(jsonCopy).forEach((moduleName) => {
            const outPath = path.join(args.files, `${moduleName}-LICENSE.txt`);
            const originalLicenseFile = jsonCopy[moduleName].licenseFile;
 
            if (originalLicenseFile && fs.existsSync(originalLicenseFile)) {
                if (args.relativeLicensePath) {
                    if (args.out) {
                        jsonCopy[moduleName].licenseFile = path.relative(path.dirname(args.out), outPath);
                    } else {
                        jsonCopy[moduleName].licenseFile = path.relative(process.cwd(), outPath);
                    }
                } else {
                    jsonCopy[moduleName].licenseFile = outPath;
                }
            }
        });
    }
 
    if (args.json) {
        return JSON.stringify(jsonCopy, null, 4) + '\n';
    }
 
    Iif (args.csv) {
        return licenseChecker.asCSV(jsonCopy, args.customFormat, args.csvComponentPrefix);
    }
 
    Iif (args.markdown) {
        return licenseChecker.asMarkDown(jsonCopy, args.customFormat) + '\n';
    }
 
    Iif (args.summary) {
        return licenseChecker.asSummary(jsonCopy);
    }
 
    Iif (args.plainVertical || args.angluarCli) {
        return licenseChecker.asPlainVertical(jsonCopy);
    }
 
    return licenseChecker.asTree(jsonCopy);
};
 
module.exports = {
    colorizeOutput: colorizeOutput,
    getFormattedOutput: getFormattedOutput,
    shouldColorizeOutput: shouldColorizeOutput,
};