SRS/tools/printDocument.ts

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-04-20 21:58:10 +09:00
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){
2022-04-20 22:24:47 +09:00
const args = argParse(Deno.args);
2022-04-20 21:58:10 +09:00
const url = new URL(import.meta.url)
url.pathname = normalize(pathJoin(url.pathname,"..","template"));
const path = fromFileUrl(url);
Eta.configure({views: path});
2022-04-20 22:24:47 +09:00
if(typeof args.path !== "string"){
console.log("Please provide a path to the json file.");
Deno.exit(1);
}
2022-04-20 21:58:10 +09:00
const c = await readContent(args.path);
const issues = JSON.parse(c) as Issue[];
2022-04-20 22:24:47 +09:00
let print: string = "";
2022-04-20 21:58:10 +09:00
if(args.overall){
2022-04-20 22:24:47 +09:00
print = await Eta.renderFile("overall.md.eta",{
2022-04-20 21:58:10 +09:00
issues: issues.sort((a,b)=>a.number-b.number)
2022-04-20 22:24:47 +09:00
}) as string;
2022-04-20 21:58:10 +09:00
}
else{
printContent(issues);
}
2022-04-20 22:24:47 +09:00
if(args.outpath){
Deno.writeTextFileSync(args.outpath,print);
}
else{
console.log(print);
}
2022-04-20 21:58:10 +09:00
}