# All files / src/systemTools doctor.js

29.41% Statements 15/51
0% Branches 0/19
0% Functions 0/8
25.53% Lines 12/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
    711x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

     import chalk from 'chalk';
    import { writeObjectSync, readObjectSync } from './fileutils';
    import { PACKAGE_JSON_FILEDS } from '../constants';
    import { logWarning } from '../common';

    const getSortedObject = (obj) => {
        if (obj !== null && typeof obj === 'object' && !Array.isArray(obj)) {
            const keys = Object.keys(obj).sort();
            const newObj = {};
            const addedKeys = {};
            keys.forEach((v) => {
                if (!addedKeys[v]) {
                    newObj[v] = obj[v];
                    addedKeys[v] = true;
                } else {

                }
            });
            return newObj;
        } if (Array.isArray(obj)) {
            return obj.sort();
        }
        return obj;
    };

    const checkForDuplicates = (arr) => {
        const dupCheck = {};
        arr.forEach((v) => {
            if (v) {
                for (const k in v) {
                    if (dupCheck[k]) {
                        logWarning(`Key ${chalk.white(k)} is duplicated in your package.json`);
                    }
                    dupCheck[k] = true;
                }
            }
        });
    };

    const fixPackageJson = (c, pkgPath) => new Promise((resolve, reject) => {
        const pth = pkgPath || c.paths.project.package;
        const pp = readObjectSync(pth);
        const output = fixPackageObject(pp);
        writeObjectSync(pth, output, 4);
        resolve();
    });

    const fixPackageObject = (pp) => {
        const output = {};
        const usedKeys = {};
        PACKAGE_JSON_FILEDS.forEach((v) => {
            if (pp[v] !== null) {
                output[v] = getSortedObject(pp[v]);
                usedKeys[v] = true;
            }
        });
        for (const k in pp) {
            if (!usedKeys[k]) {
                output[k] = pp[k];
            }
        }
        checkForDuplicates([pp.dependencies, pp.devDependencies]);

        return output;
    };

    export { fixPackageJson, fixPackageObject };
    export default {
        fixPackageJson, fixPackageObject
    };