Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refine gen-mod.ts #135

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# These are supported funding model platforms

github: [lambdalisue] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
github: [
lambdalisue,
] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
Expand Down
57 changes: 11 additions & 46 deletions .scripts/gen-mod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { fromFileUrl, globToRegExp, join, relative } from "@std/path";
import {
fromFileUrl,
globToRegExp,
join,
relative,
toFileUrl,
} from "@std/path";
import { map } from "@core/iterutil/map";
import { flatMap } from "@core/iterutil/async/flat-map";

const decoder = new TextDecoder();
import { doc } from "@deno/doc";

const excludes = [
"mod.ts",
Expand All @@ -11,47 +16,6 @@ const excludes = [
"_*.ts",
];

type DenoDocEntry = {
name: string;
location: {
filename: string;
};
declarationKind: string;
jsDoc: {
doc: string;
};
kind: string;
};

function isDenoDocEntry(x: unknown): x is DenoDocEntry {
if (x == null || typeof x !== "object") return false;
if (typeof (x as DenoDocEntry).name !== "string") return false;
if (typeof (x as DenoDocEntry).location !== "object") return false;
if (typeof (x as DenoDocEntry).location.filename !== "string") return false;
if (typeof (x as DenoDocEntry).declarationKind !== "string") return false;
if (typeof (x as DenoDocEntry).jsDoc !== "object") return false;
if (typeof (x as DenoDocEntry).jsDoc.doc !== "string") return false;
if (typeof (x as DenoDocEntry).kind !== "string") return false;
return true;
}

async function listDenoDocEntries(path: string): Promise<DenoDocEntry[]> {
const cmd = new Deno.Command(Deno.execPath(), {
args: ["doc", "--json", path],
stdout: "piped",
stderr: "piped",
});
const { success, stdout, stderr } = await cmd.output();
if (!success) {
throw new Error(decoder.decode(stderr));
}
const json = JSON.parse(decoder.decode(stdout));
if (!Array.isArray(json)) {
throw new Error(`Expected array but got ${JSON.stringify(json)}`);
}
return json.filter(isDenoDocEntry);
}

async function* iterModules(path: string): AsyncIterable<string> {
const patterns = excludes.map((p) => globToRegExp(p));
for await (const entry of Deno.readDir(path)) {
Expand All @@ -66,15 +30,16 @@ async function generateModTs(
): Promise<void> {
const path = fromFileUrl(import.meta.resolve(`../${namespace}/`));
const exports = (await Array.fromAsync(
flatMap(iterModules(path), (x) => listDenoDocEntries(x)),
flatMap(iterModules(path), (x) => doc(toFileUrl(x).href)),
))
.filter((x) => !!x.jsDoc?.doc)
.filter((x) => x.kind === "function")
.filter((x) => x.declarationKind === "export")
.filter((x) => x.name.startsWith(namespace))
.map((x) => ({
path: relative(path, fromFileUrl(x.location.filename)),
name: x.name,
doc: x.jsDoc.doc,
doc: x.jsDoc!.doc!,
}))
.toSorted((a, b) => a.name.localeCompare(b.name));
const lines = [
Expand Down
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@core/iterutil": "jsr:@core/iterutil@^0.3.0",
"@core/unknownutil": "./mod.ts",
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
"@deno/doc": "jsr:@deno/doc@^0.153.0",
"@std/assert": "jsr:@std/assert@^1.0.4",
"@std/jsonc": "jsr:@std/jsonc@^1.0.0",
"@std/path": "jsr:@std/path@^1.0.2",
Expand All @@ -85,7 +86,7 @@
"test": "deno test -A --doc --parallel --shuffle",
"test:coverage": "deno task test --coverage=.coverage",
"coverage": "deno coverage .coverage",
"gen": "deno run --allow-run=deno --allow-read --allow-write=. .scripts/gen-mod.ts",
"gen": "deno run --allow-net=jsr.io --allow-env --allow-read --allow-write=. .scripts/gen-mod.ts",
"update": "deno run --allow-env --allow-read --allow-write=. --allow-run=git,deno --allow-net=jsr.io,registry.npmjs.org jsr:@molt/cli ./*.ts",
"update:commit": "deno task -q update --commit --prefix deps: --pre-commit=fmt,lint"
}
Expand Down