summary refs log tree commit diff
path: root/scripts/license.js
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-01-18 13:05:49 +1100
committerGitHub <noreply@github.com>2023-01-18 13:05:49 +1100
commit0e7f127ece7c12d0266131605ebdfd6d0998cdb2 (patch)
tree9c7d64bca3e2ac76a6c43569c59fc1b452a8b8eb /scripts/license.js
parentsend GUILD_CREATE after READY event (#938) (diff)
downloadserver-0e7f127ece7c12d0266131605ebdfd6d0998cdb2.tar.xz
Script to add license preamble to each file. Also, does that. (#935)
* Add script to automatically add license to each file

* Add license preamble to each file.

* Fix existing preamble detection breaking due to line endings. Git :(

* Prevent license from being added to code with other licenses
Diffstat (limited to 'scripts/license.js')
-rw-r--r--scripts/license.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/scripts/license.js b/scripts/license.js
new file mode 100644
index 00000000..31321cd1
--- /dev/null
+++ b/scripts/license.js
@@ -0,0 +1,60 @@
+/*
+	Automatically appends our license preamble to each source file.
+	Does not prepend if preamble already exists.
+	Does not replace if change in util/licensePreamble.txt was made.
+	Does not prepend is file contains @fc-license-skip
+*/
+
+const Path = require("path");
+const fs = require("fs");
+const walk = require("./util/walk");
+
+const FOSSCORD_SOURCE_DIR = Path.join(__dirname, "..", "src");
+const FOSSCORD_SCRIPTS_DIR = Path.join(__dirname);
+const FOSSCORD_LICENSE_PREAMBLE = fs
+	.readFileSync(Path.join(__dirname, "util", "licensePreamble.txt"))
+	.toString()
+	.split("\r") // remove windows bs
+	.join("") // ^
+	.split("\n")
+	.map((x) => `\t${x}`)
+	.join("\n");
+
+const languageCommentStrings = {
+	js: ["/*", "*/"],
+	ts: ["/*", "*/"],
+};
+
+const addToDir = (dir) => {
+	const files = walk(dir, Object.keys(languageCommentStrings));
+
+	for (let path of files) {
+		const file = fs.readFileSync(path).toString().split("\r").join("");
+		const fileType = path.slice(path.lastIndexOf(".") + 1);
+		const commentStrings = languageCommentStrings[fileType];
+		if (!commentStrings) continue;
+
+		const preamble =
+			commentStrings[0] +
+			"\n" +
+			FOSSCORD_LICENSE_PREAMBLE +
+			"\n" +
+			commentStrings[1];
+
+		if (file.startsWith(preamble)) {
+			continue;
+		}
+
+		// This is kind of lame.
+		if (file.includes("@fc-license-skip") && path != __filename) {
+			console.log(`skipping ${path} as it has a different license.`);
+			continue;
+		}
+
+		console.log(`writing to ${path}`);
+		fs.writeFileSync(path, preamble + "\n\n" + file);
+	}
+};
+
+addToDir(FOSSCORD_SOURCE_DIR);
+addToDir(FOSSCORD_SCRIPTS_DIR);