-
Notifications
You must be signed in to change notification settings - Fork 200
/
publish-packages.ts
149 lines (136 loc) · 4.49 KB
/
publish-packages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as yargs from "yargs";
import { defaultLocalOptions } from "./lib/common";
import { publishNotNeededPackage, publishTypingsPackage } from "./lib/package-publisher";
import { getDefinitelyTyped, AllPackages } from "@definitelytyped/definitions-parser";
import {
loggerWithErrors,
logUncaughtErrors,
logger,
Fetcher,
writeLog,
NpmPublishClient,
} from "@definitelytyped/utils";
import { readChangedPackages, ChangedPackages } from "./lib/versions";
import { skipBadPublishes } from "./lib/npm";
import { getSecret, Secret } from "./lib/secrets";
if (!module.parent) {
const dry = !!yargs.argv.dry;
logUncaughtErrors(async () => {
const dt = await getDefinitelyTyped(defaultLocalOptions, loggerWithErrors()[0]);
await publishPackages(
await readChangedPackages(await AllPackages.read(dt)),
dry,
process.env.GH_API_TOKEN || "",
new Fetcher()
);
});
}
export default async function publishPackages(
changedPackages: ChangedPackages,
dry: boolean,
githubAccessToken: string,
fetcher: Fetcher
): Promise<void> {
const [log, logResult] = logger();
if (dry) {
log("=== DRY RUN ===");
} else {
log("=== Publishing packages ===");
}
const client = await NpmPublishClient.create(await getSecret(Secret.NPM_TOKEN), undefined);
for (const cp of changedPackages.changedTypings) {
log(`Publishing ${cp.pkg.desc}...`);
await publishTypingsPackage(client, cp, dry, log);
const commits = (await queryGithub(
`repos/DefinitelyTyped/DefinitelyTyped/commits?path=types%2f${cp.pkg.subDirectoryPath}`,
githubAccessToken,
fetcher
)) as {
sha: string;
commit: {
message: string;
author: {
date: string;
};
};
}[];
const firstCommit = commits[0];
if (firstCommit && !firstCommit.commit.message.includes("#no-publishing-comment")) {
log("Found related commits; hash: " + commits[0].sha);
const prs = (await queryGithub(
`search/issues?q=is:pr%20is:merged%20${commits[0].sha}`,
githubAccessToken,
fetcher
)) as { items: { number: number }[] };
let latestPr = 0;
for (const pr of prs.items) {
if (pr.number > latestPr) {
latestPr = pr.number;
}
}
log("Latest PR: " + latestPr);
if (latestPr === 0) {
continue;
}
const latest = (await queryGithub(
`repos/DefinitelyTyped/DefinitelyTyped/pulls/${latestPr}`,
githubAccessToken,
fetcher
)) as { merged_at: string };
log("Current date is " + new Date(Date.now()).toString());
log(" Merge date is " + new Date(latest.merged_at).toString());
const published = cp.pkg.fullNpmName + "@" + cp.version;
const publishNotification =
"I just published [`" + published + "` to npm](https://www.npmjs.com/package/" + cp.pkg.fullNpmName + ").";
log(publishNotification);
if (dry) {
log("(dry) Skip publishing notification to github.");
} else {
const commented = await postGithub(
`repos/DefinitelyTyped/DefinitelyTyped/issues/${latestPr}/comments`,
{ body: publishNotification },
githubAccessToken,
fetcher
);
log("From github: " + JSON.stringify(commented).slice(0, 200));
}
}
}
for (const n of changedPackages.changedNotNeededPackages) {
const target = await skipBadPublishes(n, log);
await publishNotNeededPackage(client, target, dry, log);
}
await writeLog("publishing.md", logResult());
console.log("Done!");
}
async function postGithub(path: string, data: any, githubToken: string, fetcher: Fetcher) {
const [log] = logger();
const body = JSON.stringify(data);
log(`Posting to github at ${path}: ${body}`);
return fetcher.fetchJson({
hostname: "api.github.com",
method: "POST",
path,
body,
headers: {
// arbitrary string, but something must be provided
"User-Agent": "types-publisher",
"Content-Type": "application/json",
Authorization: "token " + githubToken,
"Content-Length": Buffer.byteLength(body),
},
});
}
async function queryGithub(path: string, githubToken: string, fetcher: Fetcher) {
const [log] = logger();
log("Requesting from github: " + path);
return fetcher.fetchJson({
hostname: "api.github.com",
method: "GET",
path: path + "&access_token=" + githubToken,
headers: {
// arbitrary string, but something must be provided
"User-Agent": "types-publisher",
},
});
}