Skip to content

Commit cc61432

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(scripts): use LernaModule objects directly for better type safety
Replace setLernaModules function with direct usage of LernaModule objects throughout the code. This simplifies the flow and provides better type safety when handling module data. Ticket: DX-1201 Co-authored-by: llm-git <[email protected]>
1 parent 1b97451 commit cc61432

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

scripts/prepare-release.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,17 @@ import {
1212
changeScopeInFile,
1313
setDependencyVersion,
1414
DistTags,
15+
LernaModule,
1516
} from './prepareRelease';
1617

17-
async function setLernaModules(): Promise<{
18-
modules: string[];
19-
locations: string[];
20-
}> {
21-
const modules = await getLernaModules();
22-
return {
23-
modules: modules.map(({ name }) => name),
24-
locations: modules.map(({ location }) => location),
25-
};
26-
}
27-
28-
function replacePackageScopes(rootDir: string, lernaModules: string[], targetScope: string): number {
18+
function replacePackageScopes(rootDir: string, lernaModules: LernaModule[], targetScope: string): number {
2919
let filesChanged = 0;
3020
// replace all @bitgo packages & source code with alternate SCOPE
3121
const filePaths = [...walk(path.join(rootDir, 'modules')), ...walk(path.join(rootDir, 'webpack'))];
22+
const moduleNames = lernaModules.map(({ name }) => name);
23+
3224
filePaths.forEach((file) => {
33-
filesChanged += changeScopeInFile(file, lernaModules, targetScope);
25+
filesChanged += changeScopeInFile(file, moduleNames, targetScope);
3426
});
3527
return filesChanged;
3628
}
@@ -48,17 +40,18 @@ function replaceBitGoPackageScope(rootDir: string, targetScope: string): void {
4840
* Increment the version for a single module based on the preid.
4941
*
5042
* @param {String} preid - The prerelease identifier
51-
* @param {String} modulePath - The location of the module to update
43+
* @param {LernaModule} module - The module to update
5244
* @param {DistTags|undefined} tags - The dist tags for the module
53-
* @param {String[]} moduleLocations - All module locations for dependency updates
45+
* @param {LernaModule[]} allModules - All modules for dependency updates
5446
* @returns {String|undefined} - The new version if set, undefined otherwise
5547
*/
5648
function incrementVersionsForModuleLocation(
5749
preid: string,
58-
modulePath: string,
50+
module: LernaModule,
5951
tags: DistTags | undefined,
60-
moduleLocations: string[]
52+
allModules: LernaModule[]
6153
): string | undefined {
54+
const modulePath = module.location;
6255
const json = JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' }));
6356

6457
let prevTag: string | undefined = undefined;
@@ -79,19 +72,22 @@ function incrementVersionsForModuleLocation(
7972
console.log(`Setting next version for ${json.name} to ${next}`);
8073
json.version = next;
8174
writeFileSync(path.join(modulePath, 'package.json'), JSON.stringify(json, null, 2) + '\n');
75+
8276
// 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) => {
77+
allModules.forEach((otherModule) => {
8478
// skip it for the current version
85-
if (otherModulePath === modulePath) {
79+
if (otherModule.location === modulePath) {
8680
return;
8781
}
88-
const otherJsonContent = readFileSync(path.join(otherModulePath, 'package.json'), { encoding: 'utf-8' });
82+
83+
const otherJsonContent = readFileSync(path.join(otherModule.location, 'package.json'), { encoding: 'utf-8' });
8984
if (otherJsonContent.includes(json.name)) {
9085
const otherJson = JSON.parse(otherJsonContent);
9186
setDependencyVersion(otherJson, json.name, next);
92-
writeFileSync(path.join(otherModulePath, 'package.json'), JSON.stringify(otherJson, null, 2) + '\n');
87+
writeFileSync(path.join(otherModule.location, 'package.json'), JSON.stringify(otherJson, null, 2) + '\n');
9388
}
9489
});
90+
9591
return next;
9692
}
9793
return undefined;
@@ -101,16 +97,18 @@ function incrementVersionsForModuleLocation(
10197
* increment the version based on the preid.
10298
*
10399
* @param {String} preid - The prerelease identifier
104-
* @param {String[]} moduleLocations - The locations of the modules to update
100+
* @param {LernaModule[]} lernaModules - The modules to update
105101
*/
106-
async function incrementVersions(preid: string, moduleLocations: string[]): Promise<void> {
102+
async function incrementVersions(preid: string, lernaModules: LernaModule[]): Promise<void> {
103+
const moduleLocations = lernaModules.map(({ location }) => location);
107104
const distTags = await getDistTagsForModuleLocations(moduleLocations);
108-
for (let i = 0; i < moduleLocations.length; i++) {
105+
106+
for (let i = 0; i < lernaModules.length; i++) {
109107
try {
110-
incrementVersionsForModuleLocation(preid, moduleLocations[i], distTags[i], moduleLocations);
108+
incrementVersionsForModuleLocation(preid, lernaModules[i], distTags[i], lernaModules);
111109
} catch (e) {
112110
// it's not necessarily a blocking error. Let lerna try and publish anyways
113-
console.warn(`Couldn't set next version for ${moduleLocations[i]}`, e);
111+
console.warn(`Couldn't set next version for ${lernaModules[i].name} at ${lernaModules[i].location}`, e);
114112
}
115113
}
116114
}
@@ -145,8 +143,8 @@ yargs(hideBin(process.argv))
145143
console.log(`Using prerelease identifier: ${preid}`);
146144

147145
try {
148-
// Get lerna modules
149-
const { modules: lernaModules, locations: lernaModuleLocations } = await setLernaModules();
146+
// Get lerna modules directly
147+
const lernaModules = await getLernaModules();
150148

151149
// Replace package scopes
152150
const filesChanged = replacePackageScopes(rootDir, lernaModules, targetScope);
@@ -155,7 +153,7 @@ yargs(hideBin(process.argv))
155153
replaceBitGoPackageScope(rootDir, targetScope);
156154

157155
// Increment versions
158-
await incrementVersions(preid, lernaModuleLocations);
156+
await incrementVersions(preid, lernaModules);
159157

160158
if (filesChanged) {
161159
console.log(`Successfully re-targeted ${filesChanged} files.`);

0 commit comments

Comments
 (0)