<commit_before>'use strict';

var alias = require('identify-resource').alias
	, clone = require('lodash/lang/clone')
	, cnsl = require('./utils/cnsl')
	, extend = require('lodash/object/extend')
	, fs = require('fs')
	, fsUtils = require('recur-fs')
	, glob = require('glob').sync
	, path = require('path')
	, unique = require('lodash/array/unique')

	, DEFAULT_FILE_EXTENSIONS = {
			js: ['js', 'json'],
			css: ['css'],
			html: ['html']}
	, DEFAULT_JS = 'buddy.js'
	, DEFAULT_JSON = 'buddy.json'
	, DEFAULT_OPTIONS = {
			compress: false,
			lint: false,
			script: false,
			lazy: false,
			reload: false,
			serve: false,
			watch: false,
			deploy: false,
			verbose: false,
			targets: ['js','css','html']}
	, DEFAULT_PACKAGE_JSON = 'package.json'
	, DEFAULT_SERVER = {
			directory: '.',
			port: 8080}
	, DEFAULT_SOURCES = {
			js: ['.'],
			css: ['.'],
			html: ['.']}
	, RE_GLOB = /[\*\[\{]/
	, RE_UNIQUE = /{(?:hash|date)}/

	, debug = cnsl.debug
	, error = cnsl.error
	, hunt = fsUtils.hunt.sync
	, indir = fsUtils.indir
	, strong = cnsl.strong
	, warn = cnsl.warn;

exports.data = {
	fileExtensions: extend({}, DEFAULT_FILE_EXTENSIONS),
	runtimeOptions: extend({}, DEFAULT_OPTIONS),
	server: extend({}, DEFAULT_SERVER)
};

/**
 * Load and parse configuration file
 * Accepts optional path to file or directory
 * @param {String | Object} [configpath]
 * @param {String} [fileExtensions]
 * @param {Object} [runtimeOptions]
 * @returns {Object}
 */
exports.load = function (configpath, fileExtensions, runtimeOptions) {
	var url, data;

	runtimeOptions = runtimeOptions || {};
	runtimeOptions.version = require(path.resolve(__dirname, '../package.json')).version;

	// Load
	if ('string' == typeof configpath || configpath == null) {
		url = exports.locate(configpath);
		data = require(url);
		// Set current directory to location of file
		process.chdir(path.dirname(url));
		// Store
		exports.data.url = url;

	// Passed in JSON object
	} else {
		data = configpath;
	}

	// Package.json
	if (data.buddy) data = data.buddy;
	extend(exports.data.fileExtensions, fileExtensions);
	// Override default runtimeOptions
	extend(exports.data.runtimeOptions, runtimeOptions);
	extend(exports.data.server, data.server);
	exports.data.script = data.script;

	// Validate
	if (data.build) {
		// Store
		exports.data.build = exports.parse(data.build, exports.data.runtimeOptions, exports.data.cache);
		// Error parsing
		if (!exports.data.build) throw new Error('invalid "build" data for ' + strong(url));
		return exports.data;
	} else {
		throw new Error('no "build" data for ' + strong(url));
	}

	return data;
};

/**
 * Locate the configuration file
 * Walks the directory tree if no file/directory specified
 * @param {String} [url]
 * @returns {String}
 */
exports.locate = function (url) {
	var configpath;

	function check (dir) {
			// Support js, json, and package.json
		var urljs = path.join(dir, DEFAULT_JS)
			, urljson = path.join(dir, DEFAULT_JSON)
			, urlpkgjson = path.join(dir, DEFAULT_PACKAGE_JSON)
			, url;

		if (fs.existsSync(url = urljs)
			|| fs.existsSync(url = urljson)
			|| fs.existsSync(url = urlpkgjson)) {
				return url;
		} else {
			return '';
		}
	}

	if (url) {
		configpath = path.resolve(url);
			try {
				// Try default file name if passed directory
				if (!path.extname(configpath).length || fs.statSync(configpath).isDirectory()) {
					configpath = check(configpath);
					if (!configpath) throw 'no default found';
				}
			} catch (err) {
				throw new Error(strong('buddy') + ' config not found in ' + strong(path.dirname(url)));
			}

	// No url specified
	} else {
		try {
			// Find the first instance of a DEFAULT file based on the current working directory
			configpath = hunt(process.cwd(), function (resource, stat) {
				if (stat.isFile()) {
					var basename = path.basename(resource);
					return (basename == DEFAULT_PACKAGE_JSON || basename == DEFAULT_JS || basename == DEFAULT_JSON);
				}
			}, true);
		} catch (err) {
			throw new Error(strong('buddy') + ' config not found');
		}
	}

	return configpath;
};

/**<commit_msg>guard against inadvertent throw<commit_after>'use strict';

var alias = require('identify-resource').alias
	, clone = require('lodash/lang/clone')
	, cnsl = require('./utils/cnsl')
	, extend = require('lodash/object/extend')
	, fs = require('fs')
	, fsUtils = require('recur-fs')
	, glob = require('glob').sync
	, path = require('path')
	, unique = require('lodash/array/unique')

	, DEFAULT_FILE_EXTENSIONS = {
			js: ['js', 'json'],
			css: ['css'],
			html: ['html']}
	, DEFAULT_JS = 'buddy.js'
	, DEFAULT_JSON = 'buddy.json'
	, DEFAULT_OPTIONS = {
			compress: false,
			lint: false,
			script: false,
			lazy: false,
			reload: false,
			serve: false,
			watch: false,
			deploy: false,
			verbose: false,
			targets: ['js','css','html']}
	, DEFAULT_PACKAGE_JSON = 'package.json'
	, DEFAULT_SERVER = {
			directory: '.',
			port: 8080}
	, DEFAULT_SOURCES = {
			js: ['.'],
			css: ['.'],
			html: ['.']}
	, RE_GLOB = /[\*\[\{]/
	, RE_UNIQUE = /{(?:hash|date)}/

	, debug = cnsl.debug
	, error = cnsl.error
	, hunt = fsUtils.hunt.sync
	, indir = fsUtils.indir
	, strong = cnsl.strong
	, warn = cnsl.warn;

exports.data = {
	fileExtensions: extend({}, DEFAULT_FILE_EXTENSIONS),
	runtimeOptions: extend({}, DEFAULT_OPTIONS),
	server: extend({}, DEFAULT_SERVER)
};

/**
 * Load and parse configuration file
 * Accepts optional path to file or directory
 * @param {String | Object} [configpath]
 * @param {String} [fileExtensions]
 * @param {Object} [runtimeOptions]
 * @returns {Object}
 */
exports.load = function (configpath, fileExtensions, runtimeOptions) {
	var url, data;

	runtimeOptions = runtimeOptions || {};
	runtimeOptions.version = require(path.resolve(__dirname, '../package.json')).version;

	// Load
	if ('string' == typeof configpath || configpath == null) {
		url = exports.locate(configpath);
		data = require(url);
		// Set current directory to location of file
		process.chdir(path.dirname(url));
		// Store
		exports.data.url = url;

	// Passed in JSON object
	} else {
		data = configpath;
	}

	// Package.json
	if (data.buddy) data = data.buddy;
	extend(exports.data.fileExtensions, fileExtensions);
	// Override default runtimeOptions
	extend(exports.data.runtimeOptions, runtimeOptions);
	extend(exports.data.server, data.server);
	exports.data.script = data.script;

	// Validate
	if (data.build) {
		// Store
		exports.data.build = exports.parse(data.build, exports.data.runtimeOptions, exports.data.cache);
		// Error parsing
		if (!exports.data.build) throw new Error('invalid "build" data for ' + strong(url));
		return exports.data;
	} else {
		throw new Error('no "build" data for ' + strong(url));
	}

	return data;
};

/**
 * Locate the configuration file
 * Walks the directory tree if no file/directory specified
 * @param {String} [url]
 * @returns {String}
 */
exports.locate = function (url) {
	var configpath;

	function check (dir) {
			// Support js, json, and package.json
		var urljs = path.join(dir, DEFAULT_JS)
			, urljson = path.join(dir, DEFAULT_JSON)
			, urlpkgjson = path.join(dir, DEFAULT_PACKAGE_JSON)
			, url;

		if (fs.existsSync(url = urljs)
			|| fs.existsSync(url = urljson)
			|| fs.existsSync(url = urlpkgjson)) {
				return url;
		} else {
			return '';
		}
	}

	if (url) {
		configpath = path.resolve(url);
			try {
				// Try default file name if passed directory
				if (!path.extname(configpath).length || fs.statSync(configpath).isDirectory()) {
					configpath = check(configpath);
					if (!configpath) throw 'no default found';
				}
			} catch (err) {
				throw new Error(strong('buddy') + ' config not found in ' + strong(path.dirname(url)));
			}

	// No url specified
	} else {
		try {
			// Find the first instance of a DEFAULT file based on the current working directory
			configpath = hunt(process.cwd(), function (resource, stat) {
				if (stat.isFile()) {
					var basename = path.basename(resource);
					return (basename == DEFAULT_PACKAGE_JSON || basename == DEFAULT_JS || basename == DEFAULT_JSON);
				}
			}, true);
		} catch (err) {
			if (!configpath) throw new Error(strong('buddy') + ' config not found');
		}
	}

	return configpath;
};

/**
{'fraction_non_alphanumeric': 0.15777966719300376, 'fraction_numerical': 0.000971699259079315, 'mean_word_length': 3.5744444444444445, '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': '1736753', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2739, 'n_words': 673}