2026-02-17 23:03:48 -05:00

72 lines
2.4 KiB
JavaScript

const ejs = require('ejs');
const fs = require('fs');
const path = require('path');
const TEMPLATES_DIR = path.join(__dirname, '..', 'templates', 'site');
async function buildSite(config, clientDir) {
const siteDir = path.join(clientDir, 'site');
// Ensure site directory exists
fs.mkdirSync(siteDir, { recursive: true });
// Template data
const data = {
businessName: config.businessName,
businessAddress: config.businessAddress,
businessEmail: config.businessEmail,
businessPhone: config.businessPhone,
businessDescription: config.businessDescription,
logoUrl: config.logoUrl || null,
clientId: config.clientId,
siteUrl: config.siteUrl,
website: config.website,
a2pPacket: config.a2pPacket,
colorScheme: config.colorScheme || {
primary: '#2563EB',
primaryDark: '#1D4ED8',
primaryLight: '#EFF6FF',
accent: '#F59E0B'
},
year: new Date().getFullYear(),
// When served via subdomain, baseUrl is empty (root-relative links)
// When served via /sites/{id}/, baseUrl has the prefix
// We build with empty baseUrl for subdomain-first approach
baseUrl: ''
};
// Render each page
const pages = [
{ template: 'index.ejs', output: 'index.html' },
{ template: 'contact.ejs', output: 'contact.html' },
{ template: 'privacy-policy.ejs', output: 'privacy-policy.html' },
{ template: 'terms.ejs', output: 'terms.html' },
];
for (const page of pages) {
const templatePath = path.join(TEMPLATES_DIR, page.template);
const template = fs.readFileSync(templatePath, 'utf8');
const html = ejs.render(template, data, { filename: templatePath });
fs.writeFileSync(path.join(siteDir, page.output), html);
}
// Copy the site CSS
const cssSource = path.join(__dirname, '..', 'public', 'css', 'site.css');
const assetsDir = path.join(siteDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.copyFileSync(cssSource, path.join(assetsDir, 'site.css'));
// If there's a logo, copy it to the site assets
if (config.logoUrl) {
const logoSource = path.join(__dirname, '..', config.logoUrl);
if (fs.existsSync(logoSource)) {
const ext = path.extname(logoSource);
fs.copyFileSync(logoSource, path.join(assetsDir, `logo${ext}`));
}
}
console.log(` → Built ${pages.length} pages in ${siteDir}`);
}
module.exports = { buildSite };