<commit_before>/*
 * titanium.js: Top-level include for the Titanium CLI
 *
 * Copyright (c) 2012, Appcelerator, Inc.  All Rights Reserved.
 * See the LICENSE file for more information.
 */

var config = require('./config').load(),
	appc = require('node-appc'),
	i18n = appc.i18n(__dirname),
	__ = i18n.__,
	__n = i18n.__n,
	env = appc.environ,
	afs = appc.fs,
	fs = require('fs'),
	async = require('async'),
	colors = require('colors'),
	path = require('path'),
	cli = require('./cli'),
	pkginfo = appc.pkginfo.package(module, 'version', 'about');

require('longjohn');

config.cli.colors || (colors.mode = 'none');

global.tierror = function () {
	var args = Array.prototype.slice.call(arguments);
	args[0] = ('[ERROR] ' + args[0]).red;
	console.error.apply(null, args);
};

global.tiexception = function (ex) {
	if (ex.stack) {
		ex.stack.split('\n').forEach(console.error);
	} else {
		console.error(ex.toString());
	}
};

cli.addAnalyticsEvent = appc.analytics.addEvent;
cli.sendAnalytics = sendAnalytics;

function sendAnalytics () {
	// sadly, this will never be called on Windows if the user presses ctrl+c
	appc.analytics.send(appc.util.mix({
		appId: pkginfo.about.id,
		appName: pkginfo.about.name,
		appGuid: 'cf5c67ed-1c3b-494b-afe0-01b958ef0f40',
		directory: path.join('~', '.titanium'),
		version: pkginfo.version,
		deployType: 'production'
	}, appc.auth.status()));
}

// when the app exits, submit analytics
process.on('exit', sendAnalytics);

// find all built-in commands
env.scanCommands(env.commands, path.join(path.dirname(module.filename), 'commands'));

// find all commands in the global commands path
(function (paths) {
	if (paths) {
		Array.isArray(paths) || (paths = [paths]);
		paths.forEach(function (p) {
			env.scanCommands(env.commands, appc.fs.resolvePath(p));
		});
	}
}(config.paths.commands));

// initialize the cli processor
cli.env = env;
cli.version = pkginfo.version;
cli.flag('help', {
		abbr: 'h',
		callback: function (value, logger) {
			if (value) {
				cli.argv._.unshift(cli.argv.$command)
				cli.argv.$command = 'help';
			}
		},
		desc: __('displays help')
	})
	.flag('version', {
		abbr: 'v',
		callback: function (value, logger) {
			value && (cli.argv.$command = 'version');
		},
		desc: __('displays the current version')
	})
	.flag('colors', {
		callback: function (value, logger) {
			var c = value !== false;
			colors.mode = c ? 'console' : 'none';
			Object.keys(logger.transports).forEach(function (name) {
				logger.transports[name].colorize = c;
			});
		},
		default: true,
		desc: __('use colors in the terminal'),
		negate: true
	})
	.flag('quiet', {
		abbr: 'q',
		callback: function (value, logger) {
			logger.silence(config.cli.quiet = value);
		},
		default: false,
		desc: __('suppress all output')
	})
	.flag('prompt', {
		callback: function (value, logger) {
			config.cli.prompt = !!value;
		},
		default: true,
		desc: __('prompt for missing options'),
		negate: true
	})
	.flag('banner', {
		callback: function (value, logger) {
			logger.bannerEnabled(!!value);
		},
		default: true,
		desc: __('displays Titanium version banner'),
		negate: true
	})
	.then(function (logger) {
		// find all hooks in the user's config paths
		(function (paths) {
			if (paths) {
				Array.isArray(paths) || (paths = [paths]);
				paths.forEach(function (p) {
					env.scanHooks(appc.fs.resolvePath(p));
				});
			}
		}(config.paths.hooks));
		
		function initCommands(ctx, cmds, sdk, platform) {
			Object.keys(cmds).forEach(function (name) {
				cli.command(name, {
					context: ctx,
					modulePath: cmds[name]
				}, sdk, platform);
			});
		}
		
		initCommands('Global command', env.commands);
		Object.keys(env.sdks).forEach(function (sdk) {
			initCommands('SDK ' + sdk + ' global command', env.sdks[sdk].commands, sdk);
			Object.keys(env.sdks[sdk].platforms).forEach(function (platform) {
				initCommands('SDK ' + sdk + ' platform ' + platform + ' command', env.sdks[sdk].platforms[platform].commands, sdk, platform);
			});
		});
		
		async.series([
			cli.parse,
			function (next) {
				var cmd = cli.argv.$command;
				if (!(cmd && cli.cmds[cmd] && (
					(cli.cmds[cmd].__global__ && cli.cmds[cmd].__global__.skipBanner) ||
					(cli.sdk != null && cli.cmds[cmd][cli.sdk.name] && cli.cmds[cmd][cli.sdk.name].__global__ && cli.cmds[cmd][cli.sdk.name].__global__.skipBanner)
				))) {
					logger.banner();
				}
				next();
			},
			cli.validate,
			function (finished) {
				cli.run();
				finished();
			}
		]);
	});<commit_msg>[TIMOB-11305] Bootstrapped ti to run after locale detection<commit_after>/*
 * titanium.js: Top-level include for the Titanium CLI
 *
 * Copyright (c) 2012, Appcelerator, Inc.  All Rights Reserved.
 * See the LICENSE file for more information.
 */

