Skip to content

Commit 2dfd205

Browse files
OttoAllmendingerllm-git
andcommitted
test(scripts): use semver.lt instead of custom comparator
Add semver's `lt` function to simplify version comparison in prepare-release script. Issue: DX-1201 Co-authored-by: llm-git <[email protected]>
1 parent 3537128 commit 2dfd205

File tree

1 file changed

+4
-34
lines changed

1 file changed

+4
-34
lines changed

scripts/prepare-release.ts

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from 'node:assert';
22
import { readFileSync, writeFileSync } from 'fs';
33
import * as path from 'path';
4-
import { inc } from 'semver';
4+
import { inc, lt } from 'semver';
55
import {
66
walk,
77
getDistTagsForModuleLocations,
@@ -34,42 +34,12 @@ function replacePackageScopes() {
3434
// modules/bitgo is the only package we publish without an `@bitgo` prefix, so
3535
// we must manually set one
3636
function replaceBitGoPackageScope() {
37-
const cwd = path.join(__dirname, '../', 'modules', 'bitgo');
37+
const cwd = path.join(ROOT_DIR, 'modules', 'bitgo');
3838
const json = JSON.parse(readFileSync(path.join(cwd, 'package.json'), { encoding: 'utf-8' }));
3939
json.name = `${TARGET_SCOPE}/bitgo`;
4040
writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(json, null, 2) + '\n');
4141
}
4242

43-
/** Small version checkers in place of an npm dependency installation */
44-
function compareversion(version1, version2) {
45-
let result = false;
46-
47-
if (typeof version1 !== 'object') {
48-
version1 = version1.toString().split('.');
49-
}
50-
if (typeof version2 !== 'object') {
51-
version2 = version2.toString().split('.');
52-
}
53-
54-
for (let i = 0; i < Math.max(version1.length, version2.length); i++) {
55-
if (version1[i] === undefined) {
56-
version1[i] = 0;
57-
}
58-
if (version2[i] === undefined) {
59-
version2[i] = 0;
60-
}
61-
62-
if (Number(version1[i]) < Number(version2[i])) {
63-
result = true;
64-
break;
65-
}
66-
if (version1[i] !== version2[i]) {
67-
break;
68-
}
69-
}
70-
return result;
71-
}
72-
7343
/**
7444
* increment the version based on the preid. default to `beta`
7545
*
@@ -89,15 +59,15 @@ async function incrementVersions(preid = 'beta') {
8959
if (tags[preid]) {
9060
const version = tags[preid].split('-');
9161
const latest = tags?.latest?.split('-') ?? ['0.0.0'];
92-
prevTag = compareversion(version[0], latest[0]) ? `${tags.latest}-${preid}` : tags[preid];
62+
prevTag = lt(version[0], latest[0]) ? `${tags.latest}-${preid}` : tags[preid];
9363
} else {
9464
prevTag = `${tags.latest}-${preid}`;
9565
}
9666
}
9767

9868
if (prevTag) {
9969
const next = inc(prevTag, 'prerelease', undefined, preid);
100-
assert(typeof next === 'string', `Failed to increment version for ${json.name}`);
70+
assert(typeof next === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`);
10171
console.log(`Setting next version for ${json.name} to ${next}`);
10272
json.version = next;
10373
writeFileSync(path.join(modulePath, 'package.json'), JSON.stringify(json, null, 2) + '\n');

0 commit comments

Comments
 (0)