|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import * as process from "process"; |
| 4 | +import { AllPackages, getDefinitelyTyped } from "@definitelytyped/definitions-parser"; |
| 5 | +import { NpmPublishClient } from "@definitelytyped/utils"; |
| 6 | +import { graphql } from "@octokit/graphql"; |
| 7 | +import search from "libnpmsearch"; |
| 8 | +import yargs from "yargs"; |
| 9 | + |
| 10 | +main(); |
| 11 | + |
| 12 | +async function main() { |
| 13 | + const { dryRun } = yargs.argv; |
| 14 | + const options = { definitelyTypedPath: undefined, progress: false, parseInParallel: false }; |
| 15 | + const dt = await getDefinitelyTyped(options, console); |
| 16 | + const allPackages = await AllPackages.read(dt); |
| 17 | + const client = await NpmPublishClient.create(process.env.NPM_TOKEN!); |
| 18 | + // Loop over npm @types packages and mark as deprecated any that no longer exist in the DT repo. |
| 19 | + let from = 0; |
| 20 | + let results; |
| 21 | + do { |
| 22 | + const opts = { limit: 250, from }; |
| 23 | + // Won't return already-deprecated packages. |
| 24 | + results = await search("@types", opts); |
| 25 | + for (const result of results) { |
| 26 | + const types = result.name.slice("@types/".length); |
| 27 | + // Skip ones that exist, either in the types/ directory or notNeededPackages.json. |
| 28 | + if (allPackages.tryGetLatestVersion(types) || allPackages.getNotNeededPackage(types)) continue; |
| 29 | + const msg = await fetchMsg(types); |
| 30 | + if (!msg) { |
| 31 | + console.log(`Could not find the commit that removed types/${types}/.`); |
| 32 | + continue; |
| 33 | + } |
| 34 | + console.log(`Deprecating ${result.name}: ${msg}`); |
| 35 | + if (!dryRun) await client.deprecate(result.name, "*", msg); |
| 36 | + } |
| 37 | + from += results.length; |
| 38 | + // The registry API clamps limit at 250 and from at 5,000, so we can only loop over 5,250 packages, for now. |
| 39 | + } while (results.length >= 250 && from <= 5000); |
| 40 | +} |
| 41 | + |
| 42 | +/** Reference the commit/PR that removed the named types. */ |
| 43 | +async function fetchMsg(types: string) { |
| 44 | + const { |
| 45 | + repository: { |
| 46 | + defaultBranchRef: { |
| 47 | + target: { |
| 48 | + history: { |
| 49 | + nodes: [commit], |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } = await graphql( |
| 55 | + ` |
| 56 | + query ($path: String!) { |
| 57 | + repository(name: "DefinitelyTyped", owner: "DefinitelyTyped") { |
| 58 | + defaultBranchRef { |
| 59 | + target { |
| 60 | + ... on Commit { |
| 61 | + history(first: 1, path: $path) { |
| 62 | + nodes { |
| 63 | + associatedPullRequests(first: 1) { |
| 64 | + nodes { |
| 65 | + url |
| 66 | + } |
| 67 | + } |
| 68 | + messageHeadline |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + `, |
| 77 | + { |
| 78 | + headers: { authorization: `token ${process.env.GITHUB_TOKEN}` }, |
| 79 | + path: `types/${types}/`, |
| 80 | + } |
| 81 | + ); |
| 82 | + if (!commit) return; |
| 83 | + const { |
| 84 | + associatedPullRequests: { |
| 85 | + nodes: [pullRequest], |
| 86 | + }, |
| 87 | + messageHeadline, |
| 88 | + } = commit; |
| 89 | + const subject = messageHeadline.replace(new RegExp(String.raw`^\[${types}] `), "").replace(/ \(#[0-9]+\)$/, ""); |
| 90 | + return pullRequest ? `${subject} ${pullRequest.url}` : subject; |
| 91 | +} |
0 commit comments