/
release.js
168 lines (140 loc) · 5.01 KB
/
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
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
const fs = require('fs').promises;
const path = require('path');
const { execSync } = require('child_process');
const readline = require('readline');
const { program } = require('commander');
const PACKAGES_DIR = './packages';
const CHANGELOG_FILENAME = 'CHANGELOG.md';
const consoleInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askQuestion(query) {
return new Promise(resolve => {
consoleInterface.question(query, answer => {
resolve(answer);
consoleInterface.close();
});
});
}
async function fileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch (err) {
return false;
}
}
function incrementVersion(currentVersion, majorRelease, minorRelease) {
if (majorRelease || minorRelease) {
const [major, minor, patch] = currentVersion.match(/(\d+)\.(\d+)\.(\d+)/).slice(1);
if (majorRelease) {
// MAJOR
return `${parseInt(major, 10) + 1}.0.0`;
} else if (minorRelease) {
// MINOR
return `${major}.${parseInt(minor, 10) + 1}.0`;
}
} else if (currentVersion.includes('beta')) {
// BETA
const [major, minor, patch, beta] = currentVersion.match(/(\d+)\.(\d+)\.(\d+)-beta\.(\d+)/).slice(1);
const newBetaVersion = parseInt(beta, 10) + 1;
return `${major}.${minor}.${patch}-beta.${newBetaVersion}`;
} else {
// PATCH
const [major, minor, patch] = currentVersion.match(/(\d+)\.(\d+)\.(\d+)/).slice(1);
return `${major}.${minor}.${parseInt(patch, 10) + 1}`;
}
}
async function incrementVersionOfAllPackages(newVersion) {
const packageDirectories = await fs.readdir(PACKAGES_DIR);
for (const packageDir of packageDirectories) {
const packageJsonPath = path.join(PACKAGES_DIR, packageDir, 'package.json');
if (await fileExists(packageJsonPath)) {
const packageJsonRaw = await fs.readFile(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonRaw);
packageJson.version = newVersion;
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
await updateChangelog(path.join(PACKAGES_DIR, packageDir, CHANGELOG_FILENAME), newVersion);
}
}
}
async function updateChangelog(changelogPath, newVersion) {
if (!(await fileExists(changelogPath))) {
return;
}
const changelogContent = await fs.readFile(changelogPath, 'utf8');
const updatedChangelogContent = changelogContent.replace('# New Release', `# New Release\n\n# ${newVersion}`);
await fs.writeFile(changelogPath, updatedChangelogContent);
}
function commitChanges(newVersion) {
execSync(`git commit -a -m "New release ${newVersion}"`);
}
function createGitTag(newVersion) {
execSync(`git tag "v${newVersion}" -m "New release version v${newVersion}"`);
}
function pushChanges() {
execSync('git push --tags');
execSync('git push');
}
function getCurrentGitBranch() {
return execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' }).trim();
}
function revertPackagesChanges() {
return execSync('git checkout -- ./packages');
}
function checkForUncommittedChangesInPackages() {
const gitStatusOutput = execSync('git status --porcelain', { encoding: 'utf8' });
const modifiedFilesInPackages = gitStatusOutput
.split('\n')
.some(line => line.trim().startsWith('M') && line.includes('packages'));
if (modifiedFilesInPackages) {
console.error(
'There are uncommitted changes within the packages. Please commit or stash them before releasing.'
);
process.exit(1);
}
}
async function determineCurrentVersion() {
const firstPackageJsonPath = path.join(PACKAGES_DIR, 'cqse-commons', 'package.json');
const firstPackageJsonRaw = await fs.readFile(firstPackageJsonPath, 'utf8');
const firstPackageJson = JSON.parse(firstPackageJsonRaw);
return firstPackageJson.version;
}
function checkReleaseBranch() {
const currentBranch = getCurrentGitBranch();
if (currentBranch !== 'master' && currentBranch !== 'main') {
console.error(
`This script can only be run on the 'master' or 'main' branch. Current branch is '${currentBranch}'.`
);
process.exit(1);
}
}
(async function release() {
const currentVersion = await determineCurrentVersion();
program
.version(currentVersion)
.description('Teamscale JavaScript Profiler Release Helper')
.option('--major', 'Create a major release')
.option('--minor', 'Create a minor release')
.parse();
const options = program.opts();
if (options.major && options.minor) {
console.error("Error: You can't specify both --major and --minor. Choose one.");
process.exit(1);
}
checkForUncommittedChangesInPackages();
checkReleaseBranch();
const newVersion = incrementVersion(currentVersion, options.major, options.minor);
await incrementVersionOfAllPackages(newVersion);
const answer = await askQuestion(`Do you want to create a tag and push for version ${newVersion}? (yes/no) `);
if (answer.trim().toLowerCase() === 'yes') {
commitChanges(newVersion);
createGitTag(newVersion);
pushChanges();
console.log(`Release for version ${newVersion} completed successfully.`);
} else {
revertPackagesChanges();
console.log(`Release for version ${newVersion} aborted by user.`);
}
})();