From 8a89e5eea2e923c81ea842ffadde933ed16eb0d0 Mon Sep 17 00:00:00 2001 From: Tatsuto YAMAMOTO Date: Mon, 26 Aug 2024 20:36:57 +0900 Subject: [PATCH 1/2] feat: implement document generator --- deno.jsonc | 20 ++++---- deno.lock | 82 ++++++++++++++++++++++++++++++++ scripts/generate.ts | 111 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+), 9 deletions(-) create mode 100644 deno.lock create mode 100644 scripts/generate.ts diff --git a/deno.jsonc b/deno.jsonc index 31131f5..6facdb0 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,11 +1,13 @@ { - "tasks": { - "build": "mdbook build", - "dev": "mdbook serve", - "watch": "mdbook watch", - "test": "mdbook test", - "clean": "mdbook clean", - "format": "deno fmt src/", - "format:check": "deno fmt --check src/" - } + "tasks": { + "build": "mdbook build", + "dev": "mdbook serve", + "watch": "mdbook watch", + "test": "mdbook test", + "clean": "mdbook clean", + "format": "deno fmt src/", + "format:check": "deno fmt --check src/", + "generate:doc": "deno run --allow-read --allow-write --allow-run scripts/generate.ts" + }, + "imports": { "@std/fs": "jsr:@std/fs@^1.0.1" } } diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..3f5a701 --- /dev/null +++ b/deno.lock @@ -0,0 +1,82 @@ +{ + "version": "3", + "packages": { + "specifiers": { + "jsr:@deno/cache-dir@0.8": "jsr:@deno/cache-dir@0.8.0", + "jsr:@deno/graph@0.73": "jsr:@deno/graph@0.73.1", + "jsr:@deno/graph@^0.69.7": "jsr:@deno/graph@0.69.10", + "jsr:@std/assert@^0.218.2": "jsr:@std/assert@0.218.2", + "jsr:@std/bytes@^0.218.2": "jsr:@std/bytes@0.218.2", + "jsr:@std/fmt@^0.218.2": "jsr:@std/fmt@0.218.2", + "jsr:@std/fs@^0.218.2": "jsr:@std/fs@0.218.2", + "jsr:@std/fs@^1.0.1": "jsr:@std/fs@1.0.1", + "jsr:@std/io@^0.218.2": "jsr:@std/io@0.218.2", + "jsr:@std/path@^0.218.2": "jsr:@std/path@0.218.2", + "jsr:@std/path@^1.0.2": "jsr:@std/path@1.0.2" + }, + "jsr": { + "@deno/cache-dir@0.8.0": { + "integrity": "e87e80a404958f6350d903e6238b72afb92468378b0b32111f7a1e4916ac7fe7", + "dependencies": [ + "jsr:@deno/graph@^0.69.7", + "jsr:@std/fmt@^0.218.2", + "jsr:@std/fs@^0.218.2", + "jsr:@std/io@^0.218.2", + "jsr:@std/path@^0.218.2" + ] + }, + "@deno/graph@0.69.10": { + "integrity": "38fe22ac5686f6ece5daeec5a4df65c6314d7d32adcc33f77917a13cfaffa26f" + }, + "@deno/graph@0.73.1": { + "integrity": "cd69639d2709d479037d5ce191a422eabe8d71bb68b0098344f6b07411c84d41" + }, + "@std/assert@0.218.2": { + "integrity": "7f0a5a1a8cf86607cd6c2c030584096e1ffad27fc9271429a8cb48cfbdee5eaf" + }, + "@std/bytes@0.218.2": { + "integrity": "91fe54b232dcca73856b79a817247f4a651dbb60d51baafafb6408c137241670" + }, + "@std/fmt@0.218.2": { + "integrity": "99526449d2505aa758b6cbef81e7dd471d8b28ec0dcb1491d122b284c548788a" + }, + "@std/fs@0.218.2": { + "integrity": "dd9431453f7282e8c577cc22c9e6d036055a9a980b5549f887d6012969fabcca" + }, + "@std/fs@1.0.1": { + "integrity": "d6914ca2c21abe591f733b31dbe6331e446815e513e2451b3b9e472daddfefcb", + "dependencies": [ + "jsr:@std/path@^1.0.2" + ] + }, + "@std/io@0.218.2": { + "integrity": "c64fbfa087b7c9d4d386c5672f291f607d88cb7d44fc299c20c713e345f2785f", + "dependencies": [ + "jsr:@std/assert@^0.218.2", + "jsr:@std/bytes@^0.218.2" + ] + }, + "@std/path@0.218.2": { + "integrity": "b568fd923d9e53ad76d17c513e7310bda8e755a3e825e6289a0ce536404e2662", + "dependencies": [ + "jsr:@std/assert@^0.218.2" + ] + }, + "@std/path@1.0.2": { + "integrity": "a452174603f8c620bd278a380c596437a9eef50c891c64b85812f735245d9ec7" + } + } + }, + "redirects": { + "https://deno.land/x/deno_doc/mod.ts": "https://deno.land/x/deno_doc@0.125.0/mod.ts" + }, + "remote": { + "https://deno.land/x/deno_doc@0.125.0/deno_doc_wasm.generated.js": "9e779483e5d106e9275a7ff3acd8c0916dda4e1ffeb0a450c1ef95f22c711ed5", + "https://deno.land/x/deno_doc@0.125.0/mod.ts": "6ec7b1c6c025f7c9b335c899149cf34752a385a0363b60563ea893d1916e70c9" + }, + "workspace": { + "dependencies": [ + "jsr:@std/fs@^1.0.1" + ] + } +} diff --git a/scripts/generate.ts b/scripts/generate.ts new file mode 100644 index 0000000..beb2d8f --- /dev/null +++ b/scripts/generate.ts @@ -0,0 +1,111 @@ +/* + * pulsate-dev/pulsate specification documentation generator + */ +import { walk } from "@std/fs"; + +const viewCodePath = + "https://github.com/pulsate-dev/pulsate/tree/main" as const; +const pathReplaceRegexp = new RegExp(/file:([a-zA-Z.0-9#;&]*?)"/, "g"); +// NOTE: basePath must set like this: "path/to/pkg/" +const basePath = Deno.args[0]; +if (!basePath) { + throw new Error("ERROR: basePath is not set."); +} + +const modules = [ + "accounts", + "drive", + "notes", + "timeline", +] as const; +const packages = [ + "adaptors", + "id/mod.ts", + "id/type.ts", + "intermodule", + "password", + "time", +] as const; + +const moduleSourceDirs = [ + "model", + "service", +] as const; + +const sourcePaths = [ + ...modules.map((module) => { + return moduleSourceDirs.map((dir) => { + return `${basePath}${module}/${dir}`; + }); + }), + ...packages.map((pkg) => { + return `${basePath}${pkg}`; + }), +]; + +const command = await new Deno.Command("deno", { + args: [ + "doc", + "--unstable-byonm", + "--unstable-sloppy-imports", + "--html", + "--name=pulsate", + ...sourcePaths.flat(), + ], +}).output(); +console.log(new TextDecoder().decode(command.stderr)); + +// remove *.test.ts files & directories +const testFiles = []; +for await (const v of walk("./docs", { includeSymlinks: false })) { + if (v.isFile) continue; + if (v.name.includes("test.ts")) { + testFiles.push(v.path); + } +} + +for (const v of testFiles) { + Deno.removeSync(v, { recursive: true }); +} + +const replaceFileLink = async (path: string) => { + let file = await Deno.readTextFile(path); + + const match = file.matchAll(pathReplaceRegexp); + if (!match) { + throw new Error("No match"); + } + + // file:以降を取得 + const matchStrings = [...match].map((v) => v[1]); + const splited = matchStrings.map((v) => v.split("/")); + + // pkg, module or package が連続していたらそれ以降を取得して結合 + for (const [i, v] of splited.entries()) { + // pkgより後ろを取得 + const last3 = v.slice(v.indexOf("pkg")); + + // 前からpkg, module or packageが連続するかどうかをチェック + if (last3[0] !== "pkg") throw new Error("Not found pkg"); + if ( + !(modules.includes(last3[1] as typeof modules[number]) || + packages.includes(last3[1] as typeof packages[number])) + ) { + // idの時は結合して合っているかを確かめる + const joined = last3.slice(1).join("/"); + if (!packages.includes(joined as typeof packages[number])) { + throw new Error("Not found module or package"); + } + } + + const path = `${viewCodePath}/${last3.join("/")}`; + + file = file.replaceAll(`file:${matchStrings[i]}`, path); + } + await Deno.writeTextFile(path, file, { append: false }); +}; + +for await (const v of walk("./docs")) { + if (v.isDirectory) continue; + replaceFileLink(v.path); +} From eee465b2c62f19fb31cb91e300f4e8f35a11c0db Mon Sep 17 00:00:00 2001 From: Tatsuto YAMAMOTO Date: Mon, 26 Aug 2024 21:13:37 +0900 Subject: [PATCH 2/2] chore: ignore auto-generated documents --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 35c8911..17f01e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ theme/ +docs/ \ No newline at end of file