Skip to content

Commit

Permalink
release: v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Sep 4, 2024
1 parent d8820c7 commit 3cf5678
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 48 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

---

## [v0.3.0](https://github.com/hatemhosny/racing-bars/compare/v0.2.0...v0.3.0) (2024-09-04)


### Features

* **website:** add search ([00015fc](https://github.com/hatemhosny/racing-bars/commit/00015fce403e40099c8288a4d6fd4e6fffe32d71))




---

## [v0.2.0](https://github.com/hatemhosny/racing-bars/compare/v0.1.2...v0.2.0) (2024-09-01)
Expand Down
138 changes: 91 additions & 47 deletions scripts/start-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ const changelogPath = '../CHANGELOG.md';

const pkg = require(pkgPath);

const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().replace(/\n/g, '');
if (gitBranch !== 'develop') {
console.log('Can only prepare a release from branch: develop');
process.exit(1);
}

const gitStatus = execSync('git status -s').toString().replace(/\n/g, '').trim();
if (gitStatus) {
console.log('Please commit changes before starting a release.');
process.exit(1);
}

const getVersion = () =>
const checkIsDevelop = () => {
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().replace(/\n/g, '');
if (gitBranch !== 'develop') {
console.log('Can only prepare a release from branch: develop');
process.exit(1);
}
};

const checkIsClean = () => {
const gitStatus = execSync('git status -s').toString().replace(/\n/g, '').trim();
if (gitStatus) {
console.log('Please commit changes before starting a release.');
process.exit(1);
}
};

const specifyVersion = () =>
input({
message: 'Please specify the new version:',
validate(value) {
Expand All @@ -36,9 +40,9 @@ const getVersion = () =>
},
});

const bumpVersion = (libBump) => {
const bumpVersion = (oldVersion, libBump) => {
if (!libBump) return;
let [major, minor, patch] = pkg.version.split('.');
let [major, minor, patch] = oldVersion.split('.');
if (libBump === 'major') {
major = String(Number(major) + 1);
minor = '0';
Expand All @@ -54,9 +58,20 @@ const bumpVersion = (libBump) => {
return `${major}.${minor}.${patch}`;
};

const getBump = () =>
select({
message: 'Library version upgrade:',
const suggestBump = (releaseNotes) => {
if (releaseNotes.includes('### BREAKING CHANGES')) {
return { bump: 'major', message: 'Has breaking changes!' };
}
if (releaseNotes.includes('### Features')) {
return { bump: 'minor', message: 'Includes new feature(s)' };
}
return { bump: 'patch', message: '' };
};

const getBump = async (defaultBump) => {
const bump = await select({
message: `Library version upgrade: (${defaultBump.message})`,
default: defaultBump.bump,
choices: [
{
name: 'Major',
Expand All @@ -74,58 +89,87 @@ const getBump = () =>
name: 'Specify version',
value: 'specify',
},
{
name: 'Cancel',
value: 'cancel',
},
],
});
if (bump === 'cancel') {
await confirmCancel();
return getBump(defaultBump);
}
return bump;
};

const stringify = (obj) => JSON.stringify(obj, null, 2) + '\n';

const cancelRelease = async () => {
if (await confirm({ message: 'Cancelling release. Do you want to discard all changes?' })) {
const confirmCancel = async () => {
if (await confirm({ message: 'Do you want to cancel release and discard all changes?' })) {
execSync(`git reset --hard`);
console.log('Release cancelled!');
process.exit(1);
}
console.log('Release cancelled!');
process.exit(1);
};

(async () => {
const libBump = await getBump();
pkg.version = libBump === 'specify' ? await getVersion() : bumpVersion(libBump);
const version = 'v' + pkg.version;
if (!(await confirm({ message: `Creating version: ${version}\nProceed?` }))) {
await cancelRelease();
}
fs.writeFileSync(new URL(pkgPath, import.meta.url), stringify(pkg), 'utf8');
const streamToString = (stream) => {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
};

const getReleaseNotes = async () =>
streamToString(
conventionalChangelog({
preset: 'angular',
}),
);

const writeChangelog = async (releaseNotes, version) => {
const changelog = fs.readFileSync(new URL(changelogPath, import.meta.url), 'utf8');
const changelogSeparator = '\n---';
const [changelogHeader, ...prevLogs] = changelog.split(changelogSeparator);

const releaseChangelog = await streamToString(
conventionalChangelog({
preset: 'angular',
}),
).then((str) => {
return '\n\n#' + str.replace('[0.0.0]', `[${version}]`).replace('v0.0.0', `${version}`);
});
const releaseChangelog =
'\n\n#' + releaseNotes.replace('[0.0.0]', `[${version}]`).replace('v0.0.0', `${version}`);

const newChangelog = [changelogHeader, releaseChangelog, ...prevLogs].join(changelogSeparator);
fs.writeFileSync(new URL(changelogPath, import.meta.url), newChangelog, 'utf8');

if (!(await confirm({ message: `Change log added to ./CHANGELOG.md\nProceed?` }))) {
await cancelRelease();
await confirmCancel();
}
};

const changeVersion = async (libBump) => {
pkg.version = libBump === 'specify' ? await specifyVersion() : bumpVersion(pkg.version, libBump);
const version = 'v' + pkg.version;
if (!(await confirm({ message: `Creating version: ${version}\nProceed?` }))) {
await confirmCancel();
}
fs.writeFileSync(new URL(pkgPath, import.meta.url), stringify(pkg), 'utf8');
return version;
};

const pushReleaseBranch = (version) => {
const branchName = 'releases/' + version;
execSync(`git checkout -b ${branchName}`);
execSync(`git add -A && git commit -m "release: ${version}"`);
execSync(`git push -u origin ${branchName}`);
})();
};

function streamToString(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
}
const run = async () => {
// checkIsDevelop();
// checkIsClean();
const releaseNotes = await getReleaseNotes();
const suggestedBump = suggestBump(releaseNotes);
const libBump = await getBump(suggestedBump);
const version = await changeVersion(libBump);
writeChangelog(releaseNotes, version);
pushReleaseBranch(version);
};

run();
2 changes: 1 addition & 1 deletion src/package.lib.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "racing-bars",
"version": "0.2.0",
"version": "0.3.0",
"description": "Bar chart race made easy 📶",
"author": "Hatem Hosny",
"license": "MIT",
Expand Down

0 comments on commit 3cf5678

Please sign in to comment.