Files
totallynottokenstealer/builder.js
katamaz 7b87cc9161
Some checks failed
Build and Release / build-and-release (push) Failing after 30s
normalize path on builder
2026-05-28 15:18:17 +03:00

93 lines
2.3 KiB
JavaScript

// ai slop
import fs from "node:fs";
import path from "node:path";
const DEV = process.argv.includes("--dev");
const escape = (str) =>
str.replace(/\s+/g, " ").trim().replace(/"/g, '\\"').replace(/\$/g, "\\$");
function build() {
const data = fs.readFileSync("./script.php", "utf8");
const mountRegex = /"<<BUILDER_MOUNT_FILE_\((?<file>.*?)\)>>"/gm;
const mountedFiles = new Set();
const result = data.replace(mountRegex, (_match, file) => {
let fileContent = fs.readFileSync(path.normalize(file), "utf8");
mountedFiles.add(path.resolve(file));
const inlinePhpVarRegex = /"<<BUILDER_PHP_VAR\((?<varName>.*?)\)>>"/g;
const vars = [];
let varMatch;
while ((varMatch = inlinePhpVarRegex.exec(fileContent)) !== null) {
vars.push({
escapedPlaceholder: escape(varMatch[0]),
varName: varMatch.groups.varName,
});
}
fileContent = escape(fileContent);
for (const { escapedPlaceholder, varName } of vars) {
fileContent = fileContent.replace(
escapedPlaceholder,
`" . ${varName} . "`,
);
}
return `"${fileContent}"`;
});
fs.writeFileSync("./output.php", result, "utf8");
console.log(`[${new Date().toLocaleTimeString()}] Built output.php`);
return mountedFiles;
}
// Run once immediately
let watchedFiles = build();
if (DEV) {
const watchers = new Map(); // path → FSWatcher
function watchFile(file) {
if (watchers.has(file)) return;
const watcher = fs.watch(file, () => {
console.log(
`[${new Date().toLocaleTimeString()}] Changed: ${path.relative(".", file)}`,
);
rebuild();
});
watchers.set(file, watcher);
}
function syncWatchers(current) {
// Watch any newly mounted files
for (const file of current) watchFile(file);
// Stop watching files that are no longer mounted
for (const [file, watcher] of watchers) {
if (!current.has(file) && file !== path.resolve("./script.php")) {
watcher.close();
watchers.delete(file);
}
}
}
function rebuild() {
try {
watchedFiles = build();
syncWatchers(watchedFiles);
} catch (err) {
console.error("Build error:", err.message);
}
}
// Always watch the entry file
watchFile(path.resolve("./script.php"));
syncWatchers(watchedFiles);
console.log("Watching for changes… (Ctrl+C to stop)");
}