Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 56 additions & 29 deletions lib/prepare_release.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path';
import { promises as fs } from 'node:fs';
import { readFileSync, promises as fs } from 'node:fs';

import semver from 'semver';
import { replaceInFile } from 'replace-in-file';
Expand Down Expand Up @@ -39,23 +39,28 @@ export default class ReleasePreparation {

// Allow passing optional new version.
if (argv.newVersion) {
const newVersion = semver.clean(argv.newVersion);
if (!semver.valid(newVersion)) {
cli.warn(`${newVersion} is not a valid semantic version.`);
const newVersion = semver.parse(argv.newVersion);
if (!newVersion) {
cli.warn(`${argv.newVersion} is not a valid semantic version.`);
return;
}
this.newVersion = newVersion;
this.newVersion = newVersion.version;
this.versionComponents = {
major: newVersion.major,
minor: newVersion.minor,
patch: newVersion.patch
};
this.getLastRef(this.findMostRecentReleaseFromChangelog(
path.resolve(`doc/changelogs/CHANGELOG_V${newVersion.major}.md`)
).tag);
} else {
this.newVersion = this.calculateNewVersion();
this.versionComponents = this.calculateNewVersion();
const { major, minor, patch } = this.versionComponents;
this.newVersion = `${major}.${minor}.${patch}`;
cli.info(`Attempt at preparing ${this.newVersion}`);
}

const { upstream, owner, repo, newVersion } = this;

this.versionComponents = {
major: semver.major(newVersion),
minor: semver.minor(newVersion),
patch: semver.patch(newVersion)
};
const { upstream, owner, repo } = this;

this.stagingBranch = `v${this.versionComponents.major}.x-staging`;
this.releaseBranch = `v${this.versionComponents.major}.x`;
Expand Down Expand Up @@ -369,24 +374,35 @@ export default class ReleasePreparation {
return missing;
}

calculateNewVersion() {
let newVersion;

const lastTagVersion = semver.clean(this.getLastRef());
const lastTag = {
major: semver.major(lastTagVersion),
minor: semver.minor(lastTagVersion),
patch: semver.patch(lastTagVersion)
findMostRecentReleaseFromChangelog(pathToChangelog, isMainChangelog) {
const data = readFileSync(pathToChangelog, 'utf8');
const [, major, minor, patch] = (isMainChangelog
? /<b><a href="doc\/changelogs\/CHANGELOG_V(\d+)\.md#\1\.(\d+)\.(\d+)">\1\.\2\.\3<\/a><\/b><br\/>/
: /<a href="#(\d+)\.(\d+)\.(\d+)">\1\.\2\.\3<\/a><br\/>/).exec(data);

return {
major,
minor,
patch,
tag: `v${major}.${minor}.${patch}`
};
}

const changelog = this.getChangelog();
calculateNewVersion() {
const lastTag = this.findMostRecentReleaseFromChangelog(path.resolve('CHANGELOG.md'), true);
const { tag, ...newVersion } = lastTag;

const changelog = this.getChangelog(lastTag.tag);

if (changelog.includes('SEMVER-MAJOR')) {
newVersion = `${lastTag.major + 1}.0.0`;
newVersion.major++;
newVersion.minor = 0;
newVersion.patch = 0;
} else if (changelog.includes('SEMVER-MINOR') || this.isLTSTransition) {
newVersion = `${lastTag.major}.${lastTag.minor + 1}.0`;
newVersion.minor++;
newVersion.patch = 0;
} else {
newVersion = `${lastTag.major}.${lastTag.minor}.${lastTag.patch + 1}`;
newVersion.patch++;
}

return newVersion;
Expand All @@ -396,11 +412,22 @@ export default class ReleasePreparation {
return runSync('git', ['rev-parse', '--abbrev-ref', 'HEAD']).trim();
}

getLastRef() {
return runSync('git', ['describe', '--abbrev=0', '--tags']).trim();
getLastRef(tagName) {
if (!tagName) {
return runSync('git', ['describe', '--abbrev=0', '--tags']).trim();
}

try {
runSync('git', ['rev-parse', tagName]);
} catch {
this.cli.startSpinner(`Error parsing git ref ${tagName}, attempting fetching it as a tag`);
runSync('git', ['fetch', this.upstream, 'tag', '-n', tagName]);
this.cli.stopSpinner(`Tag fetched: ${tagName}`);
}
return tagName;
}

getChangelog() {
getChangelog(tagName) {
const changelogMaker = new URL(
'../node_modules/.bin/changelog-maker' + (isWindows ? '.cmd' : ''),
import.meta.url
Expand All @@ -411,7 +438,7 @@ export default class ReleasePreparation {
'--markdown',
'--filter-release',
'--start-ref',
this.getLastRef()
this.getLastRef(tagName)
]).trim();
}

Expand Down