Code coverage report for htmlcs/lib/config.js

Statements: 97.06% (33 / 34)      Branches: 83.33% (10 / 12)      Functions: 100% (6 / 6)      Lines: 97.06% (33 / 34)      Ignored: none     

All files » htmlcs/lib/ » config.js
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          1 1   1   1   1   1 43 43     1 41 41     41   1       40     1 1 1     1 26     26 25       1         1     1 26   26   26 26     1     25     1 43     1        
/**
 * @file get config
 * @author nighca<nighca@live.cn>
 */
 
var fs = require('fs');
var path = require('path');
 
var stripJsonComments = require('strip-json-comments');
 
var util = require('./util');
 
var configFileName = '.htmlcsrc';
 
var getConfigFileUnder = function (dirPath) {
    var configFilePath = path.resolve(dirPath, configFileName);
    return fs.existsSync(configFilePath) ? configFilePath : null;
};
 
var getConfigFileUnderOrUp = function (dirPath) {
    var parentDirPath = path.resolve(dirPath, '../');
    var configFile = getConfigFileUnder(dirPath);
 
    // arrives root
    if (dirPath === parentDirPath) {
        // return result no matter file exists or not
        return configFile;
    }
 
    // if file doesn't exist, find in parent dir
    return configFile || getConfigFileUnderOrUp(parentDirPath);
};
 
var getConfigFileUnderHomePath = function () {
    var homePath = util.getHomePath();
    return homePath ? getConfigFileUnder(homePath) : null;
};
 
var getConfigFile = util.cachable(function (dirPath) {
    var configFile;
 
    // search config file from dir to root
    if (configFile = getConfigFileUnderOrUp(dirPath)) {
        return configFile;
    }
 
    // search file in home path
    Iif (configFile = getConfigFileUnderHomePath()) {
        return configFile;
    }
 
    // use default config file
    return getConfigFileUnder(path.join(util.app.root, 'lib/default'));
});
 
var loadConfig = util.cachable(function (dirPath, refresh) {
    var configFilePath = getConfigFile(dirPath, refresh);
 
    var configFile = fs.readFileSync(configFilePath, {encoding: 'utf-8'});
 
    try {
        var cfg = JSON.parse(stripJsonComments(configFile));
    }
    catch (e) {
        throw new Error('Failed to parse config file: ' + configFilePath);
    }
 
    return cfg;
});
 
var loadFileConfig = function (filePath, refresh) {
    return loadConfig(path.dirname(filePath), refresh);
};
 
module.exports = {
    fileName: configFileName,
    load: loadFileConfig
};