profectus-docs/postProcess.js

95 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-07-14 05:25:11 +00:00
const fs = require("fs");
const path = require("path");
function walk(dir, cb) {
const list = fs.readdirSync(dir);
return Promise.all(list.map(file => {
const resolvedFile = path.resolve(dir, file);
const stat = fs.statSync(resolvedFile);
if (stat.isDirectory()) {
return walk(resolvedFile, cb);
} else {
return new Promise((resolve) => cb(dir, resolvedFile, resolve));
}
}));
}
2022-07-15 02:59:46 +00:00
(async () => {
2023-04-12 23:41:44 +00:00
// Replace overview file
2022-07-15 02:59:46 +00:00
fs.copyFileSync("./overview.md", "./docs/api/overview.md");
fs.unlinkSync("./docs/api/index.md");
2023-04-12 23:41:44 +00:00
// Copy components over
await walk("./components/components", (dir, file, resolve) => {
const relPath = path.relative("./components/components", dir);
let dest = path.resolve("./docs/api/components", relPath);
2022-07-15 02:59:46 +00:00
const filePath = path.resolve(dir, file);
const stream = fs.createReadStream(filePath);
let lineCount = 0;
stream.on("data", buffer => {
let idx = -1;
lineCount--; // Because the loop will run once for idx=-1
do {
idx = buffer.indexOf(10, idx + 1);
lineCount++;
} while (idx !== -1);
if (lineCount > 4) {
stream.destroy();
fs.mkdirSync(dest, { recursive: true });
fs.copyFileSync(filePath, path.resolve(dest, path.basename(file)));
}
resolve();
});
2022-07-14 05:25:11 +00:00
});
2023-04-12 23:41:44 +00:00
// Write features' components to end of file
await walk("./components/features", (dir, file, resolve) => {
const relPath = path.relative("./components/features", dir);
let dest = path.resolve("./docs/api/modules/features", relPath);
if (relPath == "infoboxes") {
dest = dest.slice(0, -2);
} else if (relPath === "tabs") {
dest += file.includes("TabComponent") ? "\\tab" : "\\tabFamily";
}
try {
fs.accessSync(dest + ".md", fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
dest = dest.slice(0, -1);
try {
fs.accessSync(dest + ".md", fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
console.log("Couldn't find file at", dest + ".md");
resolve();
return;
}
}
dest = dest + ".md";
let data = fs.readFileSync(dest).toString();
const elementData = fs.readFileSync(path.resolve(dir, file));
const fd = fs.openSync(dest, "w+");
const componentsSection = data.indexOf("## Components");
if (componentsSection == -1) {
data += `\n## Components\n`;
}
fs.writeSync(fd, data);
fs.writeSync(fd, "\n" + elementData + "\n");
fs.closeSync(fd);
resolve();
});
// Add frontmatter to every file
2022-07-14 05:25:11 +00:00
const frontmatter = Buffer.from("---\neditLink: false\n---\n");
2022-07-15 02:59:46 +00:00
await walk("./docs/api", function addFrontmatter(dir, file, resolve) {
2022-07-14 05:25:11 +00:00
if (path.extname(file) !== ".md") return;
const filePath = path.resolve(dir, file);
2023-04-14 06:43:05 +00:00
const data = fs.readFileSync(filePath).toString().replaceAll(/\[K in keyof T\]/g, "\\[K in keyof T]");
2022-07-14 05:25:11 +00:00
const fd = fs.openSync(filePath, "w+");
fs.writeSync(fd, frontmatter);
2023-04-12 23:41:44 +00:00
fs.writeSync(fd, data);
2022-07-14 05:25:11 +00:00
fs.closeSync(fd);
resolve();
});
2022-07-15 02:59:46 +00:00
})();