Skip to content

Commit 1a6b3de

Browse files
committed
Version bump for [email protected]
1 parent d215771 commit 1a6b3de

File tree

7 files changed

+138
-73
lines changed

7 files changed

+138
-73
lines changed

.base-branch

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
origin/main

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.0] - 2021-12-29
9+
10+
### Added
11+
12+
### Removed
13+
14+
### Changed
15+
16+
### Fixed
17+
818
## [1.0.0] - 2021-12-17
919

1020
### Added

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "release-test",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Testing release",
55
"main": "CHANGELOG.md",
66
"files": [

script/cli-utils.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const output = require('process').stdout;
1010
const util = require('util');
1111
const readline = require('readline');
1212

13+
const YES = 'yes';
1314

1415
const logger = {
1516
error: (output, info = '') => console.error('\x1b[31m%s\x1b[0m', output, info), // Red
@@ -46,24 +47,24 @@ const prompt = async (prompt) => {
4647
resolve(answer)
4748
});
4849
});
49-
}
50+
};
5051

51-
const shouldContinuePrompt = async (prompt) => {
52+
const shouldContinuePrompt = async () => {
5253
const cont = await new Promise((resolve) => {
5354
rl = readline.createInterface({ input , output });
54-
rl.question(prompt, (answer) => {
55+
rl.question(`Type '${util.format('\x1b[32m%s\x1b[0m', YES)}' to continue\n`, (answer) => {
5556
rl.close();
56-
resolve(answer.trim().toLowerCase() === 'yes');
57+
resolve(answer.trim().toLowerCase() === YES);
5758
});
5859
});
5960
if (!cont) {
6061
quit(0);
6162
}
62-
}
63+
};
6364

6465
const quit = (statusCode) => {
6566
process.exit(statusCode);
66-
}
67+
};
6768

6869
module.exports = {
6970
logger,

script/release.js

+48-34
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,53 @@ const deployDemo = (version) => {
1010
// spawnOrFail('npm', [`run deploy -- -b ${demo_name} -s ${demo_name} -o ${demo_name} -u false`], { printErr: true });
1111
};
1212

13+
const getCurrentRemoteBranch = () => {
14+
return (spawnOrFail('git', ['for-each-ref --format="%(upstream:short)" "$(git symbolic-ref -q HEAD)"'], { skipOutput: true })).trim();
15+
};
16+
17+
const buildAndPack = () => {
18+
logger.log('Building package...');
19+
spawnOrFail('npm', ['run build:release']);
20+
logger.log('Packaging ...');
21+
spawnOrFail('npm', ['pack --dry-run'], { printErr: true });
22+
};
23+
24+
const cleanUp = async (remoteBranch) => {
25+
logger.warn(`Warning: Resetting HEAD ${remoteBranch ? `to ${remoteBranch}` : ''}.\nAll current staged and local changes will be lost.`);
26+
await shouldContinuePrompt();
27+
spawnOrFail('git', [`reset --hard ${remoteBranch ? remoteBranch : ''}`]);
28+
spawnOrFail('git', [' clean -ffxd .']);
29+
};
30+
1331
const release = async () => {
1432
spawnOrFail('git', ['fetch origin'], { skipOutput: true });
1533
const currentBranch = (spawnOrFail('git', [' branch --show-current'], { skipOutput: true })).trim();
16-
const remoteBranch = (spawnOrFail('git', ['for-each-ref --format="%(upstream:short)" "$(git symbolic-ref -q HEAD)"'], { skipOutput: true })).trim();
34+
const remoteBranch = getCurrentRemoteBranch();
1735
if (!remoteBranch || (remoteBranch !== 'origin/main' && (/^origin\/release-[0-9]+\.x$/).test(remoteBranch))) {
1836
logger.error(`The local branch ${currentBranch} does not track either main or release-<version>.x branch`);
1937
quit(1);
2038
}
21-
logger.warn(`Warning: Resetting HEAD to ${remoteBranch}.\nAll current staged and local changes will be lost.`);
22-
await shouldContinuePrompt('Do you wish to continue\n');
23-
// spawnOrFail('git', [`reset --hard ${remoteBranch}`]);
24-
// spawnOrFail('git', [' clean -ffxd .']);
39+
await cleanUp(remoteBranch);
2540

26-
logger.log('Building package...');
27-
spawnOrFail('npm', ['run build:release']);
28-
logger.log('Packaging ...');
29-
spawnOrFail('npm', ['pack --dry-run'], { printErr: true });
30-
logger.log('Do you want to upload these files to release branch');
31-
await shouldContinuePrompt('Type \'yes\' to continue\n');
41+
buildAndPack();
42+
logger.log('Do you want to upload these files to release branch?\n');
43+
await shouldContinuePrompt();
3244
spawnOrFail('git', ['push origin HEAD:release -f']);
3345
deployDemo(currentVersion);
3446

3547
//Bump next development version
36-
versionBump();
37-
}
48+
await versionBump();
49+
};
50+
51+
const hotfix = async () => {
52+
await cleanUp();
53+
54+
buildAndPack();
55+
56+
await versionBump(1, 'hotfix');
57+
58+
deployDemo(currentVersion);
59+
};
3860

3961
const main = async () => {
4062
logger.log('Choose one of the following options:');
@@ -47,32 +69,24 @@ const main = async () => {
4769
case '1':
4870
release();
4971
break;
72+
case '2':
73+
hotfix();
74+
break;
75+
case '3':
76+
const remoteBranch = getCurrentRemoteBranch();
77+
if (!remoteBranch || (remoteBranch !== 'origin/release' && (remoteBranch !== 'origin/hotfix'))) {
78+
logger.error(`The local branch ${currentBranch} does not track either release or hotfix branch`);
79+
quit(1);
80+
}
81+
cleanUp(remoteBranch);
82+
deployDemo(currentVersion);
83+
break;
5084
default:
5185
if (option) {
5286
logger.error('Invalid option');
5387
}
5488
quit(1);
5589
}
56-
// logger.log(`Resetting HEAD to origin/${currentBranch}`);
57-
// // spawnOrFail('git', [`reset --hard origin/${currentBranch}`]);
58-
// // spawnOrFail('git', [' clean -ffxd .']);
59-
60-
// logger.log('Building package...');
61-
// spawnOrFail('npm', ['run build:release']);
62-
// logger.log('Packaging ...');
63-
// spawnOrFail('npm', ['pack --dry-run'], { printErr: true });
64-
65-
// logger.log('Do you want to upload these files');
66-
// let cont = await shouldContinuePrompt('Type \'yes\' to continue\n');
67-
// if (!cont) {
68-
// quit(0);
69-
// }
70-
// if (option === '1') { //Release
71-
// spawnOrFail('git', ['push origin HEAD:release -f']);
72-
// deployDemo(currentVersion);
73-
// } else if (option === '2') { //Hotfix
74-
// spawnOrFail('git', ['push origin HEAD:hotfix -f']);
75-
// }
76-
}
90+
};
7791

