summary refs log tree commit diff
path: root/scripts/build
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/build')
-rw-r--r--scripts/build/clean.js18
-rw-r--r--scripts/build/compile_tsc.js48
-rw-r--r--scripts/build/plugin_prepare.js31
-rw-r--r--scripts/build/plugin_resources.js13
-rw-r--r--scripts/build/remap_imports.js15
5 files changed, 125 insertions, 0 deletions
diff --git a/scripts/build/clean.js b/scripts/build/clean.js
new file mode 100644
index 00000000..92ec6d77
--- /dev/null
+++ b/scripts/build/clean.js
@@ -0,0 +1,18 @@
+const { execSync } = require("child_process");
+const path = require("path");
+const fs = require("fs");
+const { argv, stdout, exit } = require("process");
+const { execIn, parts, getDirs, walk, sanitizeVarName } = require("../utils");
+
+module.exports = function (config) {
+	if (fs.existsSync(config.buildLog)) fs.rmSync(config.buildLog);
+	if (fs.existsSync(config.buildLogAnsi)) fs.rmSync(config.buildLogAnsi);
+
+	if (config.clean) {
+		console.log(`==> Cleaning...`);
+		if (fs.existsSync(config.distDir)) {
+			fs.rmSync(config.distDir, { recursive: true });
+			if (config.verbose) console.log(`Deleted ${path.resolve(config.distDir)}!`);
+		}
+	}
+};
diff --git a/scripts/build/compile_tsc.js b/scripts/build/compile_tsc.js
new file mode 100644
index 00000000..179707a3
--- /dev/null
+++ b/scripts/build/compile_tsc.js
@@ -0,0 +1,48 @@
+const { execSync } = require("child_process");
+const path = require("path");
+const fs = require("fs");
+const { argv, stdout, exit } = require("process");
+const { execIn, parts, getDirs, walk, sanitizeVarName } = require("../utils");
+
+module.exports = function (config) {
+	console.log("==> Compiling source with tsc...");
+	let buildFlags = "";
+	if (config.pretty) buildFlags += "--pretty ";
+
+	try {
+		execSync(
+			'node "' +
+				path.join(config.rootDir, "node_modules", "typescript", "lib", "tsc.js") +
+				'" -p "' +
+				path.join(config.rootDir) +
+				'" ' +
+				buildFlags,
+			{
+				cwd: path.join(config.rootDir),
+				shell: true,
+				env: process.env,
+				encoding: "utf8"
+			}
+		);
+	} catch (error) {
+		if (config.verbose || config.logerr) {
+			error.stdout.split(/\r?\n/).forEach((line) => {
+				let _line = line.replace("dist/", "", 1);
+				if (!config.pretty && _line.includes(".ts(")) {
+					//reformat file path for easy jumping
+					_line = _line.replace("(", ":", 1).replace(",", ":", 1).replace(")", "", 1);
+				}
+				console.error(_line);
+			});
+		}
+		console.error(`Build failed! Please check build.log for info!`);
+		if (!config.silent) {
+			if (config.pretty) fs.writeFileSync(path.join(config.rootDir, "build.log.ansi"), error.stdout);
+			fs.writeFileSync(
+				path.join(config.rootDir, "build.log"),
+				error.stdout.replaceAll(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "")
+			);
+		}
+		throw error;
+	}
+};
diff --git a/scripts/build/plugin_prepare.js b/scripts/build/plugin_prepare.js
new file mode 100644
index 00000000..247ad22d
--- /dev/null
+++ b/scripts/build/plugin_prepare.js
@@ -0,0 +1,31 @@
+const { execSync } = require("child_process");
+const path = require("path");
+const fs = require("fs");
+const { argv, stdout, exit } = require("process");
+const { execIn, parts, getDirs, walk, sanitizeVarName } = require("../utils");
+
+module.exports = function (config) {
+	console.log(`==> Building plugin index...`);
+	let output = 'import { Plugin } from "util/plugin";\n';
+
+	const dirs = fs.readdirSync(config.pluginDir).filter((x) => {
+		try {
+			fs.readdirSync(path.join(config.pluginDir, x));
+			return true;
+		} catch (e) {
+			return false;
+		}
+	});
+	dirs.forEach((x) => {
+		let pluginManifest = require(path.join(config.pluginDir, x, "plugin.json"));
+		console.log(`  ==> Registering plugin: ${pluginManifest.name} (${pluginManifest.id}) by ${pluginManifest.authors}`);
+		output += `import * as ${sanitizeVarName(x)} from "./${x}/${pluginManifest.mainClass}";\n`;
+	});
+	output += `\nexport const PluginIndex: any = {\n`;
+	dirs.forEach((x) => {
+		output += `    "${x}": new ${sanitizeVarName(x)}.default(),\n`; //ctor test: '${path.resolve(path.join(pluginDir, x))}', require('./${x}/plugin.json')
+	});
+	output += `};`;
+
+	fs.writeFileSync(path.join(config.pluginDir, "PluginIndex.ts"), output);
+};
diff --git a/scripts/build/plugin_resources.js b/scripts/build/plugin_resources.js
new file mode 100644
index 00000000..5b4b97f2
--- /dev/null
+++ b/scripts/build/plugin_resources.js
@@ -0,0 +1,13 @@
+const { execSync } = require("child_process");
+const path = require("path");
+const fs = require("fs");
+const { argv, stdout, exit } = require("process");
+const { execIn, parts, getDirs, walk, sanitizeVarName } = require("../utils");
+
+module.exports = function (config) {
+	console.log(`==> Copying all plugin resources...`);
+	let pluginFiles = walk(config.pluginDir).filter((x) => !x.endsWith(".ts"));
+	pluginFiles.forEach((x) => {
+		fs.copyFileSync(x, x.replace("src", "dist"));
+	});
+};
diff --git a/scripts/build/remap_imports.js b/scripts/build/remap_imports.js
new file mode 100644
index 00000000..cdcd571a
--- /dev/null
+++ b/scripts/build/remap_imports.js
@@ -0,0 +1,15 @@
+const { execSync } = require("child_process");
+const path = require("path");
+const fs = require("fs");
+const { argv, stdout, exit } = require("process");
+const { execIn, parts, getDirs, walk, sanitizeVarName } = require("../utils");
+
+module.exports = function (config) {
+	console.log(`==> Remapping module imports...`);
+	let files = walk(config.distDir).filter((x) => x.endsWith(".js"));
+	files.forEach((x) => {
+		let fc = fs.readFileSync(x).toString();
+		fc = fc.replaceAll("@fosscord/", "#");
+		fs.writeFileSync(x, fc);
+	});
+};