|
| 1 | +import { replaceInFile } from "../deps.ts"; |
| 2 | +import { XSnippetDefinition, XSnippetVariant } from "../models/app.ts"; |
| 3 | +import { |
| 4 | + $col, |
| 5 | + $colCode, |
| 6 | + $colCodeBlock, |
| 7 | + $row, |
| 8 | + $table, |
| 9 | + htmlComment, |
| 10 | + joinByDoubleNewLine, |
| 11 | + joinByNewLine, |
| 12 | +} from "./table-html.ts"; |
| 13 | + |
| 14 | +type SnippetRow = { |
| 15 | + prefix: string; |
| 16 | + name: string; |
| 17 | + body: string | string[]; |
| 18 | +}; |
| 19 | + |
| 20 | +const snippetRow = ({ prefix, name, body }: SnippetRow) => { |
| 21 | + const parsedBody = Array.isArray(body) ? body.join("\n") : body; |
| 22 | + const cols = joinByNewLine([ |
| 23 | + $colCode(prefix), |
| 24 | + $col(name), |
| 25 | + $colCodeBlock(parsedBody), |
| 26 | + ]); |
| 27 | + |
| 28 | + return $row(cols); |
| 29 | +}; |
| 30 | + |
| 31 | +const generateSnippetTable = (items: SnippetRow[]) => { |
| 32 | + const headings = ["Prefix", "Name", "Body"]; |
| 33 | + const rows = items.map(snippetRow); |
| 34 | + |
| 35 | + return $table(headings, rows); |
| 36 | +}; |
| 37 | + |
| 38 | +const generateSnippetSection = ( |
| 39 | + { meta, snippets }: XSnippetDefinition, |
| 40 | +) => { |
| 41 | + const title = `### ${meta.title}`; |
| 42 | + const description = meta.description ?? ""; |
| 43 | + const table = generateSnippetTable( |
| 44 | + Object.entries(snippets).map(([prefix, value]) => ({ |
| 45 | + ...value, |
| 46 | + prefix, |
| 47 | + })), |
| 48 | + ); |
| 49 | + |
| 50 | + return joinByNewLine([title, description, table, ""]); |
| 51 | +}; |
| 52 | + |
| 53 | +const generateVariantSection = (variant: XSnippetVariant) => { |
| 54 | + const title = `## ${variant.label}`; |
| 55 | + const description = variant.description ?? ""; |
| 56 | + const sections = variant.snippetDefinitions.map(generateSnippetSection); |
| 57 | + |
| 58 | + return joinByNewLine([title, description, "", ...sections]); |
| 59 | +}; |
| 60 | + |
| 61 | +export const generateDocs = (variants: XSnippetVariant[]) => { |
| 62 | + return joinByDoubleNewLine(variants.map(generateVariantSection)); |
| 63 | +}; |
| 64 | + |
| 65 | +const docsGenId = "docs-gen"; |
| 66 | +const docsGen = { |
| 67 | + start: htmlComment(`START:${docsGenId}`), |
| 68 | + end: htmlComment(`END:${docsGenId}`), |
| 69 | +}; |
| 70 | + |
| 71 | +const docsBlock = (s: string) => { |
| 72 | + return joinByNewLine([docsGen.start, s, docsGen.end]); |
| 73 | +}; |
| 74 | + |
| 75 | +export const populateDocsBlock = async (input: string) => { |
| 76 | + const regex = new RegExp( |
| 77 | + `${docsGen.start}[\\s\\S]*?${docsGen.end}`, |
| 78 | + "g", |
| 79 | + ); |
| 80 | + |
| 81 | + const file = "./README.md"; |
| 82 | + const options = { |
| 83 | + files: file, |
| 84 | + from: regex, |
| 85 | + to: docsBlock(input), |
| 86 | + }; |
| 87 | + |
| 88 | + try { |
| 89 | + const results = await replaceInFile(options); |
| 90 | + const readmeResult = results.find((r) => r.file === file); |
| 91 | + |
| 92 | + if (readmeResult?.hasChanged) { |
| 93 | + console.log("✅ README updated"); |
| 94 | + } else { |
| 95 | + console.log("👍 README already up to date"); |
| 96 | + } |
| 97 | + } catch (error) { |
| 98 | + console.error("Error while updating README:", error); |
| 99 | + } |
| 100 | +}; |
0 commit comments