SRS/tools/printDocument.ts

106 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-04-20 21:58:10 +09:00
2022-04-20 23:03:33 +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";
2022-04-20 21:58:10 +09:00
import * as Eta from "https://deno.land/x/eta/mod.ts";
2022-04-20 23:03:33 +09:00
async function readContent(path?: string): Promise<string> {
2022-04-20 21:58:10 +09:00
let content = "[]";
2022-04-20 23:03:33 +09:00
if (path) {
2022-04-20 21:58:10 +09:00
content = await Deno.readTextFile(path);
}
2022-04-20 23:03:33 +09:00
else if (!Deno.isatty(Deno.stdin.rid)) {
const decoder = new TextDecoder(undefined, { ignoreBOM: true });
2022-04-20 21:58:10 +09:00
const buf = await readAll(Deno.stdin);
content = decoder.decode(buf);
}
2022-04-21 01:05:01 +09:00
else throw new Error("No input provided. path or stdin.");
2022-04-20 21:58:10 +09:00
return content;
}
2022-04-20 23:03:33 +09:00
async function readAndPrint(args: {
path?: string,
overall?: boolean,
outpath?: string
}) {
2022-04-20 21:58:10 +09:00
const c = await readContent(args.path);
const issues = JSON.parse(c) as Issue[];
2022-04-20 23:03:33 +09:00
issues.sort((a, b) => a.number - b.number);
2022-04-20 22:24:47 +09:00
let print: string = "";
2022-04-20 23:03:33 +09:00
if (args.overall) {
2022-04-21 17:53:14 +09:00
print = await Eta.renderFile("overall.md", {
2022-04-20 23:03:33 +09:00
issues: issues
2022-04-20 22:24:47 +09:00
}) as string;
2022-04-20 21:58:10 +09:00
}
2022-04-20 23:03:33 +09:00
else {
2022-04-21 17:53:14 +09:00
print = await Eta.renderFile("specific.md", {
2022-04-20 23:03:33 +09:00
issues: issues
}) as string;
2022-04-20 21:58:10 +09:00
}
2022-04-20 23:03:33 +09:00
if (args.outpath) {
Deno.writeTextFileSync(args.outpath, print);
2022-04-20 22:24:47 +09:00
}
2022-04-20 23:03:33 +09:00
else {
2022-04-20 22:24:47 +09:00
console.log(print);
}
2022-04-20 23:03:33 +09:00
}
async function main() {
const parsedArg = argParse(Deno.args);
2022-04-21 01:05:01 +09:00
const { path, outpath, overall, w, watch } = parsedArg;
const watchMode = w || watch;
2022-04-20 23:03:33 +09:00
if (typeof path !== "undefined" && typeof 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);
}
2022-04-21 01:05:01 +09:00
if (typeof watchMode !== "undefined" && typeof watchMode !== "boolean") {
2022-04-20 23:03:33 +09:00
console.log("Please provide a boolean value for w.");
Deno.exit(1);
}
2022-04-21 01:05:01 +09:00
if (watchMode && typeof path === "undefined") {
2022-04-20 23:03:33 +09:00
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(path);
const issues = JSON.parse(c) as Issue[];
issues.sort((a, b) => a.number - b.number);
await readAndPrint({ path, outpath, overall });
2022-04-21 01:05:01 +09:00
if (watchMode) {
2022-04-20 23:03:33 +09:00
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();
2022-04-20 21:58:10 +09:00
}