|
1 | | -#!/usr/bin/env deno run --allow-read --allow-write |
| 1 | +#!/usr/bin/env deno run --allow-read --allow-write --allow-run |
| 2 | + |
| 3 | +const encoder = new TextEncoder(); |
| 4 | +const decoder = new TextDecoder(); |
| 5 | + |
| 6 | +interface Node { |
| 7 | + kind: string; |
| 8 | + name: string; |
| 9 | +} |
| 10 | + |
| 11 | +async function doc(fileName: string): Promise<Array<Node>> { |
| 12 | + const deno = Deno.run({ |
| 13 | + cmd: [Deno.execPath(), "doc", "--json", fileName], |
| 14 | + stdout: "piped", |
| 15 | + }); |
| 16 | + try { |
| 17 | + const out = await Deno.readAll(deno.stdout); |
| 18 | + return JSON.parse(decoder.decode(out)); |
| 19 | + } finally { |
| 20 | + deno.stdout.close(); |
| 21 | + deno.close(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function fmt(content: string): Promise<string> { |
| 26 | + const deno = Deno.run({ |
| 27 | + cmd: [Deno.execPath(), "fmt", "-"], |
| 28 | + stdin: "piped", |
| 29 | + stdout: "piped", |
| 30 | + }); |
| 31 | + try { |
| 32 | + await Deno.writeAll(deno.stdin, encoder.encode(content)); |
| 33 | + deno.stdin.close(); |
| 34 | + const formattedContent = decoder.decode(await Deno.readAll(deno.stdout)); |
| 35 | + return formattedContent; |
| 36 | + } finally { |
| 37 | + deno.stdout.close(); |
| 38 | + deno.close(); |
| 39 | + } |
| 40 | +} |
2 | 41 |
|
3 | 42 | const files = [...Deno.readDirSync(".")].filter((f) => { |
4 | 43 | const name = f.name; |
5 | 44 | if (!name || name === "mod.ts") return false; |
6 | 45 | return name.endsWith(".ts") && !name.endsWith("_test.ts") && |
7 | 46 | !name.endsWith(".d.ts"); |
8 | 47 | }).map((f) => f.name).sort(); |
9 | | -let content = "// Generated by tools/make_mod.ts. Don't edit.\n"; |
| 48 | +let content = `// Generated by tools/make_mod.ts. Don't edit.\n`; |
| 49 | + |
| 50 | +// Expose public functions from redis.ts. |
| 51 | +{ |
| 52 | + const fileName = "redis.ts"; |
| 53 | + const functions = (await doc(fileName)).filter((node) => { |
| 54 | + return node.kind === "function"; |
| 55 | + }).map((node) => node.name); |
| 56 | + content += `export { ${functions.join(",")} } from "./${fileName}";\n`; |
| 57 | +} |
| 58 | + |
| 59 | +// Expose public classes from errors.ts |
| 60 | +{ |
| 61 | + const fileName = "errors.ts"; |
| 62 | + const classes = (await doc(fileName)).filter((node) => { |
| 63 | + return node.kind === "class"; |
| 64 | + }).map((node) => node.name); |
| 65 | + content += `export { ${classes.join(",")} } from "./${fileName}";\n`; |
| 66 | +} |
| 67 | + |
| 68 | +// Expose types from *.ts. |
10 | 69 | for (const f of files) { |
11 | | - content += `export * from "./${f}";\n`; |
| 70 | + const types = (await doc(f)).filter((node) => { |
| 71 | + return node.kind === "interface" || node.kind === "typeAlias"; |
| 72 | + }).map((node) => node.name); |
| 73 | + if (types.length > 0) { |
| 74 | + content += `export type { ${types.join(",")} } from "./${f}"\n`; |
| 75 | + } |
12 | 76 | } |
13 | | -Deno.writeFileSync("mod.ts", new TextEncoder().encode(content)); |
| 77 | +Deno.writeFileSync("mod.ts", encoder.encode(await fmt(content))); |
0 commit comments