|
| 1 | +/* eslint-env node */ |
| 2 | +// eslint-disable console |
| 3 | +import { promises } from "fs"; |
| 4 | +import path from "path"; |
| 5 | + |
| 6 | +import filesPaths, { docsPath, outPath, copyDir } from "./utils/io.js"; |
| 7 | +import pageTemplate from "./templates/page.js"; |
| 8 | +import tocContent from "./templates/toc.js"; |
| 9 | +import generateModuleDocs from "./templates/module.js"; |
| 10 | + |
| 11 | +const { |
| 12 | + writeFile: writeFileAsync, |
| 13 | + readFile: readFileAsync, |
| 14 | + mkdir: mkdirAsync, |
| 15 | + rmdir: rmdirAsync |
| 16 | +} = promises; |
| 17 | + |
| 18 | +let data = {}; |
| 19 | + |
| 20 | +const addData = (filePath, fileData) => { |
| 21 | + const pathParts = path |
| 22 | + .dirname(filePath) |
| 23 | + .slice(process.cwd().length) |
| 24 | + .split(path.sep) |
| 25 | + .filter(x => x != ""); |
| 26 | + |
| 27 | + const moduleName = [...pathParts].pop(); |
| 28 | + |
| 29 | + if (data[moduleName]) { |
| 30 | + data[moduleName].functions.push(fileData); |
| 31 | + } else { |
| 32 | + data[moduleName] = { |
| 33 | + pathParts, |
| 34 | + functions: [fileData] |
| 35 | + }; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const main = async cwd => { |
| 40 | + console.log("Clearing dist..."); |
| 41 | + const outputPath = outPath(); |
| 42 | + |
| 43 | + await rmdirAsync(outputPath, { recursive: true }); |
| 44 | + await mkdirAsync(outputPath, { recursive: true }); |
| 45 | + await copyDir(docsPath("css"), outPath("css")); |
| 46 | + await copyDir(docsPath("scripts"), outPath("scripts")); |
| 47 | + |
| 48 | + console.log("Generating html documentation files..."); |
| 49 | + |
| 50 | + for (const path of filesPaths(cwd)) { |
| 51 | + const content = await readFileAsync(path, "utf8"); |
| 52 | + const data = JSON.parse(content); |
| 53 | + |
| 54 | + addData(path, data); |
| 55 | + } |
| 56 | + |
| 57 | + const toc = tocContent(data); |
| 58 | + |
| 59 | + let mainPageContent = ""; |
| 60 | + |
| 61 | + for (const moduleName of Object.keys(data)) { |
| 62 | + const moduleData = data[moduleName]; |
| 63 | + |
| 64 | + mainPageContent += await generateModuleDocs({ |
| 65 | + data: moduleData, |
| 66 | + toc, |
| 67 | + name: moduleName |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + await writeFileAsync( |
| 72 | + outPath("index.html"), |
| 73 | + pageTemplate({ content: mainPageContent, toc }) |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +main(process.cwd()); |
0 commit comments