SRS/tools/printDocument.ts

154 lines
4.9 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";
2022-04-22 15:53:54 +09:00
import { copy } from "https://deno.land/std@0.136.0/fs/mod.ts";
2022-04-20 23:03:33 +09:00
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";
2022-04-22 22:51:41 +09:00
import {
normalize, join as pathJoin, fromFileUrl, parse as parsePath
, relative
} from "https://deno.land/std@0.135.0/path/mod.ts";
2022-04-21 19:23:36 +09:00
import * as Eta from "https://deno.land/x/eta@v1.12.3/mod.ts";
2022-04-22 22:51:41 +09:00
import { createReactive } from "./reactivity.ts";
2022-04-20 21:58:10 +09:00
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-21 19:23:36 +09:00
type printDocParam = {
2022-04-21 20:29:52 +09:00
target: string,
data: {
2022-04-21 19:23:36 +09:00
issues: Issue[]
2022-04-20 21:58:10 +09:00
}
2022-04-21 19:23:36 +09:00
};
2022-04-21 20:29:52 +09:00
async function printDoc(param: printDocParam, option?: {
2022-04-21 19:23:36 +09:00
outDir?: string
2022-04-21 20:29:52 +09:00
}) {
2022-04-21 19:23:36 +09:00
option = option ?? {};
2022-04-21 20:29:52 +09:00
const { target, data } = param;
2022-04-21 19:23:36 +09:00
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);
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
}
2022-04-21 19:23:36 +09:00
async function readAndPrint(args: {
issue_path?: string,
2022-04-21 20:29:52 +09:00
outDir?: string,
targets: string[],
2022-04-21 19:23:36 +09:00
}) {
const c = await readContent(args.issue_path);
const issues = JSON.parse(c) as Issue[];
issues.sort((a, b) => a.number - b.number);
2022-04-21 20:29:52 +09:00
for (const target of args.targets) {
2022-04-21 19:23:36 +09:00
await printDoc({
target: target,
data: {
issues
2022-04-21 20:29:52 +09:00
}
}
, {
outDir: args.outDir
});
2022-04-21 19:23:36 +09:00
}
}
2022-04-20 23:03:33 +09:00
async function main() {
const parsedArg = argParse(Deno.args);
2022-04-22 22:51:41 +09:00
const { issue_path, outDirArg, w, watch } = parsedArg;
2022-04-21 01:05:01 +09:00
const watchMode = w || watch;
2022-04-21 18:55:48 +09:00
if (typeof issue_path !== "undefined" && typeof issue_path !== "string") {
2022-04-20 23:03:33 +09:00
console.log("Please provide a path to the json file.");
Deno.exit(1);
}
2022-04-22 22:51:41 +09:00
if (typeof outDirArg !== "undefined" && typeof outDirArg !== "string") {
2022-04-20 23:03:33 +09:00
console.log("Please provide a path to the output file.");
Deno.exit(1);
}
2022-04-22 22:51:41 +09:00
const outDir = (outDirArg ?? "build");
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 18:55:48 +09:00
if (watchMode && typeof issue_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,
});
2022-04-22 22:51:41 +09:00
const issuesR = await createReactive(async () => {
const c = await readContent(issue_path);
const issues = JSON.parse(c) as Issue[];
issues.sort((a, b) => a.number - b.number);
return issues;
});
2022-04-21 20:29:52 +09:00
const targets = ["SUMMARY.md", "overall.md", "specific.md", "intro.md", "support.md"];
2022-04-22 22:51:41 +09:00
const targetsR = await Promise.all(targets.map(async (t) => {
return await createReactive(() => {
return readAndPrint({ issue_path, outDir, targets:[t] });
});
}
));
issuesR.wireTo(...targetsR);
const copyOp = await createReactive(async () => {
const files = [...Deno.readDirSync(viewPath)].map(x => x.name).filter(x => !x.endsWith(".md"));
const op = files.map(x => copy(pathJoin(viewPath, x), pathJoin(outDir, x), { overwrite: true }));
await Promise.all(op);
});
2022-04-21 01:05:01 +09:00
if (watchMode) {
2022-04-21 19:23:36 +09:00
const watcher = Deno.watchFs([viewPath, issue_path as string]);
2022-04-20 23:03:33 +09:00
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(", ")}`);
2022-04-21 20:29:52 +09:00
for (const path of event.paths) {
const p = parsePath(path);
2022-04-22 22:51:41 +09:00
if (p.dir === viewPath) {
if(p.ext === ".md") {
targetsR[targets.indexOf(p.base)].update();
}
else{
copyOp.update();
}
2022-04-21 20:29:52 +09:00
}
2022-04-22 22:51:41 +09:00
else if (p.base === "issues.json") {
await issuesR.update();
2022-04-21 20:29:52 +09:00
}
}
2022-04-20 23:03:33 +09:00
}
}
}
}
if (import.meta.main) {
main();
2022-04-20 21:58:10 +09:00
}