Skip to content

Commit e377132

Browse files
authored
Merge pull request #135 from jsr-core/chore-gen
chore: refine `gen-mod.ts`
2 parents 747a1ee + 727fcc0 commit e377132

File tree

3 files changed

+16
-48
lines changed

3 files changed

+16
-48
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# These are supported funding model platforms
22

3-
github: [lambdalisue] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
3+
github: [
4+
lambdalisue,
5+
] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
46
patreon: # Replace with a single Patreon username
57
open_collective: # Replace with a single Open Collective username
68
ko_fi: # Replace with a single Ko-fi username

.scripts/gen-mod.ts

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { fromFileUrl, globToRegExp, join, relative } from "@std/path";
1+
import {
2+
fromFileUrl,
3+
globToRegExp,
4+
join,
5+
relative,
6+
toFileUrl,
7+
} from "@std/path";
28
import { map } from "@core/iterutil/map";
39
import { flatMap } from "@core/iterutil/async/flat-map";
4-
5-
const decoder = new TextDecoder();
10+
import { doc } from "@deno/doc";
611

712
const excludes = [
813
"mod.ts",
@@ -11,47 +16,6 @@ const excludes = [
1116
"_*.ts",
1217
];
1318

14-
type DenoDocEntry = {
15-
name: string;
16-
location: {
17-
filename: string;
18-
};
19-
declarationKind: string;
20-
jsDoc: {
21-
doc: string;
22-
};
23-
kind: string;
24-
};
25-
26-
function isDenoDocEntry(x: unknown): x is DenoDocEntry {
27-
if (x == null || typeof x !== "object") return false;
28-
if (typeof (x as DenoDocEntry).name !== "string") return false;
29-
if (typeof (x as DenoDocEntry).location !== "object") return false;
30-
if (typeof (x as DenoDocEntry).location.filename !== "string") return false;
31-
if (typeof (x as DenoDocEntry).declarationKind !== "string") return false;
32-
if (typeof (x as DenoDocEntry).jsDoc !== "object") return false;
33-
if (typeof (x as DenoDocEntry).jsDoc.doc !== "string") return false;
34-
if (typeof (x as DenoDocEntry).kind !== "string") return false;
35-
return true;
36-
}
37-
38-
async function listDenoDocEntries(path: string): Promise<DenoDocEntry[]> {
39-
const cmd = new Deno.Command(Deno.execPath(), {
40-
args: ["doc", "--json", path],
41-
stdout: "piped",
42-
stderr: "piped",
43-
});
44-
const { success, stdout, stderr } = await cmd.output();
45-
if (!success) {
46-
throw new Error(decoder.decode(stderr));
47-
}
48-
const json = JSON.parse(decoder.decode(stdout));
49-
if (!Array.isArray(json)) {
50-
throw new Error(`Expected array but got ${JSON.stringify(json)}`);
51-
}
52-
return json.filter(isDenoDocEntry);
53-
}
54-
5519
async function* iterModules(path: string): AsyncIterable<string> {
5620
const patterns = excludes.map((p) => globToRegExp(p));
5721
for await (const entry of Deno.readDir(path)) {
@@ -66,15 +30,16 @@ async function generateModTs(
6630
): Promise<void> {
6731
const path = fromFileUrl(import.meta.resolve(`../${namespace}/`));
6832
const exports = (await Array.fromAsync(
69-
flatMap(iterModules(path), (x) => listDenoDocEntries(x)),
33+
flatMap(iterModules(path), (x) => doc(toFileUrl(x).href)),
7034
))
35+
.filter((x) => !!x.jsDoc?.doc)
7136
.filter((x) => x.kind === "function")
7237
.filter((x) => x.declarationKind === "export")
7338
.filter((x) => x.name.startsWith(namespace))
7439
.map((x) => ({
7540
path: relative(path, fromFileUrl(x.location.filename)),
7641
name: x.name,
77-
doc: x.jsDoc.doc,
42+
doc: x.jsDoc!.doc!,
7843
}))
7944
.toSorted((a, b) => a.name.localeCompare(b.name));
8045
const lines = [

deno.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@core/iterutil": "jsr:@core/iterutil@^0.3.0",
7676
"@core/unknownutil": "./mod.ts",
7777
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
78+
"@deno/doc": "jsr:@deno/doc@^0.153.0",
7879
"@std/assert": "jsr:@std/assert@^1.0.4",
7980
"@std/jsonc": "jsr:@std/jsonc@^1.0.0",
8081
"@std/path": "jsr:@std/path@^1.0.2",
@@ -85,7 +86,7 @@
8586
"test": "deno test -A --doc --parallel --shuffle",
8687
"test:coverage": "deno task test --coverage=.coverage",
8788
"coverage": "deno coverage .coverage",
88-
"gen": "deno run --allow-run=deno --allow-read --allow-write=. .scripts/gen-mod.ts",
89+
"gen": "deno run --allow-net=jsr.io --allow-env --allow-read --allow-write=. .scripts/gen-mod.ts",
8990
"update": "deno run --allow-env --allow-read --allow-write=. --allow-run=git,deno --allow-net=jsr.io,registry.npmjs.org jsr:@molt/cli ./*.ts",
9091
"update:commit": "deno task -q update --commit --prefix deps: --pre-commit=fmt,lint"
9192
}

0 commit comments

Comments
 (0)