|
| 1 | +import { parse } from "react-docgen"; |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | + |
| 4 | +const componentFile = "lib/Fitter.tsx"; |
| 5 | +const outputFile = "README.md"; |
| 6 | + |
| 7 | +const componentCode = await readFile(componentFile, "utf-8"); |
| 8 | +const [documentation] = parse(componentCode); |
| 9 | + |
| 10 | +const tableRows = []; |
| 11 | + |
| 12 | +for (const [name, prop] of Object.entries(documentation.props ?? {})) { |
| 13 | + const typeName = prop.tsType?.name ?? prop.flowType?.name; |
| 14 | + const defaultValue = prop.defaultValue?.value |
| 15 | + ? `\`${prop.defaultValue?.value}\`` |
| 16 | + : ""; |
| 17 | + const defaultColumn = prop.required ? "(required)" : defaultValue; |
| 18 | + const description = (prop.description ?? "") |
| 19 | + .replace(/\n/g, " ") |
| 20 | + .replace(/\s+/g, " "); |
| 21 | + |
| 22 | + const columns = [ |
| 23 | + `\`${name}\``, |
| 24 | + `\`${typeName}\``, |
| 25 | + defaultColumn, |
| 26 | + description, |
| 27 | + ]; |
| 28 | + |
| 29 | + tableRows.push(columns.join(" | ")); |
| 30 | +} |
| 31 | + |
| 32 | +const tableHeader = ["Prop name", "Type", "Default value", "Description"].join( |
| 33 | + " | ", |
| 34 | +); |
| 35 | +const tableDivider = ["---", "---", "---", "---"].join(" | "); |
| 36 | +const table = [tableHeader, tableDivider, ...tableRows].join("\n"); |
| 37 | + |
| 38 | +const outputContent = await readFile(outputFile, "utf-8"); |
| 39 | +const newContent = outputContent.replace( |
| 40 | + /<!-- PROPS_TABLE_START -->.*<!-- PROPS_TABLE_END -->/s, |
| 41 | + `<!-- PROPS_TABLE_START -->\n${table}\n<!-- PROPS_TABLE_END -->`, |
| 42 | +); |
| 43 | +await writeFile(outputFile, newContent); |
0 commit comments