Repository navigation
Expand file tree
/
Copy pathrelease-utils.mjs
More file actions
174 lines (156 loc) · 5 KB
/
Copy pathrelease-utils.mjs
File metadata and controls
174 lines (156 loc) · 5 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { readFileSync } from "node:fs";
export const versionPattern =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/;
export function releaseDetails(manifest, requestedTag) {
const version = manifest.version;
if (!versionPattern.test(version)) {
throw new Error(
`package.json contains an invalid release version: ${version}`,
);
}
const tag = requestedTag ?? `v${version}`;
if (tag !== `v${version}`) {
throw new Error(
`Release tag ${tag} must exactly match package version v${version}`,
);
}
return {
name: manifest.name,
npmTag: version.includes("-") ? "next" : "latest",
prerelease: version.includes("-"),
tag,
version,
};
}
export function extractReleaseNotes(changelog, requestedVersion) {
const version = requestedVersion.startsWith("v")
? requestedVersion.slice(1)
: requestedVersion;
if (!versionPattern.test(version)) {
throw new Error(`Invalid release version: ${requestedVersion}`);
}
const heading = new RegExp(
`^## \\[${escapeRegExp(version)}\\] - \\d{4}-\\d{2}-\\d{2}\\s*$`,
"m",
);
const match = heading.exec(changelog);
if (!match) {
throw new Error(
`CHANGELOG.md must contain a "## [${version}] - YYYY-MM-DD" section`,
);
}
const remainder = changelog.slice(match.index + match[0].length);
const nextHeading = /^## /m.exec(remainder);
const notes = remainder
.slice(0, nextHeading ? nextHeading.index : remainder.length)
.trim();
if (!notes || !/\S/.test(notes.replace(/<!--[^]*?-->/g, ""))) {
throw new Error(`CHANGELOG.md release section ${version} is empty`);
}
return notes;
}
export function validateManifest(manifest) {
const requiredFiles = ["@types", "dist", "tokens", "README.md", "LICENSE"];
const files = new Set(manifest.files ?? []);
const missingFiles = requiredFiles.filter((file) => !files.has(file));
if (missingFiles.length) {
throw new Error(
`package.json files is missing: ${missingFiles.join(", ")}`,
);
}
if (manifest.publishConfig?.access !== "public") {
throw new Error("package.json publishConfig.access must be public");
}
if (manifest.publishConfig?.registry !== "https://registry.npmjs.org/") {
throw new Error("package.json must publish to the public npm registry");
}
const exports = manifest.exports ?? {};
for (const path of [
".",
"./angular.css",
"./customization-tokens",
"./package.json",
]) {
if (!(path in exports)) {
throw new Error(`package.json exports is missing ${path}`);
}
}
}
export function validatePack(pack, expected) {
if (pack.name !== expected.name || pack.version !== expected.version) {
throw new Error(
`Packed ${pack.name}@${pack.version}; expected ${expected.name}@${expected.version}`,
);
}
if (pack.warnings?.length) {
throw new Error(`npm pack warnings: ${pack.warnings.join("; ")}`);
}
const included = new Set((pack.files ?? []).map(({ path }) => path));
const required = [
"@types/index.d.ts",
"LICENSE",
"README.md",
"dist/angular.css",
"dist/angular-css.esm.js",
"dist/angular-css.umd.js",
"dist/angular-css.umd.min.js",
"package.json",
"tokens/angularcss.resolver.json",
"tokens/foundation/angularcss.tokens.json",
"tokens/themes/dark.tokens.json",
];
const missing = required.filter((path) => !included.has(path));
if (missing.length) {
throw new Error(`Packed artifact is missing: ${missing.join(", ")}`);
}
const unexpected = [...included].filter(
(path) =>
path.startsWith("src/") ||
path.startsWith("docs/") ||
path.startsWith("examples/") ||
path.startsWith("test-results/"),
);
if (unexpected.length) {
throw new Error(
`Packed artifact contains development files: ${unexpected.join(", ")}`,
);
}
}
export function validateRegistryPublication(metadata, pack, expected, npmTag) {
const published = metadata.versions?.[expected.version];
if (!published) {
throw new Error(
`npm has not exposed ${expected.name}@${expected.version} yet`,
);
}
if (
published.name !== expected.name ||
published.version !== expected.version
) {
throw new Error(
`npm exposed ${published.name}@${published.version}; expected ${expected.name}@${expected.version}`,
);
}
for (const field of ["integrity", "shasum"]) {
if (!pack[field]) {
throw new Error(`Local npm pack result is missing ${field}`);
}
if (published.dist?.[field] !== pack[field]) {
throw new Error(
`Published ${field} does not match the verified local tarball`,
);
}
}
const publishedTag = metadata["dist-tags"]?.[npmTag];
if (publishedTag !== expected.version) {
throw new Error(
`npm dist-tag ${npmTag} resolves to ${publishedTag ?? "nothing"}; expected ${expected.version}`,
);
}
}
export function readJson(path) {
return JSON.parse(readFileSync(path, "utf8"));
}
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}