|
| 1 | +/** |
| 2 | + * Autogenerate documentation for source files. This assumes a very specific format. |
| 3 | + * |
| 4 | + * run with |
| 5 | + * ``` |
| 6 | + * npx esr scripts/build-docs.ts |
| 7 | + * ``` |
| 8 | + */ |
| 9 | + |
| 10 | +import * as fs from "node:fs/promises"; |
| 11 | +import * as path from "node:path"; |
| 12 | +import { Project, Node } from "ts-morph"; |
| 13 | + |
| 14 | +const README_COMMENT: string = `<!-- DO NOT MODIFY --> |
| 15 | +<!-- This file was autogenerated by build-docs.ts --> |
| 16 | +<!-- Edit the docstring in index.ts and regenerate --> |
| 17 | +<!-- rather than editing this file directly. --> |
| 18 | +`; |
| 19 | + |
| 20 | +// We can only import ESM modules via async import, so all the action happens here. |
| 21 | +(async function () { |
| 22 | + const chalk = await (await import("chalk")).default; |
| 23 | + const { unified } = await import("unified"); |
| 24 | + const inject = await (await import("mdast-util-inject")).default; |
| 25 | + const b = await import("mdast-builder"); |
| 26 | + const { getDataOnAllExports } = await import("./libs/get-symbols"); |
| 27 | + const { |
| 28 | + gfmRemark, |
| 29 | + makeConstantsSection, |
| 30 | + makeFunctionsSection, |
| 31 | + makeTypesSection, |
| 32 | + makePluginsSection, |
| 33 | + } = await ( |
| 34 | + await import("./libs/markdown-utils") |
| 35 | + ).default; |
| 36 | + type MdNode = ReturnType<typeof b.text>; |
| 37 | + |
| 38 | + /////////////////////////////////////////////////////////// |
| 39 | + // Build the documentation for all exports |
| 40 | + /////////////////////////////////////////////////////////// |
| 41 | + const project = new Project(); |
| 42 | + project.addSourceFilesAtPaths("./packages/**/*.ts"); |
| 43 | + |
| 44 | + // Find all the files we want to process. These have th form .../unified-latex.../index.ts, |
| 45 | + // however we only want the top-level index.ts files (no any that may be in subdirectories). |
| 46 | + |
| 47 | + const filesToProcess = project |
| 48 | + .getSourceFiles() |
| 49 | + .filter((file) => |
| 50 | + file.getFilePath().match(/unified-latex[^/]*\/index\.ts/) |
| 51 | + ); |
| 52 | + |
| 53 | + console.log(chalk.bold("Processing"), filesToProcess.length, "files"); |
| 54 | + for (const file of filesToProcess) { |
| 55 | + console.log( |
| 56 | + " ", |
| 57 | + chalk.magenta(file.getDirectory().getBaseName()), |
| 58 | + `/ ${file.getBaseName()}` |
| 59 | + ); |
| 60 | + |
| 61 | + const outPath = path.join(file.getDirectoryPath(), "README.md"); |
| 62 | + |
| 63 | + const readme = generateReadme(file.getFilePath()); |
| 64 | + |
| 65 | + console.log( |
| 66 | + chalk.gray(" Writing to", path.relative(__dirname, outPath)) |
| 67 | + ); |
| 68 | + |
| 69 | + await fs.writeFile(outPath, readme, { encoding: "utf-8" }); |
| 70 | + } |
| 71 | + |
| 72 | + /* |
| 73 | + const filename = |
| 74 | + // "./packages/unified-latex/unified-latex-types/index.ts"; |
| 75 | + "./packages/unified-latex/unified-latex-util-replace/index.ts"; |
| 76 | + //"./packages/unified-latex/unified-latex-util-trim/index.ts"; |
| 77 | + //"./packages/unified-latex/unified-latex-util-visit/index.ts"; |
| 78 | + */ |
| 79 | + |
| 80 | + function generateReadme(filename): string { |
| 81 | + const sourceFile = project.getSourceFileOrThrow(filename); |
| 82 | + |
| 83 | + const accumulatedExports = getDataOnAllExports(sourceFile); |
| 84 | + const constantsMd = makeConstantsSection(accumulatedExports.constants); |
| 85 | + const typesMd = makeTypesSection(accumulatedExports.types); |
| 86 | + const funcsMd = makeFunctionsSection(accumulatedExports.funcs); |
| 87 | + const pluginMd = makePluginsSection(accumulatedExports.plugins); |
| 88 | + |
| 89 | + // Grab the comments from index.ts |
| 90 | + const jsDoc = sourceFile.getLastToken().getFirstChild(); |
| 91 | + let introText = ""; |
| 92 | + if (Node.isJSDoc(jsDoc)) { |
| 93 | + introText = jsDoc.getCommentText(); |
| 94 | + } |
| 95 | + |
| 96 | + const directoryName = sourceFile.getDirectory().getBaseName(); |
| 97 | + let skeletonReadme = `# ${directoryName}`; |
| 98 | + if (introText) { |
| 99 | + skeletonReadme += "\n\n" + introText; |
| 100 | + } |
| 101 | + if (accumulatedExports.plugins.length > 0) { |
| 102 | + skeletonReadme += "\n\n# Plugins"; |
| 103 | + } |
| 104 | + if (accumulatedExports.funcs.length > 0) { |
| 105 | + skeletonReadme += "\n\n# Functions"; |
| 106 | + } |
| 107 | + if (accumulatedExports.constants.length > 0) { |
| 108 | + skeletonReadme += "\n\n# Constants"; |
| 109 | + } |
| 110 | + if (accumulatedExports.types.length > 0) { |
| 111 | + skeletonReadme += "\n\n# Types"; |
| 112 | + } |
| 113 | + |
| 114 | + // Assemble the dynamically-generated data |
| 115 | + const target = gfmRemark.parse(skeletonReadme); |
| 116 | + |
| 117 | + if (accumulatedExports.plugins.length > 0) { |
| 118 | + inject("Plugins", target, pluginMd); |
| 119 | + } |
| 120 | + if (accumulatedExports.funcs.length > 0) { |
| 121 | + inject("Functions", target, funcsMd); |
| 122 | + } |
| 123 | + if (accumulatedExports.constants.length > 0) { |
| 124 | + inject("Constants", target, constantsMd); |
| 125 | + } |
| 126 | + if (accumulatedExports.types.length > 0) { |
| 127 | + inject("Types", target, typesMd); |
| 128 | + } |
| 129 | + |
| 130 | + const renderedReadme = `${README_COMMENT}${gfmRemark.stringify( |
| 131 | + target |
| 132 | + )}`; |
| 133 | + |
| 134 | + return renderedReadme; |
| 135 | + } |
| 136 | +})(); |
0 commit comments