<commit_before>/**
 * This module handles the loading of the jamrc files used to configure the
 * behaviour of the command-line tool.
 *
 * @module
 */

var utils = require('./utils'),
    async = require('async'),
    _ = require('underscore')._,
    path = require('path'),
    env = require('./env');


/**
 * Default paths to lookup when constructing values for jamrc.
 * Paths are checked in order, with later paths overriding the values obtained
 * from earlier ones.
 */

exports.PATHS = [
    //'/etc/jamrc',
    //'/usr/local/etc/jamrc',
    env.home + '/.jamrc'
];

/**
 * The defaults jamrc settings
 */

exports.DEFAULTS = {
    repositories: [ "http://packages.jamjs.org" ]
};


/**
 * Loads jamrc settings from PATHS, and merges them along with the DEFAULT
 * values, returning the result.
 *
 * @param {Function} callback
 */

exports.load = function (callback) {
    async.map(exports.PATHS, exports.loadFile, function (err, results) {
        var defaults = _.clone(exports.DEFAULTS);
        var settings = results.reduce(function (merged, r) {
            return exports.merge(merged, r);
        }, defaults);
        callback(null, settings);
    });
};


/**
 * Deep merge for JSON objects, overwrites conflicting properties
 *
 * @param {Object} a
 * @param {Object} b
 * @returns {Object}
 */

exports.merge = function (a, b) {
    if (!b) {
        return a;
    }
    for (var k in b) {
        if (Array.isArray(b[k])) {
            a[k] = b[k];
        }
        else if (typeof b[k] === 'object') {
            if (typeof a[k] === 'object') {
                exports.merge(a[k], b[k]);
            }
            else if (b.hasOwnProperty(k)) {
                a[k] = b[k];
            }
        }
        else if (b.hasOwnProperty(k)) {
            a[k] = b[k]
        }
    }
    return a;
};


/**
 * Checks a jamrc file exists and loads it if available. If the file does not
 * exist the function will respond with an empty object.
 *
 * @param {String} p - the path of the jamrc file to load
 * @param {Function} callback
 */

exports.loadFile = function (p, callback) {
    path.exists(p, function (exists) {
        if (exists) {
            try {
                var mod = require(utils.abspath(p));
            }
            catch (e) {
                return callback(e);
            }
            callback(null, mod);<commit_msg>fix jamrc file loading from home directory<commit_after>/**
 * This module handles the loading of the jamrc files used to configure the
 * behaviour of the command-line tool.
 *
 * @module
 */

var utils = require('./utils'),
    async = require('async'),
    _ = require('underscore')._,
    path = require('path'),
    env = require('./env');


/**
 * Default paths to lookup when constructing values for jamrc.
 * Paths are checked in order, with later paths overriding the values obtained
 * from earlier ones.
 */

exports.PATHS = [
    //'/etc/jamrc',
    //'/usr/local/etc/jamrc',
    env.home + '/.jamrc'
];

/**
 * The defaults jamrc settings
 */

exports.DEFAULTS = {
    repositories: [ "http://packages.jamjs.org" ]
};


/**
 * Loads jamrc settings from PATHS, and merges them along with the DEFAULT
 * values, returning the result.
 *
 * @param {Function} callback
 */

exports.load = function (callback) {
    async.map(exports.PATHS, exports.loadFile, function (err, results) {
        if (err) {
            return callback(err);
        }
        var defaults = _.clone(exports.DEFAULTS);
        var settings = results.reduce(function (merged, r) {
            return exports.merge(merged, r);
        }, defaults);
        callback(null, settings);
    });
};


/**
 * Deep merge for JSON objects, overwrites conflicting properties
 *
 * @param {Object} a
 * @param {Object} b
 * @returns {Object}
 */

exports.merge = function (a, b) {
    if (!b) {
        return a;
    }
    for (var k in b) {
        if (Array.isArray(b[k])) {
            a[k] = b[k];
        }
        else if (typeof b[k] === 'object') {
            if (typeof a[k] === 'object') {
                exports.merge(a[k], b[k]);
            }
            else if (b.hasOwnProperty(k)) {
                a[k] = b[k];
            }
        }
        else if (b.hasOwnProperty(k)) {
            a[k] = b[k]
        }
    }
    return a;
};


/**
 * Checks a jamrc file exists and loads it if available. If the file does not
 * exist the function will respond with an empty object.
 *
 * @param {String} p - the path of the jamrc file to load
 * @param {Function} callback
 */

exports.loadFile = function (p, callback) {
    path.exists(p, function (exists) {
        if (exists) {
            try {
                var mod = require(path.resolve(p));
            }
            catch (e) {
                return callback(e);
            }
            callback(null, mod);
{'fraction_non_alphanumeric': 0.13983840894965818, 'fraction_numerical': 0.0, 'mean_word_length': 2.0193871169480926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2583400', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1452, 'n_words': 449}