all files / src/configLoader/ loader.js

100% Statements 8/8
75% Branches 9/12
100% Functions 2/2
100% Lines 8/8
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                                            14×           14×               14×           12× 36×       12×     36×          
import path from 'path';
 
import {findup, getContent} from '../configLoader';
 
export default loader;
 
/**
 * Command line config helpers
 * Shamelessly ripped from with slight modifications: 
 * https://github.com/jscs-dev/node-jscs/blob/master/lib/cli-config.js
 */
 
/**
 * Get content of the configuration file
 * @param {String} config - partial path to configuration file
 * @param {String} [cwd = process.cwd()] - directory path which will be joined with config argument
 * @return {Object|undefined}
 */
function loader(configs, config, cwd) {
    var content;
    var directory = cwd || process.cwd();
 
    // If config option is given, attempt to load it
    Iif (config) {
        return getContent(config, directory);
    }
 
    content = getContent(
        findup(configs, { nocase: true, cwd: directory }, function(configPath) {
            Eif (path.basename(configPath) === 'package.json') { 
                // return !!this.getContent(configPath);
            }
 
            return true;
        })
    );
 
    if (content) {
        return content;
    }
 
    // Try to load standard configs from home dir
    var directoryArr = [process.env.USERPROFILE, process.env.HOMEPATH, process.env.HOME];
    for (var i = 0, dirLen = directoryArr.length; i < dirLen; i++) {
        if (!directoryArr[i]) {
            continue;
        }
 
        for (var j = 0, len = configs.length; j < len; j++) {
            content = getContent(configs[j], directoryArr[i]);
 
            Iif (content) {
                return content;
            }
        }
    }
}