summary refs log tree commit diff
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-06 15:08:19 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-06 15:08:19 +0200
commitd8215365720fa77c5e82f361f082e942f7223f10 (patch)
treec618b7f00e711a4d0c47cf363a3c9511a67fddc3
parentMerge pull request #815 from fosscord/translation (diff)
downloadserver-d8215365720fa77c5e82f361f082e942f7223f10.tar.xz
Add utility scripts
-rw-r--r--bundle/package.json4
-rw-r--r--bundle/scripts/build.js44
-rw-r--r--bundle/scripts/depcheck.js56
-rw-r--r--bundle/scripts/depclean.js63
-rw-r--r--bundle/scripts/utils.js53
5 files changed, 196 insertions, 24 deletions
diff --git a/bundle/package.json b/bundle/package.json
index 311fab18..49aadeb8 100644
--- a/bundle/package.json
+++ b/bundle/package.json
@@ -4,7 +4,9 @@
 	"description": "",
 	"main": "src/start.js",
 	"scripts": {
-		"setup": "node scripts/install.js && npm install --no-optional && npx ts-patch install -s && npx patch-package --patch-dir ../api/patches/ && npm run build",
+		"setup": "node scripts/install.js && npm install --omit optional && ts-patch install -s && patch-package --patch-dir ../api/patches/ && npm run build",
+		"depclean": "node scripts/depclean.js",
+		"depcheck": "node scripts/depcheck.js",
 		"build": "node scripts/build.js",
 		"start": "node scripts/build.js && node dist/bundle/src/start.js",
 		"start:bundle": "node dist/bundle/src/start.js",
diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js
index f73fb11a..8a1b3c83 100644
--- a/bundle/scripts/build.js
+++ b/bundle/scripts/build.js
@@ -2,39 +2,32 @@ 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} = 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`);
+	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");
 
-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 (argv.includes("clean")) {
 	console.log(`[${++i}/${steps}] Cleaning...`);
 	dirs.forEach((a) => {
-		var d = "../" + a + "/dist";
+		let d = "../" + a + "/dist";
 		if (fs.existsSync(d)) {
 			fs.rmSync(d, { recursive: true });
 			if (verbose) console.log(`Deleted ${d}!`);
@@ -42,6 +35,11 @@ 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"));
diff --git a/bundle/scripts/depcheck.js b/bundle/scripts/depcheck.js
new file mode 100644
index 00000000..1957f794
--- /dev/null
+++ b/bundle/scripts/depcheck.js
@@ -0,0 +1,56 @@
+const path = require("path");
+const fs = require("fs");
+const { env } = require("process");
+const { execSync } = require("child_process");
+const { argv, stdout, exit } = require("process");
+
+const { execIn, getLines, parts } = require("./utils");
+
+let npmi_extra_flags = "";
+
+const resolveminor = argv.includes("resolveminor");
+if(argv.includes("nobuild")) npmi_extra_flags += "--ignore-scripts ";
+
+parts.forEach((part) => {
+	let partDir = path.join(__dirname, "..", "..", part);
+	let distDir = path.join(partDir, "dist");
+	console.log(`Checking updates for ${part} (${partDir})`);
+	if(part == "bundle") {
+		execIn(`npm run syncdeps`, partDir)
+	}
+    if(resolveminor) {
+        fs.rmSync(path.join(partDir, "node_modules"), {
+			recursive: true,
+			force: true,
+		});
+        execIn(`npm i --save --no-fund --no-audit --no-package-lock ${npmi_extra_flags}`, partDir)
+    }
+	let x = [
+		[
+			"pkg",
+			{
+				current: "1.0",
+				wanted: "2.0",
+				latest: "2.0",
+				dependent: "cdn",
+				location: "/usr/src/fosscord/bundle/node_packages/pkg",
+			},
+		],
+	];
+	x = Object.entries(
+		JSON.parse(execIn("npm outdated --json", partDir))
+	);
+	x.forEach((a) => {
+        let pkgname = a[0];
+        let pkginfo = a[1];
+        if(!pkginfo.current)
+            console.log(`MISSING ${pkgname}: ${pkginfo.current} -> ${pkginfo.wanted} (latest: ${pkginfo.latest})`);
+        else if(pkginfo.latest != pkginfo.wanted){
+            if(pkginfo.current != pkginfo.wanted) 
+                console.log(`MINOR   ${pkgname}: ${pkginfo.current} -> ${pkginfo.wanted}`);
+		    console.log(`MAJOR   ${pkgname}: ${pkginfo.current} -> ${pkginfo.latest}`);
+        }
+        else
+            console.log(`MINOR   ${pkgname}: ${pkginfo.current} -> ${pkginfo.wanted}`);
+	});
+});
diff --git a/bundle/scripts/depclean.js b/bundle/scripts/depclean.js
new file mode 100644
index 00000000..6d9a2260
--- /dev/null
+++ b/bundle/scripts/depclean.js
@@ -0,0 +1,63 @@
+const path = require("path");
+const fs = require("fs");
+const { env } = require("process");
+const { execSync } = require("child_process");
+const { argv, stdout, exit } = require("process");
+
+const { execIn, getLines, parts } = require('./utils');
+
+const bundleRequired = ["@ovos-media/ts-transform-paths"];
+const removeModules = argv.includes("cleanup");
+
+parts.forEach((part) => {
+	console.log(`Installing all packages for ${part}...`);
+	execIn("npm i", path.join(__dirname, "..", "..", part));
+});
+
+parts.forEach((part) => {
+	let partDir = path.join(__dirname, "..", "..", part);
+	let distDir = path.join(partDir, "dist");
+	let start = 0;
+	start = getLines(
+		execIn("npm ls --parseable --package-lock-only -a", partDir)
+	);
+	if (fs.existsSync(distDir))
+		fs.rmSync(distDir, {
+			recursive: true,
+			force: true,
+		});
+	let x = {
+		dependencies: [],
+		devDependencies: [],
+		invalidDirs: [],
+		invalidFiles: [],
+		missing: [],
+		using: [],
+	};
+	x = JSON.parse(execIn("npx depcheck --json", partDir).stdout);
+
+	fs.writeFileSync(
+		path.join(__dirname, "..", `depclean.out.${part}.json`),
+		JSON.stringify(x, null, "\t"),
+		{ encoding: "utf8" }
+	);
+
+	let depsToRemove = x.dependencies.join(" ");
+	if (depsToRemove) execIn(`npm r --save ${depsToRemove}`, partDir);
+
+	depsToRemove = x.devDependencies.join(" ");
+	if (depsToRemove) execIn(`npm r --save --dev ${depsToRemove}`, partDir);
+
+	if (removeModules && fs.existsSync(path.join(partDir, "node_modules")))
+		fs.rmSync(path.join(partDir, "node_modules"), {
+			recursive: true,
+			force: true,
+		});
+	let end = getLines(
+		execIn("npm ls --parseable --package-lock-only -a", partDir)
+	);
+	console.log(`${part}: ${start} -> ${end} (diff: ${start - end})`);
+});
+console.log("Installing required packages for bundle...");
+
+execIn(`npm i --save ${bundleRequired.join(" ")}`, path.join(__dirname, ".."));
diff --git a/bundle/scripts/utils.js b/bundle/scripts/utils.js
new file mode 100644
index 00000000..bf960532
--- /dev/null
+++ b/bundle/scripts/utils.js
@@ -0,0 +1,53 @@
+const path = require("path");
+const fs = require("fs");
+const { env } = require("process");
+const { execSync } = require("child_process");
+const { argv, stdout, exit } = require("process");
+
+const parts = ["api", "util", "cdn", "gateway", "bundle"];
+
+function copyRecursiveSync(src, dest) {
+	//if (verbose) console.log(`cpsync: ${src} -> ${dest}`);
+	let exists = fs.existsSync(src);
+	if (!exists) {
+		console.log(src + " doesn't exist, not copying!");
+		return;
+	}
+	let stats = exists && fs.statSync(src);
+	let 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);
+	}
+}
+
+function execIn(cmd, workdir) {
+	try {
+		return execSync(cmd, {
+			cwd: workdir,
+			shell: true,
+			env: process.env,
+			encoding: "utf-8",
+		});
+	} catch (error) {
+		return error.stdout;
+	}
+}
+
+function getLines(output) {
+	return output.split("\n").length;
+}
+
+module.exports = { 
+	//consts
+	parts,
+	//functions
+	copyRecursiveSync, execIn, getLines
+};