SRS/tools/printDocument.ts
2022-04-20 22:24:47 +09:00

79 lines
2.3 KiB
TypeScript

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";
function printOverall(issues: Issue[]){
const table = new Map<string,Issue[]>();
issues.forEach((x)=>{
const category = x.title.split(":")[0];
if(!category) return;
let c = table.get(category)
if(!c){
c = []
table.set(category,c);
}
c.push(x);
})
let index = 1;
for (const [c,issues] of table) {
console.log(`### 2.2.${index++} ${c} Operation\n`);
let subIndex = 1;
for (const i of issues) {
console.log(`${subIndex++}. #${i.number} ${i.title}`);
}
console.log("");
}
}
function printContent(issues: Issue[]){
console.log(issues.map(i => `## (${i.number}) ${i.title}\n${i.body}`).join("\n\n"));
}
async function readContent(path?: string):Promise<string>{
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);
}
return content;
}
if(import.meta.main){
const args = argParse(Deno.args);
const url = new URL(import.meta.url)
url.pathname = normalize(pathJoin(url.pathname,"..","template"));
const path = fromFileUrl(url);
Eta.configure({views: path});
if(typeof args.path !== "string"){
console.log("Please provide a path to the json file.");
Deno.exit(1);
}
const c = await readContent(args.path);
const issues = JSON.parse(c) as Issue[];
let print: string = "";
if(args.overall){
print = await Eta.renderFile("overall.md.eta",{
issues: issues.sort((a,b)=>a.number-b.number)
}) as string;
}
else{
printContent(issues);
}
if(args.outpath){
Deno.writeTextFileSync(args.outpath,print);
}
else{
console.log(print);
}
}