import { Issue } from "./githubType.ts"; import { readAll } from "https://deno.land/std@0.135.0/streams/mod.ts" import { parse as argParse } from "https://deno.land/std@0.135.0/flags/mod.ts"; import { normalize, join as pathJoin, fromFileUrl } from "https://deno.land/std@0.135.0/path/mod.ts"; import * as Eta from "https://deno.land/x/eta/mod.ts"; async function readContent(path?: string): Promise { let content = "[]"; if (path) { content = await Deno.readTextFile(path); } else if (!Deno.isatty(Deno.stdin.rid)) { const decoder = new TextDecoder(undefined, { ignoreBOM: true }); const buf = await readAll(Deno.stdin); content = decoder.decode(buf); } else throw new Error("No input provided. path or stdin."); return content; } async function readAndPrint(args: { path?: string, overall?: boolean, outpath?: string }) { const c = await readContent(args.path); const issues = JSON.parse(c) as Issue[]; issues.sort((a, b) => a.number - b.number); let print: string = ""; if (args.overall) { print = await Eta.renderFile("overall.md", { issues: issues }) as string; } else { print = await Eta.renderFile("specific.md", { issues: issues }) as string; } if (args.outpath) { Deno.writeTextFileSync(args.outpath, print); } else { console.log(print); } } async function main() { const parsedArg = argParse(Deno.args); const { issue_path, outpath, overall, w, watch } = parsedArg; const watchMode = w || watch; if (typeof issue_path !== "undefined" && typeof issue_path !== "string") { console.log("Please provide a path to the json file."); Deno.exit(1); } if (typeof outpath !== "undefined" && typeof outpath !== "string") { console.log("Please provide a path to the output file."); Deno.exit(1); } if (typeof overall !== "undefined" && typeof overall !== "boolean") { console.log("Please provide a boolean value for overall."); Deno.exit(1); } if (typeof watchMode !== "undefined" && typeof watchMode !== "boolean") { console.log("Please provide a boolean value for w."); Deno.exit(1); } if (watchMode && typeof issue_path === "undefined") { console.log("Could not set watch mode without a path."); Deno.exit(1); } const url = new URL(import.meta.url) url.pathname = normalize(pathJoin(url.pathname, "..", "template")); const viewPath = fromFileUrl(url); Eta.configure({ views: viewPath, "view cache": false, }); const c = await readContent(issue_path); const issues = JSON.parse(c) as Issue[]; issues.sort((a, b) => a.number - b.number); await readAndPrint({ path, outpath, overall }); if (watchMode) { const watcher = Deno.watchFs([viewPath, path as string]); for await (const event of watcher) { if (event.kind === "modify") { Deno.stdout.write( new TextEncoder().encode("\x1b[2J\x1b[0f"), ); console.log(`reloading ${event.paths.join(", ")}`); readAndPrint({ path: path as string, overall: overall as boolean, outpath: outpath as string }); } } } } if (import.meta.main) { main(); }