Restructure BD into a small mono-repo
This is a repo-wide refactor that introduces the injector to the main branch. The new architecture is as such: common/ injector/ renderer/
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rules": {
|
||||
"no-console": "off"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
const args = process.argv;
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const useBdRelease = args[2] && args[2].toLowerCase() === "release";
|
||||
const releaseInput = useBdRelease ? args[3] && args[3].toLowerCase() : args[2] && args[2].toLowerCase();
|
||||
const release = releaseInput === "canary" ? "Discord Canary" : releaseInput === "ptb" ? "Discord PTB" : "Discord";
|
||||
console.log(`Injecting into version ${release}`);
|
||||
|
||||
const discordPath = (function() {
|
||||
if (process.platform === "win32") {
|
||||
const basedir = path.join(process.env.LOCALAPPDATA, release.replace(/ /g, ""));
|
||||
if (!fs.existsSync(basedir)) throw new Error(`Cannot find directory for ${release}`);
|
||||
const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0];
|
||||
return path.join(basedir, version, "resources");
|
||||
}
|
||||
else if (process.platform === "darwin") {
|
||||
const appPath = releaseInput === "canary" ? path.join("/Applications", "Discord Canary.app")
|
||||
: releaseInput === "ptb" ? path.join("/Applications", "Discord PTB.app")
|
||||
: useBdRelease && args[3] ? args[3] ? args[2] : args[2]
|
||||
: path.join("/Applications", "Discord.app");
|
||||
|
||||
return path.join(appPath, "Contents", "Resources");
|
||||
}
|
||||
else if (process.platform === "linux") {
|
||||
return path.join("/usr", "share", release.toLowerCase().replace(/ /g, "-"), "resources");
|
||||
}
|
||||
})();
|
||||
|
||||
if (!fs.existsSync(discordPath)) throw new Error(`Cannot find directory for ${release}`);
|
||||
console.log(`Found ${release} in ${discordPath}`);
|
||||
|
||||
const appPath = path.join(discordPath, "app");
|
||||
const packageJson = path.join(appPath, "package.json");
|
||||
const indexJs = path.join(appPath, "index.js");
|
||||
|
||||
if (!fs.existsSync(appPath)) fs.mkdirSync(appPath);
|
||||
if (fs.existsSync(packageJson)) fs.unlinkSync(packageJson);
|
||||
if (fs.existsSync(indexJs)) fs.unlinkSync(indexJs);
|
||||
|
||||
const bdPath = useBdRelease ? path.resolve(__dirname, "..", "dist", "betterdiscord.asar") : path.resolve(__dirname, "..", "dist");
|
||||
|
||||
console.log(`Writing package.json`);
|
||||
fs.writeFileSync(packageJson, JSON.stringify({
|
||||
name: "betterdiscord",
|
||||
main: "index.js",
|
||||
}, null, 4));
|
||||
|
||||
console.log(`Writing index.js`);
|
||||
fs.writeFileSync(indexJs, `require("${bdPath.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}");`);
|
||||
|
||||
console.log(`Injection successful, please restart ${release}.`);
|
||||
@@ -0,0 +1,62 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const asar = require("asar");
|
||||
|
||||
const dist = path.join(__dirname, "..", "dist");
|
||||
const bundleFile = path.join(dist, "betterdiscord.asar");
|
||||
|
||||
const doSanityChecks = function() {
|
||||
console.log("Ensuring build validity");
|
||||
const files = [
|
||||
path.join(dist, "injector.js"),
|
||||
path.join(dist, "preload.js"),
|
||||
path.join(dist, "renderer.js")
|
||||
];
|
||||
|
||||
for (const file of files) {
|
||||
const exists = fs.existsSync(file);
|
||||
if (!exists) throw new Error(` ❌ File missing: ${file}`);
|
||||
console.log(` ✅ Found ${file}`);
|
||||
}
|
||||
};
|
||||
|
||||
const cleanOldAsar = function() {
|
||||
console.log("Ensuring clean build");
|
||||
if (!fs.existsSync(bundleFile)) return console.log(" ✅ Nothing to clean up");
|
||||
fs.unlinkSync(bundleFile);
|
||||
console.log(` ✅ Removed old bundle ${bundleFile}`);
|
||||
};
|
||||
|
||||
const buildPackage = function() {
|
||||
console.log("Ensuring valid package.json");
|
||||
|
||||
const pkgFile = path.join(dist, "package.json");
|
||||
if (fs.existsSync(pkgFile)) {
|
||||
const currentPkg = require(pkgFile);
|
||||
if (currentPkg.name && currentPkg.main && currentPkg.main === "injector.js") return console.log(" ✅ Existing package.json is valid");
|
||||
console.log(" ⚠️ Existing package.json is invalid");
|
||||
}
|
||||
fs.writeFileSync(pkgFile, JSON.stringify({name: "betterdiscord", main: "injector.js"}));
|
||||
console.log(" ✅ Created new package.json");
|
||||
};
|
||||
|
||||
const makeBundle = function() {
|
||||
console.log("Generating bundle");
|
||||
asar.createPackage(dist, bundleFile).then(() => {
|
||||
console.log(` ✅ Successfully created bundle ${bundleFile}`);
|
||||
}).catch(err => {
|
||||
console.log(` ❌ Could not build bundle: ${err.message}`);
|
||||
});
|
||||
};
|
||||
|
||||
console.log("");
|
||||
doSanityChecks();
|
||||
|
||||
console.log("");
|
||||
cleanOldAsar();
|
||||
|
||||
console.log("");
|
||||
buildPackage();
|
||||
|
||||
console.log("");
|
||||
makeBundle();
|
||||
Reference in New Issue
Block a user