Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(semver): increment version for preminor, prepatch and premajor #783

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,123 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/s
* **d:** 🚀 new awesome feature xxxxxxx
"
`;

exports[`@jscutlery/semver @jscutlery/semver:version when pre-releasing libs/a (--releaseAs=preminor --preid=alpha) should generate CHANGELOG.md 1`] = `
"# Changelog

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

# [1.2.0-alpha.0](/compare/a-1.1.0-alpha.0...a-1.2.0-alpha.0) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature 2 xxxxxxx



# [1.1.0-alpha.0](/compare/a-1.0.1-beta.1...a-1.1.0-alpha.0) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature 1 xxxxxxx



## [1.0.1-beta.1](/compare/a-1.0.1-beta.0...a-1.0.1-beta.1) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature 2 xxxxxxx



## [1.0.1-beta.0](/compare/a-1.0.0...a-1.0.1-beta.0) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature 1 xxxxxxx



# [1.0.0](/compare/a-0.1.0...a-1.0.0) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature xxxxxxx


### BREAKING CHANGES

* **a:** 🚨 Breaking change description



# 0.1.0 (yyyy-mm-dd)


### Bug Fixes

* **a:** 🐞 fix bug xxxxxxx


### Features

* **a:** 🚀 new feature xxxxxxx
"
`;

exports[`@jscutlery/semver @jscutlery/semver:version when pre-releasing libs/a (--releaseAs=prerelease --preid=beta) should generate CHANGELOG.md 1`] = `
"# Changelog

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [1.0.1-beta.1](/compare/a-1.0.1-beta.0...a-1.0.1-beta.1) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature 2 xxxxxxx



## [1.0.1-beta.0](/compare/a-1.0.0...a-1.0.1-beta.0) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature 1 xxxxxxx



# [1.0.0](/compare/a-0.1.0...a-1.0.0) (yyyy-mm-dd)


### Features

* **a:** 🚀 new feature xxxxxxx


### BREAKING CHANGES

* **a:** 🚨 Breaking change description



# 0.1.0 (yyyy-mm-dd)


### Bug Fixes

* **a:** 🐞 fix bug xxxxxxx


### Features

