<commit_before>#!/usr/bin/env node

var path = require("path"),
	fs = require("fs"),
	spawn = require('child_process').spawn;

var SCRIPT_BAT = "/tools/deploy.bat",
	SCRIPT_SH = "/tools/deploy.sh",
	DEPLOY_DIR = "/deploy",
	WWW = "www";

function findBootplate() {
	var deploy = SCRIPT_SH;
	if(process.platform.indexOf("win")===0) {
		deploy = SCRIPT_BAT;
	}
	try {
		if(fs.existsSync(WWW + deploy)) {
			return WWW;
		} else { //search for non-www bootplate
			var root = fs.readdirSync("./");
			for(var i=0; i<root.length; i++) {
				if(fs.existsSync(root[i] + deploy)) {
					return root[i];
				}
			}
		}
	} catch(e) {
		console.log("Error encountered while searching for bootplate\n" + e.message);
	}
}

function runBuildScript(bpDir, success) {
	var rootDir = process.cwd();
	process.chdir(bpDir);
	var child;
	if(process.platform.indexOf("win")===0) {
		child = spawn(process.env.COMSPEC || "cmd.exe", ["/c", path.join(bpDir, SCRIPT_BAT)],
				{stdio: "inherit"});
	} else {
		child = spawn(process.env.SHELL || "sh", [bpDir + SCRIPT_SH], {stdio: "inherit"});
	}
	child.on("close", function(code) {
		process.chdir(rootDir);
		if(code===0) {
			success();
		} else {
			updateWWW(bpDir);
		}
	});
}

function findDeployed(bpDir) {
	try {
		if(fs.existsSync(bpDir + DEPLOY_DIR + "/index.html")) {
			return bpDir + DEPLOY_DIR;
		} else if(fs.existsSync(bpDir + DEPLOY_DIR)) {
			//search for old-format and non-standard deploy output locations
			var bpContents = fs.readdirSync(bpDir + DEPLOY_DIR);
			// sort by age and some previous bootplates that allow multiple deploy directories
			bpContents.sort(function(a, b) {
				return fs.statSync(bpDir + DEPLOY_DIR + b).mtime.getTime() -
						fs.statSync(bpDir + DEPLOY_DIR + a).mtime.getTime();
			});
			for(var i=0; i<bpContents.length; i++) {
				if(fs.existsSync(bpDir + DEPLOY_DIR + "/" + bpContents[i] + "/index.html")) {
					return bpDir + DEPLOY_DIR + "/" + bpContents[i];
				}
			}
		}
	} catch(e) {
		console.log("Error encountered while searching for deployed bootplate\n" + e.message);
	}
}

function updateWWW(dir) {
	if(fs.lstatSync(WWW).isSymbolicLink()) {
		fs.unlinkSync(WWW);
	} else if(fs.readdirSync(WWW).length===0) {
		fs.rmdirSync(WWW);
	}
	if(!fs.existsSync(WWW)) {
		fs.symlinkSync(path.resolve(dir), WWW, 'dir');
	}
}

var bootplate = findBootplate();
if(bootplate) {
	var realBootplate = fs.realpathSync(bootplate);
	var cmdline = process.env.CORDOVA_CMDLINE || "";
	if(cmdline.indexOf("-no-deploy")>=0 || cmdline.indexOf("-enyo-debug")>=0
			|| (process.env.CORDOVA_PLATFORMS || process.env.CORDOVA_PLATFORMS==="webos")) {
		updateWWW(realBootplate);
	} else {
		runBuildScript(realBootplate, function() {
			var deploy = findDeployed(realBootplate);
			if(deploy) {
				updateWWW(deploy);
			} else {
				updateWWW(realBootplate);<commit_msg>Syntax fix<commit_after>#!/usr/bin/env node

var path = require("path"),
	fs = require("fs"),
	spawn = require('child_process').spawn;

var SCRIPT_BAT = "/tools/deploy.bat",
	SCRIPT_SH = "/tools/deploy.sh",
	DEPLOY_DIR = "/deploy",
	WWW = "www";

function findBootplate() {
	var deploy = SCRIPT_SH;
	if(process.platform.indexOf("win")===0) {
		deploy = SCRIPT_BAT;
	}
	try {
		if(fs.existsSync(WWW + deploy)) {
			return WWW;
		} else { //search for non-www bootplate
			var root = fs.readdirSync("./");
			for(var i=0; i<root.length; i++) {
				if(fs.existsSync(root[i] + deploy)) {
					return root[i];
				}
			}
		}
	} catch(e) {
		console.log("Error encountered while searching for bootplate\n" + e.message);
	}
}

function runBuildScript(bpDir, success) {
	var rootDir = process.cwd();
	process.chdir(bpDir);
	var child;
	if(process.platform.indexOf("win")===0) {
		child = spawn(process.env.COMSPEC || "cmd.exe", ["/c", path.join(bpDir, SCRIPT_BAT)],
				{stdio: "inherit"});
	} else {
		child = spawn(process.env.SHELL || "sh", [bpDir + SCRIPT_SH], {stdio: "inherit"});
	}
	child.on("close", function(code) {
		process.chdir(rootDir);
		if(code===0) {
			success();
		} else {
			updateWWW(bpDir);
		}
	});
}

function findDeployed(bpDir) {
	try {
		if(fs.existsSync(bpDir + DEPLOY_DIR + "/index.html")) {
			return bpDir + DEPLOY_DIR;
		} else if(fs.existsSync(bpDir + DEPLOY_DIR)) {
			//search for old-format and non-standard deploy output locations
			var bpContents = fs.readdirSync(bpDir + DEPLOY_DIR);
			// sort by age and some previous bootplates that allow multiple deploy directories
			bpContents.sort(function(a, b) {
				return fs.statSync(bpDir + DEPLOY_DIR + b).mtime.getTime() -
						fs.statSync(bpDir + DEPLOY_DIR + a).mtime.getTime();
			});
			for(var i=0; i<bpContents.length; i++) {
				if(fs.existsSync(bpDir + DEPLOY_DIR + "/" + bpContents[i] + "/index.html")) {
					return bpDir + DEPLOY_DIR + "/" + bpContents[i];
				}
			}
		}
	} catch(e) {
		console.log("Error encountered while searching for deployed bootplate\n" + e.message);
	}
}

function updateWWW(dir) {
	if(fs.lstatSync(WWW).isSymbolicLink()) {
		fs.unlinkSync(WWW);
	} else if(fs.readdirSync(WWW).length===0) {
		fs.rmdirSync(WWW);
	}
	if(!fs.existsSync(WWW)) {
		fs.symlinkSync(path.resolve(dir), WWW, 'dir');
	}
}

var bootplate = findBootplate();
if(bootplate) {
	var realBootplate = fs.realpathSync(bootplate);
	var cmdline = process.env.CORDOVA_CMDLINE || "";
	if(cmdline.indexOf("-no-deploy")>=0 || cmdline.indexOf("-enyo-debug")>=0
			|| (process.env.CORDOVA_PLATFORMS && process.env.CORDOVA_PLATFORMS==="webos")) {
		updateWWW(realBootplate);
	} else {
		runBuildScript(realBootplate, function() {
			var deploy = findDeployed(realBootplate);
			if(deploy) {
				updateWWW(deploy);
			} else {
				updateWWW(realBootplate);
{'fraction_non_alphanumeric': 0.17629394099982335, 'fraction_numerical': 0.0028263557675322383, 'mean_word_length': 3.984154929577465, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5694388', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2171, 'n_words': 380}