mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
UI: Search and read NS API docs in editor tab and documentation tab (#2163)
This commit is contained in:
@@ -1,32 +1,65 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const files = [];
|
||||
const docRoot = "./src/Documentation/doc";
|
||||
const docFiles = [];
|
||||
const nsDocFiles = [];
|
||||
const docRoot = path.resolve(__dirname, "../../src/Documentation/doc");
|
||||
const markdownRoot = path.resolve(__dirname, "../../markdown");
|
||||
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) => {
|
||||
console.log(dir);
|
||||
for (const file of fs.readdirSync(dir)) {
|
||||
const path = `${dir}/${file}`;
|
||||
if (fs.lstatSync(`${dir}/${file}`).isDirectory()) {
|
||||
processDir(path);
|
||||
const filePath = path.join(dir, file);
|
||||
if (fs.lstatSync(filePath).isDirectory()) {
|
||||
processDir(filePath);
|
||||
continue;
|
||||
}
|
||||
if (path.startsWith(docRoot + "/")) {
|
||||
files.push(path.slice(docRoot.length + 1));
|
||||
if (filePath.startsWith(docRoot)) {
|
||||
addFileToListOfDocPages(docFiles, docRoot, filePath);
|
||||
} else {
|
||||
addFileToListOfDocPages(nsDocFiles, markdownRoot, filePath);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
processDir(docRoot);
|
||||
processDir(markdownRoot);
|
||||
|
||||
const autogenfile = `// THIS FILE IS AUTOGENERATED
|
||||
${files.map((f, i) => `import file${i} from "./doc/${f}?raw";`).join("\n")}
|
||||
/**
|
||||
* 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";`).join("\n")}
|
||||
${nsDocFiles.map((f, i) => `import nsDoc_${f.replaceAll(".", "_")} from "../../markdown/${f}?raw";`).join("\n")}
|
||||
|
||||
import type { Document } from "./root.ts";
|
||||
export const AllPages: Record<string, string> = {};
|
||||
${docFiles.map((f, i) => `AllPages["${f}"] = file${i};`).join("\n")}
|
||||
${nsDocFiles.map((f, i) => `AllPages["nsDoc/${f}"] = nsDoc_${f.replaceAll(".", "_")};`).join("\n")}
|
||||
|
||||
export const AllPages: Record<string, Document> = {};
|
||||
${files.map((f, i) => `AllPages["${f}"] = file${i};`).join("\n")}
|
||||
export const nsApiPages = Object.keys(AllPages)
|
||||
.filter((page) => page.startsWith("nsDoc"))
|
||||
.map((page) => page.replace("nsDoc/", ""));
|
||||
`;
|
||||
|
||||
fs.writeFile(docRoot + "/../pages.ts", autogenfile, (err) => {
|
||||
fs.writeFile(path.resolve(__dirname, "../../src/Documentation/pages.ts"), autogeneratedContent, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user