* **a:** 🚀 new feature xxxxxxx
"
`;
86 changes: 86 additions & 0 deletions packages/semver/src/executors/version/index.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,92 @@ describe('@jscutlery/semver', () => {
});
});

describe('when pre-releasing libs/a (--releaseAs=prerelease --preid=beta)', () => {
beforeAll(() => {
testingWorkspace.exec(
`
echo feat >> libs/a/a.txt
git add .
git commit -m "feat(a): 🚀 new feature 1"
`,
);
testingWorkspace.runNx(
`run a:version --releaseAs=prerelease --preid=beta --noVerify`,
);
testingWorkspace.exec(
`
echo feat >> libs/a/a.txt
git add .
git commit -m "feat(a): 🚀 new feature 2"
`,
);
testingWorkspace.runNx(
`run a:version --releaseAs=prerelease --preid=beta --noVerify`,
);
});

it('should tag with version', () => {
expect(getLastTag(testingWorkspace.root)).toBe('a-1.0.1-beta.1');
});

it('should bump package version', () => {
expect(
readFile(`${testingWorkspace.root}/libs/a/package.json`),
).toMatch(/"version": "1.0.1-beta.1"/);
});

it('should generate CHANGELOG.md', () => {
expect(
deterministicChangelog(
readFile(`${testingWorkspace.root}/libs/a/CHANGELOG.md`),
),
).toMatchSnapshot();
});
});

describe('when pre-releasing libs/a (--releaseAs=preminor --preid=alpha)', () => {
beforeAll(() => {
testingWorkspace.exec(
`
echo feat >> libs/a/a.txt
git add .
git commit -m "feat(a): 🚀 new feature 1"
`,
);
testingWorkspace.runNx(
`run a:version --releaseAs=preminor --preid=alpha --noVerify`,
);
testingWorkspace.exec(
`
echo feat >> libs/a/a.txt
git add .
git commit -m "feat(a): 🚀 new feature 2"
`,
);
testingWorkspace.runNx(
`run a:version --releaseAs=preminor --preid=alpha --noVerify`,
);
});

it('should tag with version', () => {
expect(getLastTag(testingWorkspace.root)).toBe('a-1.2.0-alpha.0');
});

it('should bump package version', () => {
expect(
readFile(`${testingWorkspace.root}/libs/a/package.json`),
).toMatch(/"version": "1.2.0-alpha.0"/);
});

it('should generate CHANGELOG.md', () => {
expect(
deterministicChangelog(
readFile(`${testingWorkspace.root}/libs/a/CHANGELOG.md`),
),
).toMatchSnapshot();
});
});

describe('when libs/b changed (with --skipCommit)', () => {
beforeAll(() => {
testingWorkspace.exec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,22 @@ describe(getLastVersion.name, () => {
'my-lib-1.0.0',
]);

const tag = await lastValueFrom(getLastVersion({ tagPrefix }));
const tag = await lastValueFrom(
getLastVersion({ tagPrefix, releaseType: 'prerelease' }),
);
const tagWithPreidFeat = await lastValueFrom(
getLastVersion({ tagPrefix, preid: 'add-feature' }),
getLastVersion({
tagPrefix,
releaseType: 'prerelease',
preid: 'add-feature',
}),
);
const tagWithPreidFix = await lastValueFrom(
getLastVersion({ tagPrefix, preid: 'fix-bug' }),
getLastVersion({
tagPrefix,
releaseType: 'prerelease',
preid: 'fix-bug',
}),
);

expect(tag).toEqual('2.1.0-z-is-the-last-letter-in-alphabet.0');
Expand All @@ -69,9 +79,7 @@ describe(getLastVersion.name, () => {
'my-lib-1.0.0',
]);

const tag = await lastValueFrom(
getLastVersion({ tagPrefix, includePrerelease: false }),
);
const tag = await lastValueFrom(getLastVersion({ tagPrefix }));

expect(tag).toEqual('2.0.0');
});
Expand Down
24 changes: 11 additions & 13 deletions packages/semver/src/executors/version/utils/get-last-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import type { Observable } from 'rxjs';

export function getLastVersion({
tagPrefix,
includePrerelease = true,
releaseType,
preid,
}: {
tagPrefix: string;
includePrerelease?: boolean;
releaseType?: semver.ReleaseType;
preid?: string;
}): Observable<string> {
return from(gitSemverTags({ tagPrefix }) as Promise<string[]>).pipe(
Expand All @@ -25,17 +25,15 @@ export function getLastVersion({
return true;
}

if (includePrerelease) {
/* Filter-in everything if preid is not set. */
if (preid == null) {
return true;
}

/* Filter-in if preids match. */
const [versionPreid] = prerelease;
if (versionPreid === preid) {
return true;
}
/* Filter-in everything if preid is not set. */
if (releaseType && preid == null) {
return true;
}

/* Filter-in if preids match. */
const [versionPreid] = prerelease;
if (releaseType && versionPreid === preid) {
return true;
}

/* Filter-out everything else.*/
Expand Down
8 changes: 2 additions & 6 deletions packages/semver/src/executors/version/utils/try-bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getProjectVersion({
const lastVersion$ = getLastVersion({
tagPrefix,
preid,
includePrerelease: releaseType === 'prerelease',
releaseType,
}).pipe(
catchError(() => {
_logStep({
Expand Down Expand Up @@ -263,11 +263,7 @@ export function _manualBump({
preid: string;
}) {
return defer(() => {
const hasPreid =
['premajor', 'preminor', 'prepatch', 'prerelease'].includes(
releaseType,
) && preid !== null;

const hasPreid = preid !== null;
const semverArgs: [string, semver.ReleaseType, ...string[]] = [
since,
releaseType as semver.ReleaseType,
Expand Down
Loading