7892
main();

script/version-util.js

+69-30
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,46 @@ const { logger, spawnOrFail, prompt, shouldContinuePrompt, quit, fs, process, pa
44
// process.chdir(path.join(__dirname, '..'));
55
const currentVersion = require('../package.json').version;
66

7+
const isPreRelease = (version) => {
8+
return version.split('.')[3] >0;
9+
};
10+
711
const getNewVersion = (currentVersion, versionIncrement) => {
8-
const ver_arr = currentVersion.split('.');
12+
const verArr = currentVersion.split('.');
13+
const isBeta = isPreRelease(currentVersion);
914
switch (versionIncrement) {
1015
case 1: //Patch
11-
if (ver_arr[3]) {
16+
if (isBeta) {
1217
logger.error(`ERROR: Cannot increase patch in pre-release version `);
1318
return undefined;
1419
}
15-
ver_arr[2] = Number(ver_arr[2]) + 1;
16-
return ver_arr.join('.');
20+
verArr[2] = Number(verArr[2]) + 1;
21+
return verArr.join('.');
1722
case 2: // Minor
18-
if (ver_arr[3]) {
23+
if (isBeta) {
1924
logger.error(`ERROR: Cannot increase patch in pre-release version `);
2025
return undefined;
2126
}
22-
ver_arr[1] = Number(ver_arr[1]) + 1;
23-
return ver_arr.join('.');
27+
verArr[1] = Number(verArr[1]) + 1;
28+
return verArr.join('.');
2429
case 3: //Major
25-
if (ver_arr[3]) {
30+
if (isBeta) {
2631
return currentVersion.split('-')[0];
2732
}
28-
ver_arr[0] = Number(ver_arr[0]) + 1;
29-
return ver_arr.join('.');
33+
verArr[0] = Number(verArr[0]) + 1;
34+
return verArr.join('.');
3035
case 4: // Beta
31-
if (ver_arr[3]) { //Existing beta
32-
ver_arr[3] = Number(ver_arr[3]) + 1;
33-
return ver_arr.join('.');
36+
if (isBeta) { //Existing beta
37+
verArr[3] = Number(verArr[3]) + 1;
38+
return verArr.join('.');
3439
}
35-
ver_arr[0] = Number(ver_arr[0]) + 1;
36-
return ver_arr.join('.') + '-beta.0';
40+
verArr[0] = Number(verArr[0]) + 1;
41+
return verArr.join('.') + '-beta.0';
3742
default:
3843
logger.error(`ERROR: Invalid input: ${versionIncrement}`);
3944
return undefined;
4045
}
41-
}
46+
};
4247

4348
const updateChangelog = (newVersion) => {
4449
logger.log(`Updating CHANGELOG.md with a new release entry - ${newVersion}`);
@@ -53,35 +58,69 @@ const updateChangelog = (newVersion) => {
5358
\n`;
5459
changeLog = changeLog.substring(0, latestEntryIndex) + newEntry + changeLog.substring(latestEntryIndex);
5560
fs.writeFileSync(filePath, changeLog);
61+
};
62+
63+
const updateBaseBranch = (branchName) => {
64+
logger.log(`Updating the base branch in .base-branch to ${branchName}`);
65+
const filePath = path.resolve(__dirname, '../.base-branch');
66+
fs.writeFileSync(filePath, `origin/${branchName}`);
5667
}
5768

58-
const versionBump = async () => {
59-
logger.log('Choose one of the following options to bump the next version:');
60-
logger.log(' 1. Patch');
61-
logger.log(' 2. Minor');
62-
logger.log(' 3. Major');
63-
logger.log(' 4. Beta');
64-
const option = await prompt('');
65-
const newVersion = getNewVersion(currentVersion, Number(option));
69+
const versionBump = async (option, branchName) => {
70+
if (!option) {
71+
logger.log('Choose one of the following options to bump the next version:');
72+
logger.log(' 1. Patch');
73+
logger.log(' 2. Minor');
74+
logger.log(' 3. Major');
75+
logger.log(' 4. Beta');
76+
option = Number(await prompt(''));
77+
}
78+
const newVersion = getNewVersion(currentVersion, option);
6679
if (!newVersion) {
6780
quit(1);
6881
}
82+
83+
branchName = branchName ? branchName : 'version-bump';
84+
85+
const prevReleaseBranch = !isPreRelease(currentVersion) && (option === 3 || option === 4)
86+
? `release-${currentVersion.split('.')[0]}.x`
87+
: '';
88+
6989
logger.warn('Warning: you are bumping the version\n');
7090
logger.warn(` From: ${currentVersion}\n`);
7191
logger.warn(` To: ${newVersion}\n`);
72-
await shouldContinuePrompt('Type \'yes\' to continue\n');
92+
if (prevReleaseBranch) {
93+
logger.warn(` This will also create ${prevReleaseBranch} branch.`);
94+
}
95+
await shouldContinuePrompt();
96+
97+
if (prevReleaseBranch) {
98+
const currentBranch = (spawnOrFail('git', [' branch --show-current'], { skipOutput: true })).trim();
99+
spawnOrFail('git', [`checkout -b ${prevReleaseBranch}`]);
100+
updateBaseBranch(prevReleaseBranch);
101+
spawnOrFail('git', ['add -A']);
102+
spawnOrFail('git', [`commit -m "Update base branch to ${prevReleaseBranch}"`]);
103+
spawnOrFail('git', [`push origin HEAD:${prevReleaseBranch} -f`]);
104+
logger.log(`Branch ${prevReleaseBranch} is created. Please make sure to set branch protection.`);
105+
106+
//Switch back to the local branch
107+
spawnOrFail('git', [`checkout ${currentBranch}`]);
108+
}
109+
73110
spawnOrFail('npm', [`version ${newVersion} --no-git-tag-version`]);
74111
updateChangelog(newVersion);
75112

76113
logger.log('Committing version bump...');
77114
spawnOrFail('git', ['add -A']);
78115
spawnOrFail('git', [`commit -m "Version bump for amazon-chime-sdk-js@${newVersion}"`]);
79-
logger.log('Do you want to upload these files to version-bump branch');
80-
await shouldContinuePrompt('Type \'yes\' to continue\n');
81-
spawnOrFail('git', ['push origin HEAD:version-bump -f']);
82-
}
116+
logger.log(`Do you want to upload these files to ${branchName} branch?\n`);
117+
await shouldContinuePrompt();
118+
spawnOrFail('git', [`push origin HEAD:${branchName} -f`]);
119+
logger.log('Please create a pull request to merge the version bump to main.');
120+
};
83121

84122
module.exports = {
85123
versionBump,
86124
currentVersion,
87-
}
125+
updateBaseBranch
126+
};

0 commit comments

Comments
 (0)