#!/usr/bin/env -S deno run -A import { parse } from "jsr:@std/yaml"; import { existsPath, exit, joinPath, log, readFileToString, writeStringToFile, } from "../libraries/deno-commons-mod.ts"; async function main(): Promise { const dirs = Deno.readDir("."); const metas: CheatSheetMeta[] = []; for await (const dirEntry of dirs) { if (dirEntry.isDirectory) { log.debug(dirEntry.name); const cheatSheetMdFile = joinPath(dirEntry.name, "CHEATSHEET.md"); if (!await existsPath(cheatSheetMdFile)) { log.debug(`Cheat sheet md not exists: ${cheatSheetMdFile}`); continue; } const cheatSheetMd = await readFileToString(cheatSheetMdFile); try { const cheatSheetMeta = parseCheatSheetMeta(cheatSheetMd); metas.push(cheatSheetMeta); } catch (e) { log.error( `Parse cheat sheet md: ${cheatSheetMdFile} failed: ${e}`, ); } } } metas.sort((m1, m2) => { return m1.name.localeCompare(m2.name); }); await writeStringToFile("meta.json", JSON.stringify(metas, null, 4)); } interface CheatSheetMeta { name: string; aliases?: Array; repo?: string; description?: string; } function parseCheatSheetMeta(cheatSheetMd: string): CheatSheetMeta { const yaml = findMetaYaml(cheatSheetMd); let meta = parse(yaml); return meta as CheatSheetMeta; } function findMetaYaml(cheatSheetMd: string): string { const lines = cheatSheetMd.split(/\r?\n/); let yaml = []; let inMeta = false; let inYaml = false; for (const line of lines) { if (inMeta) { if (inYaml) { if (line.startsWith("```")) { inYaml = false; return yaml.join("\n"); } else { yaml.push(line); } } else { if (/^```\s*yaml\s*$/i.test(line)) { inYaml = true; } } } else { if (/^\s*([#]+)\s*meta\s*$/i.test(line)) { inMeta = true; } } } throw new Error("Parse cheat sheet md meta failed"); } main().catch((err) => { log.error(err); exit(1); }).then(() => exit(0));