forked from gitkraken/vscode-gitlens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep-release.js
76 lines (61 loc) · 2.43 KB
/
prep-release.js
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
const { exec } = require('child_process');
const fs = require('fs');
const readline = require('readline');
const path = require('path');
const versionRegex = /^\d{1,4}\.\d{1,4}\.\d{1,4}$/;
const changelogPath = path.join(__dirname, '..', 'CHANGELOG.md');
console.log(changelogPath);
let data = fs.readFileSync(changelogPath, 'utf8');
// Find the current version number
const match = /\[unreleased\]: https:\/\/github\.com\/gitkraken\/vscode-gitlens\/compare\/v(.+)\.\.\.HEAD/.exec(data);
const currentVersion = match?.[1];
if (currentVersion == null || versionRegex.test(currentVersion) === false) {
console.error('Unable to find current version number.');
return;
}
// Create readline interface for getting input from user
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Ask for new version number
rl.question(`Enter the new version number (format x.x.x, current is ${currentVersion}): `, function (version) {
// Validate the version input
if (!versionRegex.test(version)) {
console.error(
'Invalid version number. Please use the format x.y.z where x, y, and z are positive numbers no greater than 4 digits.',
);
rl.close();
return;
}
// Get today's date
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
const dd = String(today.getDate()).padStart(2, '0');
const newVersionHeader = `## [Unreleased]\n\n## [${version}] - ${yyyy}-${mm}-${dd}`;
const newVersionLink = `[${version}]: https://github.com/gitkraken/vscode-gitlens/compare/v${currentVersion}...gitkraken:v${version}`;
// Add the new version header below the ## [Unreleased] header
data = data.replace('## [Unreleased]', newVersionHeader);
const unreleasedLink = match[0].replace(/\/compare\/v(.+?)\.\.\.HEAD/, `/compare/v${version}...HEAD`);
// Update the [unreleased]: line
data = data.replace(match[0], `${unreleasedLink}\n${newVersionLink}`);
// Writing the updated version data to CHANGELOG
fs.writeFileSync(changelogPath, data);
// Stage CHANGELOG
exec('git add CHANGELOG.md', err => {
if (err) {
console.error(`Unable to stage CHANGELOG.md: ${err}`);
return;
}
// Call 'yarn version' to commit and create the tag
exec(`yarn version --new-version ${version}`, err => {
if (err) {
console.error(`'yarn version' failed: ${err}`);
return;
}
console.log(`${version} is ready for release.`);
});
});
rl.close();
});