SRS/tools/preprop.ts
2022-06-11 15:10:10 +09:00

86 lines
2.4 KiB
TypeScript

import { readAll } from "https://deno.land/std@0.143.0/streams/mod.ts";
import * as log from "https://deno.land/std@0.143.0/log/mod.ts";
import { WriterHandler } from "https://deno.land/std@0.143.0/log/handlers.ts";
import * as Eta from "https://deno.land/x/eta@v1.12.3/mod.ts";
import { Issue } from "./githubType.ts";
class StderrHandler extends WriterHandler {
protected _writer: Deno.Writer;
#encoder: TextEncoder;
constructor(levelName: log.LevelName, options: log.HandlerOptions = {}){
super(levelName, options);
this.#encoder = new TextEncoder();
this._writer = Deno.stderr;
}
log(msg: string): void {
const encoded = this.#encoder.encode(msg);
Deno.stderr.writeSync(encoded);
}
}
interface Book {
sections: Section[];
}
interface Section {
//chapter or separtor or PartTitle
Chapter: Chapter;
}
interface Chapter {
name: string;
content: string;
/** section number */
number?: number[];
sub_items: Section[];
path?: string;
source_path?: string;
parent_names: string[];
}
if (import.meta.main) {
await log.setup({
handlers: {
console: new StderrHandler("INFO", {
formatter: "{levelName} {msg}",
}),
},
loggers: {
default: {
level: "INFO",
handlers: ["console"],
}
}
}
);
main(Deno.args);
}
async function getIssues(){
const issue_path = "cache/issues.json";
const c = await Deno.readTextFile(issue_path);
const issues = JSON.parse(c) as Issue[];
issues.sort((a, b) => a.number - b.number);
return issues;
}
async function main(args: string[]) {
if (args.length > 1) {
//log.info(`args: ${JSON.stringify(args)}`);
if (args[0] === "supports") {
Deno.exit(0);
}
}
const issues = await getIssues();
log.info(`start`);
const data = await readAll(Deno.stdin);
const jsonText = new TextDecoder().decode(data);
await Deno.writeTextFile("log.json", jsonText);
const [context, book] = JSON.parse(jsonText) as [any, Book];
book.sections.forEach(x=>{
x.Chapter.content = Eta.render(x.Chapter.content, {
issues: issues
}) as string;
})
//Deno.stderr.writeSync(new TextEncoder().encode(`context: ${JSON.stringify(context)}\n`));
console.log(JSON.stringify(book));
}