<commit_before>const childProcess = require("child_process");
const compiler = require("./compiler");
const fs = require("fs");

function compile(destination, files, overrides) {
    let minifiedData = [];

    files.forEach(filename => {
        minifiedData.push({
            filename: filename,
            content: compiler.compile(filename, overrides)
        });
    });

    let content = `// ${destination} -- DO NOT EDIT\n\n`;

    minifiedData.forEach(fileData => {
        content += `// ${fileData.filename}\n`;
        content += `(function (window, undefined) { ${fileData.content.code} })(this);`;
        content += "\n\n";
    });

    fs.writeFileSync(destination, content);
};

//
// step 1) build `WCF.Combined.min.js` and `WCF.Combined.tiny.min.js`
//
process.chdir("../wcfsetup/install/files/js/");
[true, false].forEach(COMPILER_TARGET_DEFAULT => {
    let output = "WCF.Combined" + (COMPILER_TARGET_DEFAULT ? "" : ".tiny") + ".min.js";
    console.time(output);
    {
        let data = fs.readFileSync(".buildOrder", "utf8");
        let files = data
            .trim()
            .split(/\r?\n/)
            .map(filename => `${filename}.js`);

        compile(output, files, {
            compress: {
                global_defs: {
                    COMPILER_TARGET_DEFAULT: COMPILER_TARGET_DEFAULT
                }
            }
        });
    }
    console.timeEnd(output);
});

//
// step 2) Redactor II + plugins
//
const redactorCombined = "redactor.combined.min.js";
process.chdir("3rdParty/redactor2/");

console.time(redactorCombined);
{
    let files = ['redactor.js'];
    fs.readdirSync("./plugins/").forEach(file => {
        file = `plugins/${file}`;
        let stat = fs.statSync(file);
        if (stat.isFile() && !stat.isSymbolicLink()) {
            files.push(file);
        }
    });

    compile(redactorCombined, files);
}
console.timeEnd(redactorCombined);

//
// step3) build rjs
//
const rjsCmd = (process.platform === "win32") ? "r.js.cmd" : "r.js";
process.chdir("../../");

{
    let configFile = "require.build.js";
    let outFilename = require(process.cwd() + `/${configFile}`).out;

    const promisePolyfill = fs.readFileSync("./3rdParty/polyfill/promise.min.js");

    [true, false].forEach(COMPILER_TARGET_DEFAULT => {
        let overrides = "uglify2.compress.global_defs.COMPILER_TARGET_DEFAULT=" + (COMPILER_TARGET_DEFAULT ? "true" : "false");
        if (!COMPILER_TARGET_DEFAULT) {
            outFilename = outFilename.replace(/\.min\.js$/, '.tiny.min.js');
            overrides += " out=" + outFilename;
        }

        console.time(outFilename);
        childProcess.execSync(`${rjsCmd} -o ${configFile} ${overrides}`, {
            cwd: process.cwd(),
            stdio: [0, 1, 2]
        });
        console.timeEnd(outFilename);

        // prepend the promise polyfill
        let content = `// promise.min.js\n${promisePolyfill}\n\n// ${outFilename}\n` + fs.readFileSync(outFilename);
        fs.writeFileSync(outFilename, content);
    });
}

//
// step 4) legacy ACP scripts
//
process.chdir("../acp/js/");

fs.readdirSync("./")
    .filter(filename => {
        let stat = fs.statSync(filename);
        if (stat.isFile() && !stat.isSymbolicLink()) {
            return filename.match(/\.js$/) && !filename.match(/\.min\.js$/);
        }

        return false;
    })
    .forEach(filename => {
        console.time(filename);
        {
            let output = compiler.compile(fs.readFileSync(process.cwd() + `/${filename}`, 'utf-8'));
            fs.writeFileSync(filename.replace(/\.js$/, '.min.js'), output.code);
        }
        console.timeEnd(filename);
    });<commit_msg>Adjusted the call to the compiler<commit_after>const childProcess = require("child_process");
const compiler = require("./compiler");
const fs = require("fs");

