|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-check |
| 3 | +// Purpose: Download repos known to use TypeDoc's APIs for custom plugins/themes |
| 4 | +// which are NOT published to NPM. This is used to check potentially-breaking |
| 5 | +// changes which are likely not actually breaking anyone, so could be included |
| 6 | +// in a patch release with little risk. |
| 7 | + |
| 8 | +import cp from "node:child_process"; |
| 9 | +import fs from "node:fs/promises"; |
| 10 | +import { PQueue } from "./capture_screenshots.mjs"; |
| 11 | +import { ALL_API_USERS, API_USERS, OLD_API_USERS } from "./data/api_users.js"; |
| 12 | +import { parseArgs } from "node:util"; |
| 13 | +import { existsSync, readFileSync } from "node:fs"; |
| 14 | +import semver from "semver"; |
| 15 | + |
| 16 | +if (import.meta.url.endsWith(process.argv[1])) { |
| 17 | + const args = parseArgs({ |
| 18 | + options: { |
| 19 | + jobs: { |
| 20 | + short: "j", |
| 21 | + type: "string", |
| 22 | + default: "6", |
| 23 | + }, |
| 24 | + rebuild: { |
| 25 | + type: "boolean", |
| 26 | + default: false, |
| 27 | + }, |
| 28 | + all: { |
| 29 | + type: "boolean", |
| 30 | + default: false, |
| 31 | + }, |
| 32 | + output: { |
| 33 | + short: "o", |
| 34 | + type: "string", |
| 35 | + default: "../typedoc_api_users", |
| 36 | + }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + const rebuild = args.values.rebuild; |
| 41 | + const outDir = args.values.output; |
| 42 | + const jobs = parseInt(args.values.jobs) || 1; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param {string[]} args |
| 46 | + * @returns {Promise<void>} |
| 47 | + */ |
| 48 | + const git = (...args) => |
| 49 | + new Promise((resolve, reject) => { |
| 50 | + const child = cp.spawn("git", args, { cwd: outDir, stdio: "inherit" }); |
| 51 | + child.on("close", () => { |
| 52 | + if (child.exitCode) reject(new Error(`When executing git ${args.join(" ")}`)); |
| 53 | + resolve(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + const start = Date.now(); |
| 58 | + |
| 59 | + if (rebuild) { |
| 60 | + await fs.rm(outDir, { recursive: true, force: true }); |
| 61 | + } |
| 62 | + await fs.mkdir(outDir, { recursive: true }); |
| 63 | + |
| 64 | + const q = new PQueue(jobs); |
| 65 | + |
| 66 | + for (const { repo, branch, pkg = "package.json", filter } of args.values.all ? ALL_API_USERS : API_USERS) { |
| 67 | + q.add(async () => { |
| 68 | + const repoDir = `${repo.replace("/", "_")}`; |
| 69 | + if (!existsSync(`${outDir}/${repoDir}`)) { |
| 70 | + await git( |
| 71 | + "clone", |
| 72 | + "--quiet", |
| 73 | + "--filter=blob:none", |
| 74 | + "--no-checkout", |
| 75 | + "--depth=1", |
| 76 | + `git@github.com:${repo}.git`, |
| 77 | + repoDir, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + await git("-C", repoDir, "fetch", "--quiet", "--depth=1", "origin", branch); |
| 82 | + |
| 83 | + const filterArg = Array.isArray(filter) ? filter : [filter]; |
| 84 | + await git("-C", repoDir, "checkout", "--quiet", branch, "--", pkg, ...filterArg); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + await q.run(); |
| 90 | + } catch (error) { |
| 91 | + console.error(error); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + console.log(`Cloning/updating took ${(Date.now() - start) / 1000} seconds`); |
| 96 | + |
| 97 | + // Check for repos listed in the wrong list |
| 98 | + const currentMinor = semver.parse(JSON.parse(readFileSync("package.json", "utf-8")).version)?.minor; |
| 99 | + console.log("Current minor is", currentMinor); |
| 100 | + |
| 101 | + for (const { repo, pkg = "package.json" } of API_USERS) { |
| 102 | + const repoDir = `${repo.replace("/", "_")}`; |
| 103 | + const manifest = JSON.parse(readFileSync(`${outDir}/${repoDir}/${pkg}`, "utf-8")); |
| 104 | + const depVersion = manifest.devDependencies?.typedoc || manifest.dependencies?.typedoc || |
| 105 | + "(missing dependency)"; |
| 106 | + const depMinor = semver.parse(depVersion.replace(/^[\^<=~]+/, ""))?.minor; |
| 107 | + |
| 108 | + if (depMinor !== currentMinor) { |
| 109 | + console.log(`API->OLD ${repo} ${depVersion}`); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (args.values.all) { |
| 114 | + for (const { repo, pkg = "package.json" } of OLD_API_USERS) { |
| 115 | + const repoDir = `${repo.replace("/", "_")}`; |
| 116 | + const manifest = JSON.parse(readFileSync(`${outDir}/${repoDir}/${pkg}`, "utf-8")); |
| 117 | + const depVersion = manifest.devDependencies?.typedoc || manifest.dependencies?.typedoc; |
| 118 | + if (!depVersion) { |
| 119 | + console.log(`OLD->DEL ${repo} (missing dependency)`); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + const depMinor = semver.parse(depVersion.replace(/^[\^<=~]+/, ""))?.minor; |
| 124 | + |
| 125 | + if (depMinor === currentMinor) { |
| 126 | + console.log(`OLD->API ${repo} ${depVersion}`); |
| 127 | + } else if (depMinor !== (currentMinor || 0) - 1) { |
| 128 | + console.log(`OLD->DEL ${repo} ${depVersion}`); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments