import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; import { build } from "esbuild"; import { writeWebLicenseNotices } from "./package-licenses.mjs"; import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { tsImport } from "tsx/esm/api"; const source = new URL("../site/", import.meta.url); const output = new URL("../build/site/", import.meta.url); await rm(output, { recursive: true, force: true }); await mkdir(output, { recursive: true }); await cp(source, output, { recursive: true, filter: (path) => !path.endsWith(".ts") && !path.endsWith(".tsx") && !path.endsWith("tsconfig.json") && !path.endsWith("README.md"), }); await cp(new URL("../LICENSE", import.meta.url), new URL("LICENSE", output)); const [{ WELCOME_LINES }, { catalogs }, { Glyph }] = await Promise.all([ "../src/client/components/living/WelcomeLine.tsx", "../src/client/locales/index.ts", "../src/client/ui/icons.tsx", ].map(path => tsImport(path, { parentURL: import.meta.url, tsconfig: fileURLToPath(new URL("../src/tsconfig.json", import.meta.url)), }))); // Reuse the product's paired lines as inert HTML, without a runtime catalog. const welcomeLines = WELCOME_LINES.map(({ key }) => renderToStaticMarkup( createElement("span", { "data-welcome-key": key }, createElement("span", { lang: "en" }, `“${catalogs.en[key]}”`), createElement("span", { lang: "zh-CN" }, `“${catalogs.zh[key]}”`), ), )); let homepage = (await readFile(new URL("index.html", source), "utf8")) .replace(/
.*?<\/p>/, `
${welcomeLines[0]}
`); if (process.env.PIIK_WEBSITE_RELEASE_DATA) { const { websiteDownloads } = await tsImport("../site/downloads.ts", { parentURL: import.meta.url }); const release = websiteDownloads(JSON.parse(await readFile(process.env.PIIK_WEBSITE_RELEASE_DATA, "utf8"))); homepage = homepage.replace(/]*data-download="([^"]+)"[^>]*data-provider="([^"]+)"[^>]*)>([\s\S]*?)<\/a\s*>/g, (original, attributes, target, provider, content) => { const item = release.packages.find(item => item.target === target); const href = provider === 'github' ? item?.url : item?.mirrorURL; if (!href) return original; if (provider === 'gitee') content = content .replace(/[\s\S]*?<\/span\s*>/, `Gitee alternative · ${release.version} ZIP`) .replace(/[\s\S]*?<\/span\s*>/, `Gitee 备用 · ${release.version} ZIP`); return `${content}`; }); } await writeFile(new URL("index.html", output), homepage.replace( '', `${welcomeLines.join('')}`, )); // The opening shot uses the current homepage, with only its film clock adapter // substituted for the ordinary page script. Keep its layout and copy in one place. await writeFile(new URL("film/ui/website.html", output), homepage .replace('
', '
') .replace('', '') .replace('', (await readFile(new URL("film/ui/index.html", source), "utf8")).match(/