|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Import docs from existing release tags. |
| 4 | + * Checks out each release tag, builds docs for the corresponding package, |
| 5 | + * then returns to the original branch. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * bun scripts/import-release-docs.ts |
| 9 | + * |
| 10 | + * This is a one-time migration script. Run it to populate versioned docs |
| 11 | + * from existing release history. |
| 12 | + */ |
| 13 | + |
| 14 | +import { $ } from "bun"; |
| 15 | +import path from "path"; |
| 16 | +import fs from "fs"; |
| 17 | + |
| 18 | +const ROOT = path.resolve(import.meta.dir, ".."); |
| 19 | + |
| 20 | +// Package name mapping: tag prefix → directory name |
| 21 | +const PKG_MAP: Record<string, string> = { |
| 22 | + "@wdprlib/ast": "ast", |
| 23 | + "@wdprlib/parser": "parser", |
| 24 | + "@wdprlib/render": "render", |
| 25 | + "@wdprlib/runtime": "runtime", |
| 26 | +}; |
| 27 | + |
| 28 | +// Get current branch to return to |
| 29 | +const currentBranch = (await $`git rev-parse --abbrev-ref HEAD`.cwd(ROOT).text()).trim(); |
| 30 | +console.log(`Current branch: ${currentBranch}\n`); |
| 31 | + |
| 32 | +// Get all tags |
| 33 | +const tagsRaw = (await $`git tag -l`.cwd(ROOT).text()).trim(); |
| 34 | +const tags = tagsRaw.split("\n").filter(Boolean); |
| 35 | + |
| 36 | +// Parse tags into package+version pairs |
| 37 | +const releases: { tag: string; pkg: string; version: string }[] = []; |
| 38 | +for (const tag of tags) { |
| 39 | + for (const [prefix, dir] of Object.entries(PKG_MAP)) { |
| 40 | + if (tag.startsWith(`${prefix}@`)) { |
| 41 | + const version = tag.slice(prefix.length + 1); |
| 42 | + releases.push({ tag, pkg: dir, version }); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Sort by package then version |
| 48 | +releases.sort((a, b) => a.pkg.localeCompare(b.pkg) || a.version.localeCompare(b.version)); |
| 49 | + |
| 50 | +console.log(`Found ${releases.length} release tags:\n`); |
| 51 | +for (const r of releases) { |
| 52 | + console.log(` ${r.tag} → ${r.pkg}@${r.version}`); |
| 53 | +} |
| 54 | +console.log(); |
| 55 | + |
| 56 | +const results: { tag: string; status: string }[] = []; |
| 57 | + |
| 58 | +for (const { tag, pkg, version } of releases) { |
| 59 | + console.log(`--- ${tag} ---`); |
| 60 | + |
| 61 | + // Checkout the tag |
| 62 | + const checkout = await $`git checkout ${tag} --force`.cwd(ROOT).nothrow().quiet(); |
| 63 | + if (checkout.exitCode !== 0) { |
| 64 | + console.error(` Failed to checkout ${tag}`); |
| 65 | + results.push({ tag, status: "checkout failed" }); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + // Install dependencies (might differ per tag) |
| 70 | + const install = await $`bun install --frozen-lockfile`.cwd(ROOT).nothrow().quiet(); |
| 71 | + if (install.exitCode !== 0) { |
| 72 | + // Try without frozen lockfile |
| 73 | + await $`bun install`.cwd(ROOT).nothrow().quiet(); |
| 74 | + } |
| 75 | + |
| 76 | + const pkgDir = path.join(ROOT, "packages", pkg); |
| 77 | + |
| 78 | + // Check if source exists at this tag |
| 79 | + if (!fs.existsSync(path.join(pkgDir, "src"))) { |
| 80 | + console.log(` Skipping: packages/${pkg}/src not found at ${tag}`); |
| 81 | + results.push({ tag, status: "no src" }); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + // Write the monorepo config (it may not exist at this tag) |
| 86 | + const configContent = JSON.stringify( |
| 87 | + { |
| 88 | + $schema: "https://typedoc.org/schema.json", |
| 89 | + entryPoints: ["src/**/*.ts"], |
| 90 | + tsconfig: fs.existsSync(path.join(pkgDir, "tsconfig.typedoc.json")) |
| 91 | + ? "tsconfig.typedoc.json" |
| 92 | + : "tsconfig.json", |
| 93 | + plugin: ["@r74tech/typedoc-plugin-monorepo-versions"], |
| 94 | + validation: { |
| 95 | + notExported: false, |
| 96 | + }, |
| 97 | + versions: { |
| 98 | + stable: "auto", |
| 99 | + dev: "auto", |
| 100 | + packageFile: "package.json", |
| 101 | + makeRelativeLinks: true, |
| 102 | + monorepo: { |
| 103 | + name: pkg, |
| 104 | + root: "../../docs", |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + null, |
| 109 | + "\t", |
| 110 | + ); |
| 111 | + const configPath = path.join(pkgDir, "typedoc.monorepo.json"); |
| 112 | + fs.writeFileSync(configPath, configContent); |
| 113 | + |
| 114 | + // Build docs |
| 115 | + const build = |
| 116 | + await $`bunx typedoc --options ${configPath}` |
| 117 | + .cwd(pkgDir) |
| 118 | + .nothrow(); |
| 119 | + |
| 120 | + if (build.exitCode !== 0) { |
| 121 | + console.error(` Build failed for ${tag}`); |
| 122 | + results.push({ tag, status: "build failed" }); |
| 123 | + } else { |
| 124 | + console.log(` Success: ${pkg}@${version}\n`); |
| 125 | + results.push({ tag, status: "ok" }); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Return to original branch |
| 130 | +await $`git checkout ${currentBranch} --force`.cwd(ROOT); |
| 131 | +await $`bun install`.cwd(ROOT).nothrow().quiet(); |
| 132 | + |
| 133 | +console.log("\n=== Results ==="); |
| 134 | +for (const { tag, status } of results) { |
| 135 | + const icon = status === "ok" ? "[OK]" : "[FAIL]"; |
| 136 | + console.log(` ${icon} ${tag}: ${status}`); |
| 137 | +} |
| 138 | + |
| 139 | +const ok = results.filter((r) => r.status === "ok").length; |
| 140 | +console.log(`\n${ok}/${results.length} tags imported successfully.`); |
0 commit comments