📜 Update script metadata to include new cheatsheet script

This commit is contained in:
2026-04-12 00:34:03 +08:00
parent 8facf78352
commit 367c628131
2 changed files with 95 additions and 0 deletions

View File

@@ -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,

View File

@@ -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<void> {
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<string>;
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));