make build script more better-er

This commit is contained in:
MathMan05 2025-05-01 19:51:39 -05:00
parent 854da8ecec
commit e036d76cb0

View file

@ -6,19 +6,19 @@ import child_process from "child_process";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function moveFiles(path: string, newPath: string) {
async function moveFiles(curPath: string, newPath: string) {
async function processFile(file: string) {
const Prom: Promise<unknown>[] = [];
if ((await fs.stat(path + "/" + file)).isDirectory()) {
await fs.mkdir(newPath + "/" + file);
Prom.push(moveFiles(path + "/" + file, newPath + "/" + file));
if ((await fs.stat(path.join(curPath, file))).isDirectory()) {
await fs.mkdir(path.join(newPath, file));
Prom.push(moveFiles(path.join(curPath, file), path.join(newPath, file)));
} else {
if (!file.endsWith(".ts")) {
await fs.copyFile(path + "/" + file, newPath + "/" + file);
await fs.copyFile(path.join(curPath, file), path.join(newPath, file));
} else {
const plainname = file.split(".ts")[0];
const newfileDir = newPath + "/" + plainname;
const mod = await swc.transformFile(path + "/" + file, {
const newfileDir = path.join(newPath, plainname);
const mod = await swc.transformFile(path.join(curPath, file), {
minify: true,
sourceMaps: true,
isModule: true,
@ -34,41 +34,41 @@ async function moveFiles(path: string, newPath: string) {
}
await Promise.all(Prom);
}
await Promise.all((await fs.readdir(path)).map(processFile));
await Promise.all((await fs.readdir(curPath)).map(processFile));
}
async function build() {
console.time("build");
console.time("Cleaning dir");
try {
await fs.rm(__dirname + "/dist", {recursive: true});
await fs.rm(path.join(__dirname, "dist"), {recursive: true});
} catch {}
await fs.mkdir(__dirname + "/dist");
await fs.mkdir(path.join(__dirname, "dist"));
console.timeEnd("Cleaning dir");
console.time("Moving and compiling files");
await moveFiles(__dirname + "/src", __dirname + "/dist");
await moveFiles(path.join(__dirname, "src"), path.join(__dirname, "dist"));
console.timeEnd("Moving and compiling files");
console.time("Moving translations");
let langs = await fs.readdir(__dirname + "/translations");
let langs = await fs.readdir(path.join(__dirname, "translations"));
langs = langs.filter((e) => e !== "qqq.json");
const langobj = {};
for (const lang of langs) {
const str = await fs.readFile("translations/" + lang);
const str = await fs.readFile(path.join(__dirname, "translations", lang));
const json = JSON.parse(str.toString());
langobj[lang] = json.readableName;
fs.writeFile(__dirname + "/dist/webpage/translations/" + lang, str);
fs.writeFile(path.join(__dirname, "dist", "webpage", "translations") + lang, str);
}
await fs.writeFile(
"dist/webpage/translations/langs.js",
path.join(__dirname, "dist", "webpage", "translations", "langs.js"),
`const langs=${JSON.stringify(langobj)};export{langs}`,
);
console.timeEnd("Moving translations");
console.time("Adding git commit hash");
const revision = child_process.execSync("git rev-parse HEAD").toString().trim();
await fs.writeFile("dist/webpage/getupdates", revision);
await fs.writeFile(path.join(__dirname, "dist", "webpage", "getupdates"), revision);
console.timeEnd("Adding git commit hash");
console.timeEnd("build");
@ -79,7 +79,7 @@ await build();
if (process.argv.includes("watch")) {
let last = Date.now();
(async () => {
for await (const thing of fs.watch(__dirname + "/src")) {
for await (const thing of fs.watch(path.join(__dirname, "src"))) {
if (Date.now() - last < 100) {
continue;
}
@ -88,7 +88,7 @@ if (process.argv.includes("watch")) {
}
})();
(async () => {
for await (const thing of fs.watch(__dirname + "/translations")) {
for await (const thing of fs.watch(path.join(__dirname, "translations"))) {
if (Date.now() - last < 100) {
continue;
}