|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const fs = require("fs/promises"); |
| 4 | +const fsSync = require("fs"); |
| 5 | +const path = require("path"); |
| 6 | +const { |
| 7 | + repoRoot, |
| 8 | + projectAssetMap, |
| 9 | + fullPathFromRel, |
| 10 | + collectCatalog, |
| 11 | + buildDocPayload, |
| 12 | + fetchGithubRepo, |
| 13 | +} = require("../web/lib/content"); |
| 14 | + |
| 15 | +const distRoot = path.join(repoRoot, "dist"); |
| 16 | +const siteRoot = path.join(distRoot, "zh-cn"); |
| 17 | +const publicRoot = path.join(repoRoot, "web", "public"); |
| 18 | +const katexRoot = path.join(repoRoot, "node_modules", "katex", "dist"); |
| 19 | + |
| 20 | +async function ensureDir(dirPath) { |
| 21 | + await fs.mkdir(dirPath, { recursive: true }); |
| 22 | +} |
| 23 | + |
| 24 | +async function emptyDir(dirPath) { |
| 25 | + await fs.rm(dirPath, { recursive: true, force: true }); |
| 26 | + await ensureDir(dirPath); |
| 27 | +} |
| 28 | + |
| 29 | +async function copyFilePreserveDir(fromPath, toPath) { |
| 30 | + await ensureDir(path.dirname(toPath)); |
| 31 | + await fs.copyFile(fromPath, toPath); |
| 32 | +} |
| 33 | + |
| 34 | +async function copyDirectory(source, target) { |
| 35 | + await ensureDir(target); |
| 36 | + const entries = await fs.readdir(source, { withFileTypes: true }); |
| 37 | + for (const entry of entries) { |
| 38 | + const fromPath = path.join(source, entry.name); |
| 39 | + const toPath = path.join(target, entry.name); |
| 40 | + if (entry.isDirectory()) { |
| 41 | + await copyDirectory(fromPath, toPath); |
| 42 | + } else if (entry.isFile()) { |
| 43 | + await copyFilePreserveDir(fromPath, toPath); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function collectFileRefs(value, bucket) { |
| 49 | + const matches = String(value).match(/files\/([^"'()\s?#]+(?:\?[^"'()\s#]*)?)/g) || []; |
| 50 | + for (const match of matches) { |
| 51 | + const relPath = decodeURIComponent( |
| 52 | + match.replace(/^files\//, "").replace(/\?.*$/, ""), |
| 53 | + ); |
| 54 | + bucket.add(relPath); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function collectHomeAssetRefs() { |
| 59 | + const appSource = fsSync.readFileSync(path.join(publicRoot, "app.js"), "utf8"); |
| 60 | + const refs = new Set(); |
| 61 | + const matches = appSource.matchAll(/\b(?:media|image):\s*"([^"]+)"/g); |
| 62 | + for (const match of matches) { |
| 63 | + refs.add(match[1]); |
| 64 | + } |
| 65 | + return refs; |
| 66 | +} |
| 67 | + |
| 68 | +async function buildSite() { |
| 69 | + await emptyDir(distRoot); |
| 70 | + await ensureDir(siteRoot); |
| 71 | + |
| 72 | + await copyDirectory(publicRoot, siteRoot); |
| 73 | + await copyDirectory(katexRoot, path.join(siteRoot, "vendor", "katex")); |
| 74 | + |
| 75 | + const catalog = await collectCatalog({ withVersion: true }); |
| 76 | + const github = await fetchGithubRepo().catch(() => ({ |
| 77 | + stars: null, |
| 78 | + forks: null, |
| 79 | + updatedAt: null, |
| 80 | + })); |
| 81 | + |
| 82 | + const dataRoot = path.join(siteRoot, "data"); |
| 83 | + const docsRoot = path.join(dataRoot, "docs"); |
| 84 | + await ensureDir(docsRoot); |
| 85 | + |
| 86 | + await fs.writeFile( |
| 87 | + path.join(dataRoot, "catalog.json"), |
| 88 | + JSON.stringify(catalog, null, 2), |
| 89 | + "utf8", |
| 90 | + ); |
| 91 | + await fs.writeFile( |
| 92 | + path.join(dataRoot, "github.json"), |
| 93 | + JSON.stringify(github, null, 2), |
| 94 | + "utf8", |
| 95 | + ); |
| 96 | + |
| 97 | + const referencedRepoFiles = collectHomeAssetRefs(); |
| 98 | + Object.values(projectAssetMap).forEach((relPath) => { |
| 99 | + if (relPath && fsSync.existsSync(fullPathFromRel(relPath))) { |
| 100 | + referencedRepoFiles.add(relPath); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + for (const doc of catalog.docs) { |
| 105 | + const payload = await buildDocPayload(doc.relPath, catalog, { withVersion: true }); |
| 106 | + collectFileRefs(payload.html, referencedRepoFiles); |
| 107 | + await fs.writeFile( |
| 108 | + path.join(docsRoot, `${doc.id}.json`), |
| 109 | + JSON.stringify(payload), |
| 110 | + "utf8", |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + for (const relPath of referencedRepoFiles) { |
| 115 | + const sourcePath = fullPathFromRel(relPath); |
| 116 | + if (!fsSync.existsSync(sourcePath)) continue; |
| 117 | + const stat = fsSync.statSync(sourcePath); |
| 118 | + if (!stat.isFile()) continue; |
| 119 | + const toPath = path.join(siteRoot, "files", ...relPath.split("/")); |
| 120 | + await copyFilePreserveDir(sourcePath, toPath); |
| 121 | + } |
| 122 | + |
| 123 | + const faviconRel = projectAssetMap.favicon; |
| 124 | + if (faviconRel && fsSync.existsSync(fullPathFromRel(faviconRel))) { |
| 125 | + await copyFilePreserveDir(fullPathFromRel(faviconRel), path.join(siteRoot, "favicon.png")); |
| 126 | + } |
| 127 | + |
| 128 | + const rootRedirect = `<!doctype html> |
| 129 | +<html lang="zh-CN"> |
| 130 | + <head> |
| 131 | + <meta charset="utf-8" /> |
| 132 | + <meta http-equiv="refresh" content="0; url=./zh-cn/" /> |
| 133 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 134 | + <title>Every-Embodied</title> |
| 135 | + </head> |
| 136 | + <body> |
| 137 | + <p>Redirecting to <a href="./zh-cn/">./zh-cn/</a>...</p> |
| 138 | + </body> |
| 139 | +</html> |
| 140 | +`; |
| 141 | + |
| 142 | + const noJekyllPath = path.join(distRoot, ".nojekyll"); |
| 143 | + await fs.writeFile(path.join(distRoot, "index.html"), rootRedirect, "utf8"); |
| 144 | + await fs.writeFile(noJekyllPath, "", "utf8"); |
| 145 | + |
| 146 | + const siteIndex = await fs.readFile(path.join(siteRoot, "index.html"), "utf8"); |
| 147 | + await fs.writeFile(path.join(siteRoot, "404.html"), siteIndex, "utf8"); |
| 148 | +} |
| 149 | + |
| 150 | +buildSite().catch((error) => { |
| 151 | + console.error(error); |
| 152 | + process.exitCode = 1; |
| 153 | +}); |
0 commit comments