diff --git a/script-meta-v2.json b/script-meta-v2.json index fe3cd5a..4f4aead 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -338,6 +338,15 @@ "publish_time": 1775409068941, "update_time": 1775409068941 }, + "update-cheatsheet.ts": { + "script_name": "update-cheatsheet.ts", + "script_length": 2422, + "script_sha256": "2b60ecbfc08caf85b4cbfde266ba49d51e86930506c5930ed420559ac712e625", + "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/update-cheatsheet.ts", + "single_script_file": true, + "publish_time": 1775925235458, + "update_time": 1775925235458 + }, "upper.ts": { "script_name": "upper.ts", "script_length": 390, diff --git a/single-scripts/update-cheatsheet.ts b/single-scripts/update-cheatsheet.ts new file mode 100755 index 0000000..b2b235b --- /dev/null +++ b/single-scripts/update-cheatsheet.ts @@ -0,0 +1,86 @@ +#!/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));