All files / switchman-core/src/repo index.js

85.29% Statements 29/34
60% Branches 9/15
66.66% Functions 4/6
87.09% Lines 27/31

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 682x 2x 2x   2x   2x   5x 5x 9x 4x 4x       1x         3x   2x 2x     2x   2x 2x           2x       3x   3x 3x 3x 3x                   2x 2x 2x                 2x  
const os = require('os');
const writeFile = require('../file-tool/write-file');
const loadFile = require('../file-tool/read-file');
 
const switchman_global_config_location = `${os.homedir()}/.switchman/config.json`;
 
const core = {
    find(alias, repoConfigs) {
        let found = false;
        for (let i = 0; i < repoConfigs.length; i++) {
            if (repoConfigs[i].alias === alias) {
                found = true;
                return repoConfigs[i];
            }
        }
 
        return null;
    },
 
    //添加环境
    add(options = {}) {
        if (!options.alias) throw new Error('alias is null or undefined');
 
        let repoConfigs = this.getAll();
        Iif (repoConfigs.length === 0) {
            repoConfigs.push(options);
        } else {
            const repoConfig = this.find(options.alias, repoConfigs);
 
            if (repoConfig) {
                repoConfig.location = options.location;
            } else E{
                repoConfigs.push(options);
            }
        }
 
        writeFile(JSON.stringify(repoConfigs), switchman_global_config_location);
    },
 
    get(alias) {
        Iif (!alias) throw new Error('alias is null or undefined');
 
        const configStr = loadFile(switchman_global_config_location).toString();
        Eif (configStr) {
            const repoConfigs = JSON.parse(configStr);
            return this.find(alias, repoConfigs);
        }
        return null;
    },
 
    update() {
 
    },
 
    getAll() {
        const configStr = loadFile(switchman_global_config_location).toString();
        Eif (configStr) {
            return JSON.parse(configStr);
        }
 
        return [];
    },
    remove() {
    }
};
 
module.exports = core;