function compile(destination, files, overrides) {
	let minifiedData = [];

	files.forEach((filename) => {
		minifiedData.push({
			filename: filename,
			content: compiler.compile(fs.readFileSync(process.cwd() + `/${filename}`, "utf-8"), overrides),
		});
	});

	let content = `// ${destination} -- DO NOT EDIT\n\n`;

	minifiedData.forEach((fileData) => {
		content += `// ${fileData.filename}\n`;
		content += `(function (window, undefined) { ${fileData.content.code} })(this);`;
		content += "\n\n";
	});

	fs.writeFileSync(destination, content);
}

//
// step 1) build `WCF.Combined.min.js` and `WCF.Combined.tiny.min.js`
//
process.chdir("../wcfsetup/install/files/js/");
[true, false].forEach((COMPILER_TARGET_DEFAULT) => {
	let output = "WCF.Combined" + (COMPILER_TARGET_DEFAULT ? "" : ".tiny") + ".min.js";
	console.time(output);
	{
		let data = fs.readFileSync(".buildOrder", "utf8");
		let files = data
			.trim()
			.split(/\r?\n/)
			.map((filename) => `${filename}.js`);

		compile(output, files, {
			compress: {
				global_defs: {
					COMPILER_TARGET_DEFAULT: COMPILER_TARGET_DEFAULT,
				},
			},
		});
	}
	console.timeEnd(output);
});

//
// step 2) Redactor II + plugins
//
const redactorCombined = "redactor.combined.min.js";
process.chdir("3rdParty/redactor2/");

console.time(redactorCombined);
{
	let files = ["redactor.js"];
	fs.readdirSync("./plugins/").forEach((file) => {
		file = `plugins/${file}`;
		let stat = fs.statSync(file);
		if (stat.isFile() && !stat.isSymbolicLink()) {
			files.push(file);
		}
	});

	compile(redactorCombined, files);
}
console.timeEnd(redactorCombined);

//
// step3) build rjs
//
const rjsCmd = process.platform === "win32" ? "r.js.cmd" : "r.js";
process.chdir("../../");

{
	let configFile = "require.build.js";
	let outFilename = require(process.cwd() + `/${configFile}`).out;

	const promisePolyfill = fs.readFileSync("./3rdParty/polyfill/promise.min.js");

	[true, false].forEach((COMPILER_TARGET_DEFAULT) => {
		let overrides =
			"uglify2.compress.global_defs.COMPILER_TARGET_DEFAULT=" +
			(COMPILER_TARGET_DEFAULT ? "true" : "false");
		if (!COMPILER_TARGET_DEFAULT) {
			outFilename = outFilename.replace(/\.min\.js$/, ".tiny.min.js");
			overrides += " out=" + outFilename;
		}

		console.time(outFilename);
		childProcess.execSync(`${rjsCmd} -o ${configFile} ${overrides}`, {
			cwd: process.cwd(),
			stdio: [0, 1, 2],
		});
		console.timeEnd(outFilename);

		// prepend the promise polyfill
		let content =
			`// promise.min.js\n${promisePolyfill}\n\n// ${outFilename}\n` + fs.readFileSync(outFilename);
		fs.writeFileSync(outFilename, content);
	});
}

//
// step 4) legacy ACP scripts
//
process.chdir("../acp/js/");

fs.readdirSync("./")
	.filter((filename) => {
		let stat = fs.statSync(filename);
		if (stat.isFile() && !stat.isSymbolicLink()) {
			return filename.match(/\.js$/) && !filename.match(/\.min\.js$/);
		}

		return false;
	})
	.forEach((filename) => {
		console.time(filename);
		{
			let output = compiler.compile(fs.readFileSync(process.cwd() + `/${filename}`, "utf-8"));
			fs.writeFileSync(filename.replace(/\.js$/, ".min.js"), output.code);
		}
		console.timeEnd(filename);
	});
{'fraction_non_alphanumeric': 0.19426206109049388, 'fraction_numerical': 0.004424778761061947, 'mean_word_length': 2.952058657642414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2314404', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2387, 'n_words': 387}