SRS/tools/printDocument.ts
2022-04-21 20:29:52 +09:00

140 lines
4.4 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, parse as parsePath } from "https://deno.land/std@0.135.0/path/mod.ts";
import * as Eta from "https://deno.land/x/eta@v1.12.3/mod.ts";
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);
}
else throw new Error("No input provided. path or stdin.");
return content;
}
type printDocParam = {
target: string,
data: {
issues: Issue[]
}
};
async function printDoc(param: printDocParam, option?: {
outDir?: string
}) {
option = option ?? {};
const { target, data } = param;
const { outDir } = option;
let print: string = "";
print = await Eta.renderFile(target, data) as string;
if (outDir) {
const outPath = pathJoin(outDir, target);
await Deno.mkdir(pathJoin(outDir), { recursive: true });
await Deno.writeTextFile(outPath, print);
}
else {
console.log(print);
}
}
async function readAndPrint(args: {
issue_path?: string,
outDir?: string,
targets: string[],
}) {
const c = await readContent(args.issue_path);
const issues = JSON.parse(c) as Issue[];
issues.sort((a, b) => a.number - b.number);
for (const target of args.targets) {
await printDoc({
target: target,
data: {
issues
}
}
, {
outDir: args.outDir
});
}
}
async function main() {
const parsedArg = argParse(Deno.args);
const { issue_path, outDir, 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 outDir !== "undefined" && typeof outDir !== "string") {
console.log("Please provide a path to the output file.");
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);
const targets = ["SUMMARY.md", "overall.md", "specific.md", "intro.md", "support.md"];
await readAndPrint({ issue_path, outDir, targets });
if (watchMode) {
const watcher = Deno.watchFs([viewPath, issue_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(", ")}`);
let reloadAll = false;
for (const path of event.paths) {
const p = parsePath(path);
if (targets.includes(p.base)) {
readAndPrint({
issue_path: issue_path as string,
outDir: outDir as string,
targets: [p.base]
});
}
else{
reloadAll = true;
}
}
if (reloadAll) {
readAndPrint({
issue_path: issue_path as string,
outDir: outDir as string,
targets: targets
});
}
}
}
}
}
if (import.meta.main) {
main();
}