All files / src/utils getPresetConfig.js

100% Statements 24/24
100% Branches 9/9
100% Functions 1/1
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 43 44 451x 1x   1x 1x   1x   1x 4x 4x 4x   4x 1x 1x     3x   3x 3x         1x   2x   1x 1x       2x 1x 1x     1x     1x  
const path = require('path');
const fs = require('fs');
 
const getHomeDir = require('./getHomeDir');
const log = require('./log');
 
const CONFIG_NAME = '.synd.config.js';
 
const getPresetConfig = name => {
    const homeDir = getHomeDir();
    const configPath = path.resolve(homeDir, CONFIG_NAME);
    const syndConfig = {};
 
    if (!fs.existsSync(configPath)) {
        log.errorAndExit(`~/${CONFIG_NAME} does not exist`);
        return null;
    }
 
    try {
        /* eslint-disable-next-line */
        const userConfig = require(configPath);
        if (
            typeof userConfig !== 'object' ||
            userConfig === null ||
            Array.isArray(userConfig)
        ) {
            throw new Error('invalid config');
        }
        Object.assign(syndConfig, userConfig);
    } catch (e) {
        log.errorAndExit(`~/${CONFIG_NAME} is invalid`);
        return null;
    }
 
    // preset must exist
    if (!(name in syndConfig)) {
        log.errorAndExit(`${name} is not in your ${CONFIG_NAME} file`);
        return null;
    }
 
    return syndConfig[name];
};
 
module.exports = getPresetConfig;