import fs from "node:fs";
import { appsConfig } from "../src/config/apps.config";
import { ANSI } from "../packages/core/src/constants";
import { NAME, TAG_LINE } from "../src/config/branding.config";
import { BASE_URL, BUILD_DIR, DOMAIN } from "../src/config/deploy.config";
import { desktopConfig } from "../src/config/desktop.config";
const PATHS = {
sitemapXml: BUILD_DIR + "/sitemap.xml",
robotsTxt: BUILD_DIR + "/robots.txt",
indexHtml: BUILD_DIR + "/index.html",
cname: BUILD_DIR + "/CNAME",
};
function generateSitemapXml() {
const date = new Date();
const lastModified = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
const images = desktopConfig.wallpapers.map((path) => `
${BASE_URL.slice(0, -1) + path}
`
);
const pages = appsConfig.apps.map(({ id }) => `
${BASE_URL + id}
${lastModified}
`
);
const sitemap = `
${BASE_URL}
${lastModified}
${images.join("")}
${pages.join("")}
`;
return sitemap.trim();
}
function generateRobotsTxt() {
const sitemapUrl = BASE_URL + PATHS.sitemapXml.replace(BUILD_DIR + "/", "");
return `# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Sitemap: ${sitemapUrl}`;
}
function generateCname() {
return DOMAIN;
}
function generateTemplate(html: string) {
const baseUrlRegex = /(?<=")https?:\/\/[a-z0-9-]+(\.)([a-zA-Z0-9-]+(\.))*[a-z]{2,3}\/(?=.*")/gi;
html = html.replaceAll(baseUrlRegex, BASE_URL);
const commentsRegex = //g;
html = html.replaceAll(commentsRegex, "");
const emptyLinesRegex = /^\s*$\n/gm;
html = html.replaceAll(emptyLinesRegex, "");
const path = `${BUILD_DIR}/index.html`;
fs.writeFileSync(path, html, { flag: "w+" });
console.log(`- ${ANSI.fg.cyan}${path}${ANSI.reset}`);
return html;
}
/**
* To avoid GitHub pages rendering certain pages that are only defined by React router as a 404 page,
* we copy the content of our index file to the 404 page, letting React router properly handle routing on every page
*/
function generate404Page(template: string) {
const path = `${BUILD_DIR}/404.html`;
fs.writeFileSync(path, template, { flag: "w+" });
console.log(`- ${ANSI.fg.cyan}${path}${ANSI.reset}`);
}
/**
* Add an HTML file for every app page so they can be properly crawled and indexed
*/
function generateAppPages(template: string) {
for (const app of appsConfig.apps) {
const appId = app.id;
const appName = app.name;
const appDescription = app.description ?? TAG_LINE;
if (appId === "index") {
console.log("Invalid app ID found: " + appId);
return;
}
let html = template;
const titleRegex = /(?<=(
|))/g;
html = html.replaceAll(titleRegex, `${appName} | ${NAME}`);
const descriptionRegex = /(?<=())/g;
html = html.replaceAll(descriptionRegex, appDescription);
const canonicalRegex = /(?<=())/g;
html = html.replaceAll(canonicalRegex, BASE_URL + appId);
const faqRegex = /.*?