Skip to content

Commit d8812b2

Browse files
committed
fix: versioning after pre-release exit
1 parent b983570 commit d8812b2

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

e2e/pre-release.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ it("exists pre-release", async () => {
6363
await sandbox.$`${CONVER} pre exit my-package`;
6464

6565
// check package
66-
const pkg = (await sandbox.readJsoncFile("package.json")) as {
66+
let pkg = (await sandbox.readJsoncFile("package.json")) as {
6767
version?: string;
6868
publishConfig?: {
6969
tag?: string;
@@ -75,6 +75,67 @@ it("exists pre-release", async () => {
7575
// versioning
7676
await sandbox.$`${CONVER} version`;
7777

78+
// check package
79+
pkg = (await sandbox.readJsoncFile("package.json")) as {
80+
version?: string;
81+
publishConfig?: {
82+
tag?: string;
83+
};
84+
};
85+
expect(pkg.version).toBe("1.3.0");
86+
expect(pkg.publishConfig?.tag).toBe(undefined);
87+
88+
// check config
89+
const options = (await sandbox.readJsoncFile("conver.json")) as Options;
90+
expect(Object.keys(options.original ?? {})).not.toContain("my-package");
91+
});
92+
});
93+
94+
it("preserves version after pre-release exit", async () => {
95+
await sandbox(async (sandbox) => {
96+
// package.json
97+
await sandbox.writeFiles({
98+
"package.json": json({
99+
name: "my-package",
100+
version: "1.3.0-rc.5",
101+
publishConfig: {
102+
tag: "next",
103+
},
104+
}),
105+
"conver.json": json({
106+
original: {
107+
"my-package": "1.2.3",
108+
},
109+
}),
110+
});
111+
112+
// init sandbox
113+
await sandbox.$`git init`;
114+
await sandbox.$`git commit -m ${"chore: first commit"} --allow-empty`;
115+
116+
// add commit
117+
await sandbox.writeFiles({
118+
"foo.txt": "foo",
119+
});
120+
await sandbox.$`git add foo.txt`;
121+
await sandbox.$`git commit -m ${"fix: add foo"}`;
122+
123+
// exit pre-release
124+
await sandbox.$`${CONVER} pre exit my-package`;
125+
126+
// versioning
127+
await sandbox.$`${CONVER} version`;
128+
129+
// check package
130+
const pkg = (await sandbox.readJsoncFile("package.json")) as {
131+
version?: string;
132+
publishConfig?: {
133+
tag?: string;
134+
};
135+
};
136+
expect(pkg.version).toBe("1.3.0");
137+
expect(pkg.publishConfig?.tag).toBe(undefined);
138+
78139
// check config
79140
const options = (await sandbox.readJsoncFile("conver.json")) as Options;
80141
expect(Object.keys(options.original ?? {})).not.toContain("my-package");

src/lib/versioning.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,22 @@ export function createVersioningPlan(
233233
let bump = bumps.get(pkg);
234234
if (!bump) continue;
235235

236+
/** The package's original version before pre-release. */
237+
const originalVersion = option(options, "original")[pkg.name];
238+
239+
/** Current version is pre-release. */
240+
const isPre = isPreRelease(pkg.version);
241+
/** Was pre-release in previous versioning. */
242+
const wasPre = !!originalVersion;
243+
236244
/** The package's current version. */
237245
const currentVersion = new SemVer(pkg.version);
238246

239247
/**
240248
* The version to base increments from. Normally this is the same as
241249
* {@link currentVersion}, except in pre-releases.
242250
*/
243-
const baseVersion = new SemVer(
244-
option(options, "original")[pkg.name] ?? pkg.version,
245-
);
251+
const baseVersion = new SemVer(originalVersion ?? pkg.version);
246252

247253
// Limit bump for '0.x' packages to 'minor', to prevent releasing the first
248254
// stable version, which may not be wanted.
@@ -258,7 +264,7 @@ export function createVersioningPlan(
258264
let newVersion = baseVersion.inc(bump);
259265
newVersion.prerelease = currentVersion.prerelease;
260266

261-
if (isPreRelease(currentVersion)) {
267+
if (isPre) {
262268
// When the main version (stable version before pre-release suffix) has
263269
// not changed, revert to the current version.
264270
const noMainBump = currentVersion.compareMain(newVersion) >= 0;
@@ -278,6 +284,10 @@ export function createVersioningPlan(
278284
}
279285
}
280286

287+
if (wasPre && currentVersion.compare(newVersion) > 0) {
288+
newVersion = currentVersion;
289+
}
290+
281291
versioning.push({
282292
name: pkg.name,
283293
oldVersion: currentVersion.format(),

0 commit comments

Comments
 (0)