summary refs log tree commit diff
path: root/bundle/scripts/build.js
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-06 22:28:15 +0200
committerGitHub <noreply@github.com>2022-08-06 22:28:15 +0200
commit8fbcd35a87ac572c8e14b24b33fcb3d2e9223703 (patch)
treec9afc6ff4f708fb4c3ebbfb6a4a754375cc50d4e /bundle/scripts/build.js
parentMerge pull request #815 from fosscord/translation (diff)
parentMake build script use parts, defined in utils.js (diff)
downloadserver-8fbcd35a87ac572c8e14b24b33fcb3d2e9223703.tar.xz
Merge pull request #817 from fosscord/dev/new-scripts
Add utility scripts
Diffstat (limited to 'bundle/scripts/build.js')
-rw-r--r--bundle/scripts/build.js80
1 files changed, 50 insertions, 30 deletions
diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js
index f73fb11a..f53a2223 100644
--- a/bundle/scripts/build.js
+++ b/bundle/scripts/build.js
@@ -2,39 +2,36 @@ const { execSync } = require("child_process");
 const path = require("path");
 const fs = require("fs");
 const { getSystemErrorMap } = require("util");
-const { argv } = require("process");
+const { argv, stdout, exit } = require("process");
+const { copyRecursiveSync, execIn, parts } = require('./utils');
 
-var steps = 2, i = 0;
+if(argv.includes("help")) {
+	console.log(`Fosscord build script help:
+Arguments:
+  clean			Cleans up previous builds
+  copyonly		Only copy source files, don't build (useful for updating assets)
+  verbose		Enable verbose logging
+  logerrors		Log build errors to console
+  pretty-errors		Pretty-print build errors
+  silent		No output to console or files.`);
+	exit(0);
+}
+
+let steps = 3, i = 0;
 if (argv.includes("clean")) steps++;
 if (argv.includes("copyonly")) steps--;
-const dirs = ["api", "util", "cdn", "gateway", "bundle"];
 
 const verbose = argv.includes("verbose") || argv.includes("v");
+const logerr = argv.includes("logerrors");
+const pretty = argv.includes("pretty-errors");
+const silent = argv.includes("silent");
 
-var copyRecursiveSync = function(src, dest) {
-	if(verbose) console.log(`cpsync: ${src} -> ${dest}`);
-	var exists = fs.existsSync(src);
-	if(!exists){
-		console.log(src + " doesn't exist, not copying!");
-		return;
-	}
-	var stats = exists && fs.statSync(src);
-	var isDirectory = exists && stats.isDirectory();
-	if (isDirectory) {
-	  fs.mkdirSync(dest, {recursive: true});
-	  fs.readdirSync(src).forEach(function(childItemName) {
-		copyRecursiveSync(path.join(src, childItemName),
-						  path.join(dest, childItemName));
-	  });
-	} else {
-	  fs.copyFileSync(src, dest);
-	}
-  };
+if(silent) console.error = console.log = function(){}
 
 if (argv.includes("clean")) {
 	console.log(`[${++i}/${steps}] Cleaning...`);
-	dirs.forEach((a) => {
-		var d = "../" + a + "/dist";
+	parts.forEach((a) => {
+		let d = "../" + a + "/dist";
 		if (fs.existsSync(d)) {
 			fs.rmSync(d, { recursive: true });
 			if (verbose) console.log(`Deleted ${d}!`);
@@ -42,11 +39,16 @@ if (argv.includes("clean")) {
 	});
 }
 
+console.log(`[${++i}/${steps}] Checking if dependencies were installed correctly...`);
+//exif-be-gone v1.3.0 doesnt build js, known bug
+if(!fs.existsSync(path.join(__dirname, "..", "node_modules", "exif-be-gone", "index.js")))
+	execIn("npm run build", path.join(__dirname, "..", "node_modules", "exif-be-gone"));
+
 console.log(`[${++i}/${steps}] Copying src files...`);
 copyRecursiveSync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets"));
 copyRecursiveSync(path.join(__dirname, "..", "..", "api", "client_test"), path.join(__dirname, "..", "dist", "api", "client_test"));
 copyRecursiveSync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales"));
-dirs.forEach((a) => {
+parts.forEach((a) => {
 	copyRecursiveSync("../" + a + "/src", "dist/" + a + "/src");
 	if (verbose) console.log(`Copied ${"../" + a + "/dist"} -> ${"dist/" + a + "/src"}!`);
 });
@@ -54,13 +56,16 @@ dirs.forEach((a) => {
 if (!argv.includes("copyonly")) {
 	console.log(`[${++i}/${steps}] Compiling src files ...`);
 
-	console.log(
+	let buildFlags = ''
+	if(pretty) buildFlags += '--pretty '
+
+	try {
 		execSync(
 			'node "' +
 				path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
 				'" -p "' +
 				path.join(__dirname, "..") +
-				'"',
+				'" ' + buildFlags,
 			{
 				cwd: path.join(__dirname, ".."),
 				shell: true,
@@ -68,6 +73,21 @@ if (!argv.includes("copyonly")) {
 				encoding: "utf8"
 			}
 		)
-	);
-}
-
+	} catch (error) {
+		if(verbose || logerr) {
+			error.stdout.split(/\r?\n/).forEach((line) => {
+				let _line = line.replace('dist/','',1);
+				if(!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(!silent){
+			if(pretty) fs.writeFileSync("build.log.ansi",  error.stdout);
+			fs.writeFileSync("build.log",  error.stdout.replaceAll(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''));
+		}
+	}
+}
\ No newline at end of file