|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +// cspell:ignore astro |
| 4 | +// This script check the docs are compatible for astro to simplify migration |
| 5 | +// It check each docs has |
| 6 | +// - a `title:` frontmatter |
| 7 | +import { readFile, readdir } from "fs/promises"; |
| 8 | +import { resolve } from "path"; |
| 9 | +import { repoRoot } from "./helpers.js"; |
| 10 | + |
| 11 | +const docsFolder = resolve(repoRoot, "docs"); |
| 12 | + |
| 13 | +async function findMarkdownFiles(folder) { |
| 14 | + const items = await readdir(folder, { withFileTypes: true }); |
| 15 | + |
| 16 | + return ( |
| 17 | + await Promise.all( |
| 18 | + items.map(async (item) => { |
| 19 | + if (item.isDirectory()) { |
| 20 | + const files = await findMarkdownFiles(resolve(folder, item.name)); |
| 21 | + return files.map((x) => resolve(folder, x)); |
| 22 | + } |
| 23 | + |
| 24 | + if (item.name.endsWith(".md")) { |
| 25 | + return [resolve(folder, item.name)]; |
| 26 | + } else { |
| 27 | + return []; |
| 28 | + } |
| 29 | + }) |
| 30 | + ) |
| 31 | + ).flat(); |
| 32 | +} |
| 33 | +await main(); |
| 34 | + |
| 35 | +async function main() { |
| 36 | + const docs = await findMarkdownFiles(docsFolder); |
| 37 | + |
| 38 | + const failure = []; |
| 39 | + const regex = /^---.*title:.*---$/ms; |
| 40 | + for (const doc of docs) { |
| 41 | + const buffer = await readFile(doc, { encoding: "utf-8" }); |
| 42 | + const content = buffer.toString(); |
| 43 | + if (!regex.test(content)) { |
| 44 | + failure.push(doc); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (failure.length > 0) { |
| 49 | + console.log("Files with missing title: frontmatter", failure); |
| 50 | + |
| 51 | + console.log( |
| 52 | + [ |
| 53 | + "Make sure to add front matter in the file with title, e.g.:", |
| 54 | + "---", |
| 55 | + "title: xyz", |
| 56 | + "---", |
| 57 | + ].join("\n") |
| 58 | + ); |
| 59 | + process.exit(1); |
| 60 | + } else { |
| 61 | + console.log("Docs look good!"); |
| 62 | + } |
| 63 | +} |
0 commit comments