<commit_before><commit_msg>Added deploy script.<commit_after>const Vue = require("vue");
const fs = require("fs");

const path = require("path");

const template = require("./template.js");

const compression = require('compression');

const webpack = require("webpack");
const webpackMerge = require("webpack-merge");

const { createBundleRenderer } = require("vue-server-renderer");

const deploys = require("./deploys");

module.exports = async (websomServer, deploy) => {
	const tmp = path.resolve(websomServer.config.root, "../tmp");
	if (!fs.existsSync(tmp))
		fs.mkdirSync(tmp);

	let deployBundle = deploy ? deploy.bundle : "default";

	let config = require("./webpack.client.config")(() => websomServer, deployBundle, true);
	let serverConfig = require("./webpack.server.config.js")(() => websomServer, deployBundle, true);
	//let cssConfig = require("./webpack.css.config.js")(() => websomServer, deployBundle);

	let clientHooks = {};
	let serverHooks = {};

	if (!deploys[deploy.type]) {
		return "Invalid deploy type: " + deploy.type;
	}

	let outs = await ((deploys[deploy.type])({
		server: websomServer,
		deploy,
		api: "",
		template,
		bundles: {
			server: null,
			client: null
		},
		createBundleRenderer,
		tmp
	}));

	if (outs.server && outs.server.webpack) {
		serverConfig = webpackMerge(serverConfig, outs.server.webpack);

		serverHooks = outs.server.hooks || {};
	}

	if (outs.client && outs.client.webpack) {
		config = webpackMerge(config, outs.client.webpack);
		clientHooks = outs.client.hooks || {};
	}

	/*if (outs.css && outs.css.webpack) {
		cssConfig = webpackMerge(cssConfig, outs.css.webpack);
		cssHooks = outs.css.hooks || {};
	}*/

	let compiler = webpack(config);

	for (let ch in clientHooks) {
		compiler.hooks[ch].tap("deploy", clientHooks[ch]);
	}

	compiler.hooks.done.tap("renderWatcher", stats => {
		stats = stats.toJson();
		stats.errors.forEach(err => console.error(err));
		stats.warnings.forEach(err => console.warn(err));

		if (stats.errors.length)
			return;
	});

	const serverCompiler = webpack(serverConfig);
	
	for (let sh in serverHooks) {
		serverCompiler.hooks[sh].tap("deploy", serverHooks[sh]);
	}
	
	console.log("Starting build");

	await Promise.all([
			
		new Promise((res, rej) => {
			serverCompiler.run((err, stats) => {
				console.log(stats.errors);
				res();
			});
		}),

		new Promise((res, rej) => {
			compiler.run((err, stats) => {
				console.log(stats.errors);
				res();
			});
		})
	]);

	if (outs.finish) {
		await outs.finish();
	}
};
{'fraction_non_alphanumeric': 0.18066561014263074, 'fraction_numerical': 0.0, 'mean_word_length': 4.121703853955375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '5728660', 'n_tokens_mistral': 916, 'n_tokens_neox': 838, 'n_words': 166}