Skip to content

Commit 1b97451

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(scripts): extract version increment logic into separate function
Improve code organization by moving version increment logic from incrementVersions into a new reusable function incrementVersionsForModuleLocation. Ticket: DX-1201 Co-authored-by: llm-git <[email protected]>
1 parent 5a484c1 commit 1b97451

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

scripts/prepare-release.ts

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getLernaModules,
1212
changeScopeInFile,
1313
setDependencyVersion,
14+
DistTags,
1415
} from './prepareRelease';
1516

1617
async function setLernaModules(): Promise<{
@@ -43,6 +44,59 @@ function replaceBitGoPackageScope(rootDir: string, targetScope: string): void {
4344
writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(json, null, 2) + '\n');
4445
}
4546

47+
/**
48+
* Increment the version for a single module based on the preid.
49+
*
50+
* @param {String} preid - The prerelease identifier
51+
* @param {String} modulePath - The location of the module to update
52+
* @param {DistTags|undefined} tags - The dist tags for the module
53+
* @param {String[]} moduleLocations - All module locations for dependency updates
54+
* @returns {String|undefined} - The new version if set, undefined otherwise
55+
*/
56+
function incrementVersionsForModuleLocation(
57+
preid: string,
58+
modulePath: string,
59+
tags: DistTags | undefined,
60+
moduleLocations: string[]
61+
): string | undefined {
62+
const json = JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' }));
63+
64+
let prevTag: string | undefined = undefined;
65+
66+
if (tags) {
67+
if (tags[preid]) {
68+
const version = tags[preid].split('-');
69+
const latest = tags?.latest?.split('-') ?? ['0.0.0'];
70+
prevTag = lt(version[0], latest[0]) ? `${tags.latest}-${preid}` : tags[preid];
71+
} else {
72+
prevTag = `${tags.latest}-${preid}`;
73+
}
74+
}
75+
76+
if (prevTag) {
77+
const next = inc(prevTag, 'prerelease', undefined, preid);
78+
assert(typeof next === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`);
79+
console.log(`Setting next version for ${json.name} to ${next}`);
80+
json.version = next;
81+
writeFileSync(path.join(modulePath, 'package.json'), JSON.stringify(json, null, 2) + '\n');
82+
// since we're manually setting new versions, we must also reconcile all other lerna packages to use the 'next' version for this module
83+
moduleLocations.forEach((otherModulePath) => {
84+
// skip it for the current version
85+
if (otherModulePath === modulePath) {
86+
return;
87+
}
88+
const otherJsonContent = readFileSync(path.join(otherModulePath, 'package.json'), { encoding: 'utf-8' });
89+
if (otherJsonContent.includes(json.name)) {
90+
const otherJson = JSON.parse(otherJsonContent);
91+
setDependencyVersion(otherJson, json.name, next);
92+
writeFileSync(path.join(otherModulePath, 'package.json'), JSON.stringify(otherJson, null, 2) + '\n');
93+
}
94+
});
95+
return next;
96+
}
97+
return undefined;
98+
}
99+
46100
/**
47101
* increment the version based on the preid.
48102
*
@@ -53,42 +107,7 @@ async function incrementVersions(preid: string, moduleLocations: string[]): Prom
53107
const distTags = await getDistTagsForModuleLocations(moduleLocations);
54108
for (let i = 0; i < moduleLocations.length; i++) {
55109
try {
56-
const modulePath = moduleLocations[i];
57-
const tags = distTags[i];
58-
const json = JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' }));
59-
60-
let prevTag: string | undefined = undefined;
61-
62-
if (typeof tags === 'object') {
63-
if (tags[preid]) {
64-
const version = tags[preid].split('-');
65-
const latest = tags?.latest?.split('-') ?? ['0.0.0'];
66-
prevTag = lt(version[0], latest[0]) ? `${tags.latest}-${preid}` : tags[preid];
67-
} else {
68-
prevTag = `${tags.latest}-${preid}`;
69-
}
70-
}
71-
72-
if (prevTag) {
73-
const next = inc(prevTag, 'prerelease', undefined, preid);
74-
assert(typeof next === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`);
75-
console.log(`Setting next version for ${json.name} to ${next}`);
76-
json.version = next;
77-
writeFileSync(path.join(modulePath, 'package.json'), JSON.stringify(json, null, 2) + '\n');
78-
// since we're manually setting new versions, we must also reconcile all other lerna packages to use the 'next' version for this module
79-
moduleLocations.forEach((otherModulePath) => {
80-
// skip it for the current version
81-
if (otherModulePath === modulePath) {
82-
return;
83-
}
84-
const otherJsonContent = readFileSync(path.join(otherModulePath, 'package.json'), { encoding: 'utf-8' });
85-
if (otherJsonContent.includes(json.name)) {
86-
const otherJson = JSON.parse(otherJsonContent);
87-
setDependencyVersion(otherJson, json.name, next as string);
88-
writeFileSync(path.join(otherModulePath, 'package.json'), JSON.stringify(otherJson, null, 2) + '\n');
89-
}
90-
});
91-
}
110+
incrementVersionsForModuleLocation(preid, moduleLocations[i], distTags[i], moduleLocations);
92111
} catch (e) {
93112
// it's not necessarily a blocking error. Let lerna try and publish anyways
94113
console.warn(`Couldn't set next version for ${moduleLocations[i]}`, e);

0 commit comments

Comments
 (0)