forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-release.js
More file actions
executable file
·76 lines (66 loc) · 2.59 KB
/
Copy pathprepare-release.js
File metadata and controls
executable file
·76 lines (66 loc) · 2.59 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
#!/usr/bin/env node
/**
* Prepare for X.Y.Z release: set the `nextBump` field in
* common/config/rush/version-policies.json so that Rush will bump from the
* current version to the target version on the next publish.
*
* Usage: node .github/scripts/prepare-release.js <X.Y.Z>
*
* The file uses JSON-with-comments, so we don't JSON.parse it — we read the
* current `version` with a regex and rewrite the `nextBump` line in place.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const target = process.argv[2];
if (!target || !/^\d+\.\d+\.\d+$/.test(target)) {
console.error(`Usage: ${process.argv[1]} <X.Y.Z>`);
process.exit(2);
}
const repoRoot = path.resolve(__dirname, '..', '..');
const policyPath = path.join(repoRoot, 'common/config/rush/version-policies.json');
const original = fs.readFileSync(policyPath, 'utf8');
const versionMatch = original.match(/"version"\s*:\s*"(\d+\.\d+\.\d+)"/);
if (!versionMatch) {
console.error('Could not find "version" field in version-policies.json');
process.exit(1);
}
const current = versionMatch[1];
console.log(`Current version: ${current}`);
console.log(`Target version: ${target}`);
if (current === target) {
console.error(`Current version is already ${target}; nothing to do.`);
process.exit(1);
}
const [cMaj, cMin, cPatch] = current.split('.').map(Number);
const [tMaj, tMin, tPatch] = target.split('.').map(Number);
let nextBump;
if (tMaj > cMaj && tMin === 0 && tPatch === 0) {
nextBump = 'major';
} else if (tMaj === cMaj && tMin > cMin && tPatch === 0) {
nextBump = 'minor';
} else if (tMaj === cMaj && tMin === cMin && tPatch > cPatch) {
nextBump = 'patch';
} else {
console.error(
`Target ${target} is not a valid next semver bump from ${current}. ` +
`Expected one of: ${cMaj}.${cMin}.${cPatch + 1} (patch), ` +
`${cMaj}.${cMin + 1}.0 (minor), or ${cMaj + 1}.0.0 (major).`
);
process.exit(1);
}
console.log(`Computed nextBump: ${nextBump}`);
// Rewrite the "nextBump" line in place. Preserve indentation and trailing
// whitespace/newline exactly so the diff is the single intended change.
const nextBumpRegex = /(^\s*"nextBump"\s*:\s*")(prerelease|release|patch|minor|major)(")/m;
if (!nextBumpRegex.test(original)) {
console.error('Could not find "nextBump" field in version-policies.json');
process.exit(1);
}
const updated = original.replace(nextBumpRegex, `$1${nextBump}$3`);
if (updated === original) {
console.log(`nextBump is already "${nextBump}"; no change needed.`);
process.exit(0);
}
fs.writeFileSync(policyPath, updated);
console.log(`Updated nextBump to "${nextBump}" in ${policyPath}`);