All files / core/repositories config.repository.ts

27.41% Statements 17/62
2.38% Branches 1/42
50% Functions 3/6
26.66% Lines 16/60

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x   1x   1x 1x 1x     1x             1x 1x     1x 1x 1x   1x     1x             1x                                                                                                                                                                                     1x   1x  
import Logger from '../../infrastructure/logging/logger';
 
import { COMPODOC_DEFAULTS } from '../defaults';
 
import { InternalConfiguration } from '../entities/internal-configuration';
import { PublicConfiguration } from '../entities/public-configuration';
import { Flag, PUBLIC_FLAGS } from '../entities/public-flags';
import { CLIProgram } from '../entities/cli-program';
 
export class ConfigurationRepository {
    private static instance: ConfigurationRepository;
 
    public publicConfiguration: PublicConfiguration;
    public internalConfiguration: InternalConfiguration;
 
    constructor() {
        this.publicConfiguration = new PublicConfiguration();
        this.internalConfiguration = new InternalConfiguration();
    }
 
    public static getInstance() {
        Eif (!ConfigurationRepository.instance) {
            ConfigurationRepository.instance = new ConfigurationRepository();
        }
        return ConfigurationRepository.instance;
    }
 
    public update(configExplorerResult) {
        this.publicConfiguration = Object.assign(
            this.publicConfiguration,
            configExplorerResult.config
        );
    }
 
    public init(currentProgram: CLIProgram) {
        PUBLIC_FLAGS.forEach((publicFlag: Flag) => {
            if (this.publicConfiguration[publicFlag.label]) {
                this.internalConfiguration[publicFlag.label] = this.publicConfiguration[
                    publicFlag.label
                ];
            }
 
            if (currentProgram.hasOwnProperty(publicFlag.label)) {
                this.internalConfiguration[publicFlag.label] = currentProgram[publicFlag.label];
            }
        });
 
        /**
         * Specific use-cases flags
         */
 
        if (this.publicConfiguration.coverageTest) {
            this.internalConfiguration.coverageTest = 0;
            this.internalConfiguration.coverageTestThreshold =
                typeof this.publicConfiguration.coverageTest === 'string'
                    ? parseInt(this.publicConfiguration.coverageTest, 10)
                    : COMPODOC_DEFAULTS.defaultCoverageThreshold;
        }
        if (currentProgram.coverageTest) {
            this.internalConfiguration.coverageTest = 0;
            this.internalConfiguration.coverageTestThreshold =
                typeof currentProgram.coverageTest === 'string'
                    ? parseInt(currentProgram.coverageTest, 10)
                    : COMPODOC_DEFAULTS.defaultCoverageThreshold;
        }
 
        if (this.publicConfiguration.coverageMinimumPerFile) {
            this.internalConfiguration.coverageTestPerFile = true;
            this.internalConfiguration.coverageMinimumPerFile =
                typeof this.publicConfiguration.coverageMinimumPerFile === 'string'
                    ? parseInt(this.publicConfiguration.coverageMinimumPerFile, 10)
                    : COMPODOC_DEFAULTS.defaultCoverageMinimumPerFile;
        }
        if (currentProgram.coverageMinimumPerFile) {
            this.internalConfiguration.coverageTestPerFile = true;
            this.internalConfiguration.coverageMinimumPerFile =
                typeof currentProgram.coverageMinimumPerFile === 'string'
                    ? parseInt(currentProgram.coverageMinimumPerFile, 10)
                    : COMPODOC_DEFAULTS.defaultCoverageMinimumPerFile;
        }
 
        if (this.publicConfiguration.coverageTestThresholdFail) {
            this.internalConfiguration.coverageTestThresholdFail = this.publicConfiguration.coverageTestThresholdFail;
        }
        if (currentProgram.coverageTestThresholdFail) {
            this.internalConfiguration.coverageTestThresholdFail =
                currentProgram.coverageTestThresholdFail;
        }
 
        if (this.publicConfiguration.host) {
            this.internalConfiguration.host = this.publicConfiguration.host;
            this.internalConfiguration.hostname = this.publicConfiguration.host;
        }
        if (currentProgram.host) {
            this.internalConfiguration.host = currentProgram.host;
            this.internalConfiguration.hostname = currentProgram.host;
        }
 
        if (this.publicConfiguration.minimal) {
            this.internalConfiguration.disableSearch = true;
            this.internalConfiguration.disableRoutesGraph = true;
            this.internalConfiguration.disableGraph = true;
            this.internalConfiguration.disableCoverage = true;
        }
        if (currentProgram.minimal) {
            this.internalConfiguration.disableSearch = true;
            this.internalConfiguration.disableRoutesGraph = true;
            this.internalConfiguration.disableGraph = true;
            this.internalConfiguration.disableCoverage = true;
        }
 
        if (
            currentProgram.navTabConfig &&
            JSON.parse(currentProgram.navTabConfig).length !== COMPODOC_DEFAULTS.navTabConfig.length
        ) {
            this.internalConfiguration.navTabConfig = JSON.parse(currentProgram.navTabConfig);
        }
 
        if (this.publicConfiguration.silent) {
            Logger.silent = true;
        }
        if (currentProgram.silent) {
            Logger.silent = true;
        }
    }
}
 
export default ConfigurationRepository.getInstance();