ldc-gen-game-web/build.mjs
2026-04-15 11:21:52 +02:00

83 lines
2.2 KiB
JavaScript

/* eslint-disable no-undef */
import { build } from "vite";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
/** @var {string} dirname Project source directory. */
const dirname = path.dirname(fileURLToPath(import.meta.url));
/** @var {string} themesDir Theme source directory. */
const themesDir = path.resolve(dirname, "resources/theme");
/** @var {string|null} themName Target theme to build. */
const themeName = process.argv[2];
/** @var {string[]} themesToBuild List of themes to build. */
let themesToBuild = [];
if (themeName) {
const themePath = path.join(themesDir, themeName);
if (!fs.existsSync(themePath)) {
console.error(`❌ Theme folder "${themePath}" does not exist.`);
process.exit(1);
}
themesToBuild = [themeName];
} else {
themesToBuild = fs
.readdirSync(themesDir)
.filter((f) => fs.statSync(path.join(themesDir, f)).isDirectory());
}
if (themesToBuild.length === 0) {
console.error("❌ No themes found in resources/theme/.");
process.exit(1);
}
/**
* Run build for a target resource with env variables.
*
* @param {string} label
* @param {object} env
* @return {Promise<void>}
*/
async function runBuild(label, env = {}) {
console.log(`${label}`);
Object.assign(process.env, env);
try {
await build({
configFile: path.resolve(dirname, "vite.config.ts"),
mode: process.env.NODE_ENV ?? "production",
});
} catch (err) {
console.error(`\n❌ Build failed for "${label}":`, err);
process.exit(1);
} finally {
// Remove env variables to don't affect next build.
Object.keys(env).forEach((k) => delete process.env[k]);
}
}
/**
* Build the shared backend in public/
*
* @return {Promise<void>}
*/
await runBuild("> Building shared backend in public/", {
VITE_BUILD_TARGET: "bo",
});
/**
* Build each theme in public/theme/
*
* @return {Promise<void>}
*/
for (const theme of themesToBuild) {
await runBuild(`\n> Building front theme ${theme} in public/${theme}/`, {
VITE_BUILD_TARGET: "fo",
VITE_CURRENT_THEME: theme,
});
}
console.log(`\n> All builds completed (shared backend + ${themesToBuild.length} theme(s)).`);