Repository navigation
Expand file tree
/
Copy pathprepare-release-version.cjs
More file actions
186 lines (166 loc) · 5.85 KB
/
Copy pathprepare-release-version.cjs
File metadata and controls
186 lines (166 loc) · 5.85 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
175
176
177
178
179
180
181
182
183
184
185
186
const { execFileSync } = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const root = path.resolve(__dirname, "..");
const releaseFiles = [
"integrations/scala/README.md",
"integrations/scala/RELEASE_NOTES.md",
"integrations/dart/pubspec.yaml",
"integrations/gleam/examples/basic_app/manifest.toml",
"integrations/gleam/gleam.toml",
"integrations/closure/clojurescript/pom.xml",
"integrations/closure/clojurescript/README.md",
"integrations/closure/clojurescript/bin/pom.xml",
"integrations/closure/clojurescript/bin/README.md",
"integrations/closure/java/pom.xml",
"integrations/closure/java/README.md",
"integrations/closure/java/demo/pom.xml",
"docs/content/docs/integrations/java-j2cl.md",
"docs/content/docs/integrations/scala.md",
"docs/content/docs/integrations/clojurescript.md",
];
function nextVersion(current, release) {
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(current);
if (!match) throw new Error(`Invalid current version '${current}'.`);
const [, majorText, minorText, patchText] = match;
const major = Number(majorText);
const minor = Number(minorText);
const patch = Number(patchText);
if (release === "major") return `${major + 1}.0.0`;
if (release === "minor") return `${major}.${minor + 1}.0`;
if (release === "patch") return `${major}.${minor}.${patch + 1}`;
throw new Error(
`Release must be major, minor, or patch; received '${release}'.`,
);
}
function latestReleaseVersion(refs) {
const versions = [...refs.matchAll(/refs\/tags\/v(\d+\.\d+\.\d+)$/gmu)].map(
([, version]) => version,
);
versions.sort((left, right) => {
const leftParts = left.split(".").map(Number);
const rightParts = right.split(".").map(Number);
for (let index = 0; index < leftParts.length; index += 1) {
const difference = leftParts[index] - rightParts[index];
if (difference !== 0) return difference;
}
return 0;
});
return versions.at(-1);
}
function assertReleaseBaseVersion(current, refs) {
const latest = latestReleaseVersion(refs);
if (latest !== undefined && current !== latest) {
throw new Error(
`Current version ${current} does not match latest release tag v${latest}. ` +
"Synchronize with the release repository before preparing a release.",
);
}
}
function readRemoteReleaseTags(repository, run = execFileSync) {
if (typeof repository !== "string" || repository.length === 0) {
throw new Error("package.json must declare a release repository URL.");
}
try {
return run("git", ["ls-remote", "--tags", "--refs", repository, "v*"], {
cwd: root,
encoding: "utf8",
env: {
...process.env,
GIT_TERMINAL_PROMPT: "0",
},
timeout: 15_000,
});
} catch {
throw new Error(
"Unable to read tags from the release repository. Check network access and package.json.",
);
}
}
function promoteChangelog(source, version, date) {
const pattern = /^## \[Unreleased\]\s*\n([\s\S]*?)(?=^## \[)/m;
const match = pattern.exec(source);
if (!match)
throw new Error("CHANGELOG.md must contain an Unreleased section.");
const notes = match[1].trim();
if (!notes)
throw new Error("CHANGELOG.md Unreleased section must not be empty.");
return source.replace(
pattern,
`## [Unreleased]\n\n## [${version}] - ${date}\n\n${notes}\n\n`,
);
}
function promoteIntegrationChangelog(source, version) {
const heading = `## ${version}`;
if (source.includes(heading)) {
throw new Error(`Integration changelog already contains ${version}.`);
}
const title = source.startsWith("# Changelog\n") ? "# Changelog\n\n" : "";
const history = title ? source.slice(title.length) : source;
return `${title}${heading}\n\n- Updated bindings for AngularTS ${version}.\n\n${history}`;
}
function read(relativePath) {
return fs.readFileSync(path.join(root, relativePath), "utf8");
}
function prepare(release) {
const packageJson = JSON.parse(read("package.json"));
const packageLock = JSON.parse(read("package-lock.json"));
const current = packageJson.version;
const remoteTags = readRemoteReleaseTags(packageJson.repository?.url);
assertReleaseBaseVersion(current, remoteTags);
const version = nextVersion(current, release);
const date = new Date().toISOString().slice(0, 10);
const updates = new Map();
packageJson.version = version;
packageLock.version = version;
if (!packageLock.packages?.[""]) {
throw new Error("package-lock.json is missing its root package entry.");
}
packageLock.packages[""].version = version;
updates.set("package.json", `${JSON.stringify(packageJson, null, 2)}\n`);
updates.set("package-lock.json", `${JSON.stringify(packageLock, null, 2)}\n`);
updates.set(
"CHANGELOG.md",
promoteChangelog(read("CHANGELOG.md"), version, date),
);
for (const relativePath of [
"integrations/dart/CHANGELOG.md",
"integrations/gleam/CHANGELOG.md",
]) {
updates.set(
relativePath,
promoteIntegrationChangelog(read(relativePath), version),
);
}
for (const relativePath of releaseFiles) {
const source = read(relativePath);
if (!source.includes(current)) {
throw new Error(
`${relativePath} does not contain current version ${current}.`,
);
}
updates.set(relativePath, source.replaceAll(current, version));
}
for (const [relativePath, source] of updates) {
fs.writeFileSync(path.join(root, relativePath), source);
}
console.log(
`Prepared AngularTS ${version} (${release}) across ${updates.size} files.`,
);
}
module.exports = {
assertReleaseBaseVersion,
latestReleaseVersion,
nextVersion,
promoteChangelog,
promoteIntegrationChangelog,
readRemoteReleaseTags,
};
if (require.main === module) {
try {
prepare(process.argv[2]);
} catch (error) {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
}
}