all files / src/configLoader/ getContent.js

88.89% Statements 16/18
75% Branches 6/8
100% Functions 1/1
88.89% Lines 16/18
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                     50×       50× 50× 50×   50×   50× 14×   14× 14×                     14×       50×    
import fs from 'fs';
import path from 'path';
import stripJSONComments from 'strip-json-comments';
 
import {getNormalizedConfig} from '../configLoader';
 
export default getContent;
 
/**
 * Get content of the configuration file
 * @param {String} config - partial path to configuration file
 * @param {String} directory - directory path which will be joined with config argument
 * @return {Object}
 */
function getContent(config, directory) {
    Iif (!config) {
        return;
    }
 
    var configPath = path.resolve(directory, config);
    var ext;
    var content;
 
    config = path.basename(config);
 
    if (fs.existsSync(configPath)) {
        ext = path.extname(configPath);
 
        Eif (ext === '.js' || ext === '.json') {
            content = JSON.parse(fs.readFileSync(configPath, 'utf8'));
        } else {
            content = JSON.parse(
                stripJSONComments(
                    fs.readFileSync(configPath, 'utf8')
                )
            );
        }
 
        // Adding property via Object.defineProperty makes it
        // non-enumerable and avoids warning for unsupported rules
        Object.defineProperty(content, 'configPath', {
            value: configPath
        });
    }
    return getNormalizedConfig(config, content);
    
};