mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-08 11:52:55 +01:00
Better patch system
This commit is contained in:
parent
88b04adab2
commit
b3a3ea2ff1
2
.gitignore
vendored
2
.gitignore
vendored
@ -34,3 +34,5 @@ dbconf.json
|
||||
migrations.db
|
||||
|
||||
assets/cache_src/
|
||||
|
||||
package-lock.json
|
||||
|
16
build.tab-linux-desktop.json
Normal file
16
build.tab-linux-desktop.json
Normal 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"]
|
||||
}
|
||||
}
|
14
scripts/build/clean_cache.js
Normal file
14
scripts/build/clean_cache.js
Normal 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}!`);
|
||||
}
|
||||
};
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
80
src/util/util/formatters/CodeFormatter.ts
Normal file
80
src/util/util/formatters/CodeFormatter.ts
Normal 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;
|
||||
}
|
@ -23,3 +23,4 @@ export * from "./Snowflake";
|
||||
export * from "./String";
|
||||
export * from "./Token";
|
||||
export * from "./TraverseDirectory";
|
||||
export * from "./formatters/CodeFormatter";
|
Loading…
Reference in New Issue
Block a user