var fs = require('fs'),
	path = require('path');

// Read the locale and bootstrap the CLI as necessary
(function() {
	var configFilePath = path.join(process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'], '.titanium', 'config.json'),
		config;
	
	function detectLocale(callback) {
		var exec = require('child_process').exec;
		if (process.platform == 'win32') {
			exec('reg query "HKCU\\Control Panel\\International" /v Locale', function(err, stdout, stderr) {
				if (err) return callback();
				var m = stdout.match(/Locale\s+REG_SZ\s+(.+)/);
				if (m) {
					m = m[1].substring(m[1].length - 4, m[1].length);
					exec('reg query "HKLM\\SOFTWARE\\Classes\\MIME\\Database\\Rfc1766" /v ' + m, function(err, stdout, stderr) {
						if (!err) {
							var m = stdout.match(/REG_SZ\s+([^;,\n]+?);/);
							if (m) return callback(m[1]);
						}
						callback();
					});
					return;
				}
			});
		} else {
			exec('locale', function (err, stdout, stderr) {
				callback(stdout.split('\n').shift().replace(/(LANG=["']?([^\."']+).*)/m, '$2'));
			});
		}
	}
	
	function setLocale(config, callback) {
		detectLocale(function (locale) {
			config.user || (config.user = {});
			config.user.locale = locale && locale.trim().toLowerCase().replace(/_/g, '-') || 'en';
			fs.writeFileSync(configFilePath, JSON.stringify(config, false, '\t'));
			run();
		});
	}
	
	if ((fs.existsSync || path.existsSync)(configFilePath)) {
		try {
			config = JSON.parse(fs.readFileSync(configFilePath));
		} catch(e) {
			run();
			return;
		}
		if (!config || !config.user || !config.user.locale) {
			setLocale(config);
		} else {
			run();
		}
	} else {
		setLocale({});
	}
})();

function run() {

	global.tierror = function () {
		var args = Array.prototype.slice.call(arguments);
		args[0] = ''.red ? ('[ERROR] ' + args[0]).red : ('[ERROR] ' + args[0]);
		console.log();
		console.error.apply(null, args);
		console.log();
	};

	global.tiexception = function (ex) {
		if (ex.stack) {
			ex.stack.split('\n').forEach(console.error);
		} else {
			console.error(ex.toString());
		}
	};

	var colors = require('colors'),
		config = require('./config').load(),
		appc = require('node-appc'),
		i18n = appc.i18n(__dirname),
		__ = i18n.__,
		__n = i18n.__n,
		env = appc.environ,
		afs = appc.fs,
		async = require('async'),
		cli = require('./cli'),
		pkginfo = appc.pkginfo.package(module, 'version', 'about');

	require('longjohn');

	config.cli.colors || (colors.mode = 'none');

	cli.addAnalyticsEvent = appc.analytics.addEvent;
	cli.sendAnalytics = sendAnalytics;

	function sendAnalytics () {
		// sadly, this will never be called on Windows if the user presses ctrl+c
		appc.analytics.send(appc.util.mix({
			appId: pkginfo.about.id,
			appName: pkginfo.about.name,
			appGuid: 'cf5c67ed-1c3b-494b-afe0-01b958ef0f40',
			directory: path.join('~', '.titanium'),
			version: pkginfo.version,
			deployType: 'production'
		}, appc.auth.status()));
	}

	// when the app exits, submit analytics
	process.on('exit', sendAnalytics);

	// find all built-in commands
	env.scanCommands(env.commands, path.join(path.dirname(module.filename), 'commands'));

	// find all commands in the global commands path
	(function (paths) {
		if (paths) {
			Array.isArray(paths) || (paths = [paths]);
			paths.forEach(function (p) {
				env.scanCommands(env.commands, appc.fs.resolvePath(p));
			});
		}
	}(config.paths.commands));

	// initialize the cli processor
	cli.env = env;
	cli.version = pkginfo.version;
	cli.flag('help', {
			abbr: 'h',
			callback: function (value, logger) {
				if (value) {
					cli.argv._.unshift(cli.argv.$command)
					cli.argv.$command = 'help';
				}
			},
			desc: __('displays help')
		})
		.flag('version', {
			abbr: 'v',
			callback: function (value, logger) {
				value && (cli.argv.$command = 'version');
			},
			desc: __('displays the current version')
		})
		.flag('colors', {
			callback: function (value, logger) {
				var c = value !== false;
				colors.mode = c ? 'console' : 'none';
				Object.keys(logger.transports).forEach(function (name) {
					logger.transports[name].colorize = c;
				});
			},
			default: true,
			desc: __('use colors in the terminal'),
			negate: true
		})
		.flag('quiet', {
			abbr: 'q',
			callback: function (value, logger) {
				logger.silence(config.cli.quiet = value);
			},
			default: false,
			desc: __('suppress all output')
		})
		.flag('prompt', {
			callback: function (value, logger) {
				config.cli.prompt = !!value;
			},
			default: true,
			desc: __('prompt for missing options'),
			negate: true
		})
		.flag('banner', {
			callback: function (value, logger) {
				logger.bannerEnabled(!!value);
			},
			default: true,
			desc: __('displays Titanium version banner'),
			negate: true
		})
		.then(function (logger) {
			// find all hooks in the user's config paths
			(function (paths) {
				if (paths) {
					Array.isArray(paths) || (paths = [paths]);
					paths.forEach(function (p) {
						env.scanHooks(appc.fs.resolvePath(p));
					});
				}
			}(config.paths.hooks));
		
			function initCommands(ctx, cmds, sdk, platform) {
				Object.keys(cmds).forEach(function (name) {
					cli.command(name, {
						context: ctx,
						modulePath: cmds[name]
					}, sdk, platform);
				});
			}
		
			initCommands('Global command', env.commands);
			Object.keys(env.sdks).forEach(function (sdk) {
				initCommands('SDK ' + sdk + ' global command', env.sdks[sdk].commands, sdk);
				Object.keys(env.sdks[sdk].platforms).forEach(function (platform) {
					initCommands('SDK ' + sdk + ' platform ' + platform + ' command', env.sdks[sdk].platforms[platform].commands, sdk, platform);
				});
			});
		
			async.series([
				cli.parse,
				function (next) {
					var cmd = cli.argv.$command;
					if (!(cmd && cli.cmds[cmd] && (
						(cli.cmds[cmd].__global__ && cli.cmds[cmd].__global__.skipBanner) ||
						(cli.sdk != null && cli.cmds[cmd][cli.sdk.name] && cli.cmds[cmd][cli.sdk.name].__global__ && cli.cmds[cmd][cli.sdk.name].__global__.skipBanner)
					))) {
						logger.banner();
					}
					next();
				},
				cli.validate,
				function (finished) {
					cli.run();
					finished();
				}
			]);
		});
}
{'fraction_non_alphanumeric': 0.18531724264033644, 'fraction_numerical': 0.007496800146279028, 'mean_word_length': 3.352964584162356, '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': '179201', 'n_tokens_mistral': 4520, 'n_tokens_neox': 3842, 'n_words': 791}