2022-03-21 00:53:16 +08:00
|
|
|
import url from "url";
|
|
|
|
import path from "path";
|
|
|
|
import fs from "fs-extra";
|
|
|
|
|
|
|
|
import json5 from "json5";
|
|
|
|
|
|
|
|
import { renderMdx } from "./mdx.ts";
|
|
|
|
|
|
|
|
const PUBLIC_DIR = "public";
|
|
|
|
|
|
|
|
const PEOPLE_DIR = "people";
|
2022-04-09 05:38:52 +08:00
|
|
|
const COMMENTS_DIR = "comments";
|
2022-03-21 00:53:16 +08:00
|
|
|
|
|
|
|
const DIST_DIR = "dist";
|
|
|
|
const DIST_PEOPLE_LIST = "people-list.json";
|
|
|
|
|
|
|
|
const projectRoot = path.dirname(path.dirname(url.fileURLToPath(import.meta.url)));
|
|
|
|
const peopleDir = path.join(projectRoot, PEOPLE_DIR);
|
|
|
|
const people = fs.readdirSync(peopleDir).map(person => ({
|
|
|
|
dirname: person,
|
|
|
|
srcPath: path.join(peopleDir, person),
|
|
|
|
distPath: path.join(projectRoot, DIST_DIR, PEOPLE_DIR, person)
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Transform `info.json5` to `info.json`.
|
|
|
|
// Extract metadata from `people/${dirname}/info.json5` to `dist/people-list.json`.
|
|
|
|
function buildPeopleInfoAndList() {
|
|
|
|
const PEOPLE_LIST_KEYS = ["id", "name", "profileUrl"] as const;
|
|
|
|
|
|
|
|
type PeopleMeta = Record<"path" | typeof PEOPLE_LIST_KEYS[number], unknown>;
|
|
|
|
const peopleList: PeopleMeta[] = [];
|
|
|
|
|
|
|
|
for (const { dirname, srcPath, distPath } of people) {
|
2022-11-17 14:15:59 +08:00
|
|
|
for (const lang of ['', '.zh_hant']) {
|
|
|
|
const infoFile = fs.readFileSync(path.join(srcPath, `info${lang}.json5`), "utf-8");
|
|
|
|
const info = json5.parse(infoFile);
|
|
|
|
|
|
|
|
// Add meta information
|
|
|
|
const peopleMeta = {
|
|
|
|
path: dirname,
|
|
|
|
...Object.fromEntries(PEOPLE_LIST_KEYS.map(key => [key, info[key]]))
|
|
|
|
} as PeopleMeta;
|
|
|
|
|
2022-11-17 15:30:42 +08:00
|
|
|
// Avoid duplicates
|
|
|
|
if (peopleList.filter(it => it.id == peopleMeta.id).length == 0)
|
|
|
|
peopleList.push(peopleMeta);
|
2022-11-17 14:15:59 +08:00
|
|
|
|
|
|
|
// Combine comments in people/${dirname}/comments/${cf}.json
|
|
|
|
const commentPath = path.join(srcPath, COMMENTS_DIR)
|
|
|
|
fs.ensureDirSync(commentPath)
|
|
|
|
info.comments = fs.readdirSync(commentPath)
|
2022-04-09 05:38:52 +08:00
|
|
|
.filter(cf => cf.endsWith('.json'))
|
|
|
|
.map(cf => JSON.parse(fs.readFileSync(path.join(commentPath, cf), 'utf-8')))
|
|
|
|
|
2022-11-17 14:15:59 +08:00
|
|
|
// Write info.json
|
|
|
|
fs.ensureDirSync(distPath);
|
|
|
|
fs.writeFileSync(path.join(distPath, `info${lang}.json`), JSON.stringify(info));
|
|
|
|
}
|
2022-03-21 00:53:16 +08:00
|
|
|
}
|
|
|
|
|
2022-04-09 05:38:52 +08:00
|
|
|
// Write people-list.json
|
2022-03-21 00:53:16 +08:00
|
|
|
fs.writeFileSync(path.join(projectRoot, DIST_DIR, DIST_PEOPLE_LIST), JSON.stringify(peopleList));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render `people/${dirname}/page.md` to `dist/people/${dirname}/page.js`.
|
|
|
|
function buildPeoplePages() {
|
|
|
|
for (const { srcPath, distPath } of people) {
|
2022-11-17 14:15:59 +08:00
|
|
|
for (const lang of ['', '.zh_hant'])
|
|
|
|
{
|
|
|
|
const markdown = fs.readFileSync(path.join(srcPath, `page${lang}.md`), "utf-8");
|
|
|
|
const result = renderMdx(markdown);
|
2022-03-21 00:53:16 +08:00
|
|
|
|
2022-11-17 14:15:59 +08:00
|
|
|
fs.ensureDirSync(distPath);
|
|
|
|
fs.writeFileSync(path.join(distPath, `page${lang}.js`), result);
|
|
|
|
}
|
2022-03-21 00:53:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 05:38:52 +08:00
|
|
|
// Copy `people/${dirname}/photos` to `dist/people/${dirname}/`.
|
2022-03-21 00:53:16 +08:00
|
|
|
function copyPeopleAssets() {
|
2022-04-09 05:38:52 +08:00
|
|
|
const PEOPLE_ASSETS = ["photos"];
|
2022-03-21 00:53:16 +08:00
|
|
|
|
|
|
|
for (const { srcPath, distPath } of people) {
|
|
|
|
fs.ensureDirSync(distPath);
|
|
|
|
|
|
|
|
for (const assetDirname of PEOPLE_ASSETS) {
|
|
|
|
const assetSrcPath = path.join(srcPath, assetDirname);
|
|
|
|
if (fs.pathExistsSync(assetSrcPath)) {
|
|
|
|
fs.copySync(assetSrcPath, path.join(distPath, assetDirname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy files `public` to dist.
|
|
|
|
function copyPublic() {
|
|
|
|
fs.copySync(path.join(projectRoot, PUBLIC_DIR), path.join(projectRoot, DIST_DIR));
|
|
|
|
}
|
|
|
|
|
|
|
|
buildPeopleInfoAndList();
|
|
|
|
buildPeoplePages();
|
|
|
|
copyPeopleAssets();
|
|
|
|
copyPublic();
|