Files
bitburner-src/tools/bundle-doc/index.js
2025-07-23 14:16:24 -07:00

77 lines
3.4 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const docFiles = [];
const nsDocFiles = [];
const docImagesFiles = [];
const docRoot = path.resolve(__dirname, "../../src/Documentation/doc");
const markdownRoot = path.resolve(__dirname, "../../markdown");
const docImagesRoot = path.resolve(__dirname, "../../src/Documentation/images");
const addFileToListOfDocPages = (files, root, filePath) => {
// Windows path uses "\", so we need to replace it with "/".
files.push(path.relative(root, filePath).replace(/\\/g, "/"));
};
const processDir = (dir) => {
if (!fs.existsSync(dir)) {
return;
}
console.log(dir);
for (const file of fs.readdirSync(dir).sort((a, b) => a.localeCompare(b, "en-US"))) {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
processDir(filePath);
continue;
}
if (filePath.startsWith(docRoot)) {
addFileToListOfDocPages(docFiles, docRoot, filePath);
} else if (filePath.startsWith(markdownRoot)) {
addFileToListOfDocPages(nsDocFiles, markdownRoot, filePath);
} else {
addFileToListOfDocPages(docImagesFiles, docImagesRoot, filePath);
}
}
};
processDir(docRoot);
processDir(markdownRoot);
processDir(docImagesRoot);
/**
* When importing NS API markdown files, we use "nsDoc_something" instead of file<number> like other docs. Currently,
* there are more than 1400 files in the "markdown" folder. If we use the file<number> pattern, every time we add or
* remove a file, it creates a massive diff change. For example, let's say it looks like this:
*
* import file100 from "../../markdown/bitburner.activefragment.highestcharge.md?raw";
* import file101 from "../../markdown/bitburner.activefragment.id.md?raw";
* ...
* import file1500 from "../../markdown/index.md?raw";
*
* If we delete "bitburner.activefragment.id.md", all lines below "import file100 ..." will be changed. In this case,
* the maintainer has to either check all of those lines, which is a meticulous task, or ignore them and hope that
* nothing goes wrong.
*
* By using the nsDoc_something pattern, we don't have this problem. Until now, we also have had this problem with docs
* in src\Documentation\doc, but we rarely add or remove files in it, so it's not a big deal. Checking ~60 lines is also
* much easier than checking ~1400 lines.
*/
const autogeneratedContent = `// THIS FILE IS AUTOGENERATED. DO NOT EDIT IT MANUALLY.
${docFiles.map((f, i) => `import file${i} from "./doc/${f}?raw";\n`).join("")}
${nsDocFiles.map((f) => `import nsDoc_${f.replaceAll(".", "_")} from "../../markdown/${f}?raw";\n`).join("")}
${docImagesFiles.map((f) => `import docImages_${f.replaceAll(/\.|-/g, "_")} from "./images/${f}";\n`).join("")}
export const AllPages: Record<string, string> = {};
${docFiles.map((f, i) => `AllPages["${f}"] = file${i};\n`).join("")}
${nsDocFiles.map((f) => `AllPages["nsDoc/${f}"] = nsDoc_${f.replaceAll(".", "_")};\n`).join("")}
export const nsApiPages = Object.keys(AllPages)
.filter((page) => page.startsWith("nsDoc"))
.map((page) => page.replace("nsDoc/", ""));
export const DocImages: Record<string, string> = {};
${docImagesFiles.map((f) => `DocImages["${f}"] = docImages_${f.replaceAll(/\.|-/g, "_")};\n`).join("")}`;
fs.writeFile(path.resolve(__dirname, "../../src/Documentation/pages.ts"), autogeneratedContent, (err) => {
if (err) {
console.error(err);
}
// file written successfully
});