Skip to content

Commit

Permalink
feat(bundle): jsr mirroring support for forced types in patches
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Oct 16, 2024
1 parent 82a9730 commit ba33a3e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 9 additions & 1 deletion bundle/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions bundle/ts/mirror/jsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function mirror(
registryApi?: string
cwd?: string
logger?: Logger
patches?: { inject?: record<string>; aliases?: { [key: string]: { [key: string]: Nullable<string> } } }
patches?: { inject?: record<string>; types?: { [key: string]: boolean }; aliases?: { [key: string]: { [key: string]: Nullable<string> } } }
},
): Promise<{ files: record<string>; exports: record<string> }> {
// List packages
Expand Down Expand Up @@ -103,7 +103,7 @@ export async function mirror(
])
} // Handle regular exports
else {
const symbols = await generate(jsr)
const symbols = await generate(jsr, { path: paths.b, patches })
output.files[path] = [...symbols.values()].map(({ content }) => content).join("\n")
if (patches?.inject?.[paths.b]) {
output.files[path] = `${patches.inject[paths.b]}\n${output.files[path]}`
Expand Down Expand Up @@ -169,13 +169,18 @@ export async function mirror(
* This is computed by extracting `deno doc` information and re-attaching documentation and type information.
* This is to ensure that it is properly documented once published back on the registry.
*/
async function generate(jsr: string) {
async function generate(jsr: string, { path, patches }: { path: string; patches?: { types?: { [key: string]: boolean } } }) {
const temp = await Deno.makeTempFile()
const symbols = new Map<string, { type: boolean; content: string }>()
try {
await Deno.writeTextFile(temp, `export * from "${jsr}"\n`)
const ast = await parseDoc(temp)
for (const { kind, name, jsDoc, ..._ } of ast) {
for (let { kind, name, jsDoc, ..._ } of ast) {
let forced = false
if (patches?.types?.[`${path}#${name}`]) {
kind = "typeAlias"
forced = true
}
const { stdout } = await command("deno", ["doc", "--filter", name, temp])
const signature = stripAnsiCode(stdout).split("\n")[2]
const type = ["interface", "typeAlias"].includes(kind)
Expand Down Expand Up @@ -206,7 +211,7 @@ async function generate(jsr: string) {
break
}
}
if ((!type) || (type && (!ast.some((node: { name: string; kind: string }) => node.name === name && node.kind !== kind)))) {
if ((!type) || forced || (type && (!ast.some((node: { name: string; kind: string }) => node.name === name && node.kind !== kind)))) {
lines.push(`export ${type ? "type " : ""}{ ${name} }`, "")
}
symbols.set(name, { type, content: lines.join("\n") })
Expand Down

0 comments on commit ba33a3e

Please sign in to comment.