Skip to content

Commit

Permalink
Merge pull request #135 from jsr-core/chore-gen
Browse files Browse the repository at this point in the history
chore: refine `gen-mod.ts`
  • Loading branch information
lambdalisue authored Oct 16, 2024
2 parents 747a1ee + 727fcc0 commit e377132
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 48 deletions.
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

0 comments on commit e377132

Please sign in to comment.