/* 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} */ 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} */ await runBuild("> Building shared backend in public/", { VITE_BUILD_TARGET: "bo", }); /** * Build each theme in public/theme/ * * @return {Promise} */ 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)).`);