1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-22 02:12:40 +01:00

Better patch system

This commit is contained in:
TheArcaneBrony 2022-09-17 13:17:12 +02:00
parent 88b04adab2
commit b3a3ea2ff1
No known key found for this signature in database
GPG Key ID: 32FC5AAADAD75A22
7 changed files with 133 additions and 12 deletions

2
.gitignore vendored
View File

@ -34,3 +34,5 @@ dbconf.json
migrations.db
assets/cache_src/
package-lock.json

View File

@ -0,0 +1,16 @@
{
"compiler": "tsc",
"verbose": true,
"writeBuildLog": true,
"writeAnsiBuildLog": true,
"logErrors": true,
"tsc": {
"prettyErrors": true
},
"clean": true,
"quiet": false,
"steps": {
"pre": ["clean", "clean_cache"],
"post": ["remap_imports"]
}
}

View File

@ -0,0 +1,14 @@
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(`==> Cleaning asset cache...`);
const assetDir = path.resolve(path.join(config.rootDir, "assets", "cache"));
if (fs.existsSync(assetDir)) {
fs.rmSync(assetDir, { recursive: true });
if (config.verbose) console.log(`Deleted ${assetDir}!`);
}
};

View File

@ -3,6 +3,7 @@ const path = require("path");
const fs = require("fs");
const { argv, stdout, exit } = require("process");
const { execIn, parts, getDirs, walk, sanitizeVarName } = require("./utils");
const os = require('os');
//file paths
const rootDir = path.join(__dirname, "..");
@ -17,14 +18,25 @@ const pluginDir = path.join(srcDir, "plugins");
//more, dont export
const buildStepDir = path.join(scriptsDir, "build");
if (!fs.existsSync(configPath)) {
let config;
if (fs.existsSync(path.join(rootDir, `build.${os.hostname()}.json`))) {
console.log(`Using build.${os.hostname()}.json`);
config = { rootDir, srcDir, distDir, configPath, buildLog, buildLogAnsi, pluginDir, ...require(path.join(rootDir, `build.${os.hostname()}.json`)) };
}
else if (fs.existsSync(configPath)) {
console.log(`Using build.json`);
config = { rootDir, srcDir, distDir, configPath, buildLog, buildLogAnsi, pluginDir, ...require(configPath) };
}
else if (!fs.existsSync(configPath)) {
console.log(`Using default config`);
if (!fs.existsSync(path.join(configPath + ".default"))) {
console.log("build.json.default not found! Exiting!");
exit(1);
}
fs.copyFileSync(configPath + ".default", configPath);
config = { rootDir, srcDir, distDir, configPath, buildLog, buildLogAnsi, pluginDir, ...require(configPath) };
}
let config = { rootDir, srcDir, distDir, configPath, buildLog, buildLogAnsi, pluginDir, ...require(configPath) };
config.steps.pre.forEach((step) => require(path.join(buildStepDir, step))(config));
require(path.join(buildStepDir, "compile_" + config.compiler))(config);

View File

@ -1,5 +1,6 @@
import path from "path";
import fs from "fs";
import { formatFile } from "@fosscord/util";
console.log('[TestClient] Loading private assets...');
@ -27,10 +28,11 @@ fs.readdirSync(path.join(iconsRoot, "custom")).forEach(file => {
console.log('[TestClient] Patcher ready!');
export function patchFile(filePath: string, content: string): string {
console.log(`[TestClient] Patching ${filePath}`);
//console.log(`[TestClient] Patching ${filePath}`);
let startTime = Date.now();
content = prettier(filePath, content);
content = formatFile(filePath, content);
//content = prettier(filePath, content);
content = autoPatch(filePath, content);
console.log(`[TestClient] Patched ${filePath} in ${Date.now() - startTime}ms`);
@ -104,18 +106,12 @@ function autoPatch(filePath: string, content: string): string {
content = content.replace(/d: "M23\.0212.*/, `d: "${icons.get("homeIcon.path")!.toString()}"`);
content = content.replace('width: n, height: o, viewBox: "0 0 28 20"', 'width: 48, height: 48, viewBox: "0 0 48 48"');
//undo webpacking
// - booleans
content = content.replace(/!0/g, "true");
content = content.replace(/!1/g, "false");
// - real esmodule defs
content = content.replace(/Object.defineProperty\((.), "__esModule", { value: (.*) }\);/g, '\$1.__esModule = \$2;');
//save some time on load resolving asset urls...
content = content.replaceAll('e.exports = n.p + "', 'e.exports = "/assets/');
content = content.replaceAll('e.exports = r.p + "', 'e.exports = "/assets/');
console.log(`[TestClient] Autopatched ${path.basename(filePath)}!`);
//console.log(`[TestClient] Autopatched ${path.basename(filePath)}!`);
return content;
}

View File

@ -0,0 +1,80 @@
export function formatFile(filePath: string, content: string): string {
let startTime = Date.now();
if(filePath.endsWith(".css")) {
content = formatCss(content);
} else if(filePath.endsWith(".js")) {
content = formatJs(content);
}
console.log(`[CodeFormatter] Formatted ${filePath} in ${Date.now() - startTime}ms`);
return content;
}
function unfoldBlocks(content: string): string{
content = content.replaceAll("{", "{\n");
content = content.replaceAll("}", "\n}\n");
content = content.replaceAll(";", ";\n");
return content;
}
function formatCss(content: string): string {
content = unfoldBlocks(content);
return content;
}
//simple js formatter... this was absolute pain...
function formatJs(content: string): string {
content = content.replaceAll("{", "{\n");
content = content.replaceAll("}", "\n}\n");
content = content.replaceAll(";", ";\n");
content = content.replaceAll(":\"", ": \"");
content = content.replaceAll(/(\w)=\"/g, "\$1 = \"");
content = content.replaceAll("function(){", "function() {");
//undo webpacking, improves performance and debugging
// - booleans
content = content.replaceAll("return!", "return !");
content = content.replace(/!0/g, "true");
content = content.replace(/!1/g, "false");
// - real esmodule defs, slightly faster
content = content.replace(/Object.defineProperty\((.), "__esModule", { value: (.*) }\);/g, '\$1.__esModule = \$2;');
//fix accidentals
content = content.replaceAll(";\n\"", ";\"");
content = content.replaceAll("{\n\n}", "{}");
content = content.replaceAll("\\{\n", "\\{");
content = content.replaceAll("\\\n}", "\\}");
content = content.replaceAll("\\}\n", "\\}");
content = content.replaceAll("}\n\"", "}\"");
content = content.replaceAll("{\n [polyfill code] \n}\"", "{ [polyfill code] }\"");
content = content.replaceAll(";\n ", "; ");
// --regex--
content = content.replaceAll(/\{\n(\w{1,4})\n\}/g, '{\$1}')
content = content.replaceAll(/\{\n(\w{1,4},\w{0,4})\n\}/g, '{\$1}')
content = content.replaceAll("}\n)", "})")
content = content.replaceAll("}\n|", "}|")
content = content.replaceAll("}\n\\s", "}\\s")
content = content.replaceAll("}\n$", "}$")
content = content.replaceAll("}\n\\", "}\\")
// --!regex--
content = content.replaceAll("\n\\n", "\\n")
content = content.replaceAll(" {\n", " {");
content = content.replaceAll("\n}\\n", "}\\n");
content = content.replaceAll(";\nbase64,", ";base64,");
content = content.replaceAll(/!!{\n(\w*)\n}\n!!/g, "!!{\$1}!!");
content = content.replaceAll("}\n!!", "}!!");
content = content.replaceAll("}\n**", "}**");
content = content.replaceAll("}\nx", "}x");
content = content.replaceAll(/{\n(\w*)\n}/g, "{\$1}");
content = content.replaceAll("\",\n\"", "\",\"");
while(/(\w) {\n([^\}\.]*)\n?}/g.test(content))
content = content.replaceAll(/(\w) {\n?([^\}\.]*)\n?}/g, "\$1 {\$2}");
//no double newlines
while(content.includes("\n\n"))
content = content.replaceAll("\n\n", "\n");
return content;
}

View File

@ -23,3 +23,4 @@ export * from "./Snowflake";
export * from "./String";
export * from "./Token";
export * from "./TraverseDirectory";
export * from "./formatters/CodeFormatter";