106 lines
3.0 KiB
TypeScript
Executable File
106 lines
3.0 KiB
TypeScript
Executable File
#!/usr/bin/env runts -- --allow-all
|
|
|
|
import {parse} from "jsr:@std/yaml";
|
|
import {
|
|
existsPath,
|
|
exit,
|
|
joinPath,
|
|
log,
|
|
readFileToString,
|
|
writeStringToFile,
|
|
} from "https://script.hatter.ink/@70/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));
|
|
|
|
let md = [];
|
|
md.push("# cheatsheet");
|
|
md.push("");
|
|
|
|
md.push("| Name | Alias | Cheat Sheet | Description |");
|
|
md.push("| ----- | ----- | ----- | ----- |");
|
|
for (const meta of metas) {
|
|
md.push(
|
|
`| ${meta.name} | ${
|
|
meta.aliases ? meta.aliases : "-"
|
|
} | [CHEATSHEET.md](${meta.name}/CHEATSHEET.md) | ${meta.description} |`,
|
|
);
|
|
}
|
|
md.push("");
|
|
await writeStringToFile("README.md", md.join("\n"));
|
|
}
|
|
|
|
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));
|
|
|
|
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T004945+08:00.MEQCIFlvRR177uPSgZ9hGzaa
|
|
// 2WekjFgmYLSvZ1XnSZr0LdtMAiBgd18LDfJBUKz9YS8ouOI5FMTkM4H34J9Wq/iKC6aB9w==
|