Skip to content

Commit e18a427

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(scripts): improve module handling in release script
Abstract file operations into helper functions and simplify data flow. Use module objects consistently instead of paths and improve iterations. Issue: DX-1201 Co-authored-by: llm-git <[email protected]>
1 parent cc61432 commit e18a427

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

scripts/prepare-release.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { hideBin } from 'yargs/helpers';
77

88
import {
99
walk,
10-
getDistTagsForModuleLocations,
10+
getDistTagsForModules,
1111
getLernaModules,
1212
changeScopeInFile,
1313
setDependencyVersion,
@@ -36,6 +36,24 @@ function replaceBitGoPackageScope(rootDir: string, targetScope: string): void {
3636
writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(json, null, 2) + '\n');
3737
}
3838

39+
/**
40+
* Read package.json for a module
41+
* @param module The module to read package.json from
42+
* @returns The parsed package.json content
43+
*/
44+
function readModulePackageJson(module: LernaModule): any {
45+
return JSON.parse(readFileSync(path.join(module.location, 'package.json'), { encoding: 'utf-8' }));
46+
}
47+
48+
/**
49+
* Write package.json for a module
50+
* @param module The module to write package.json to
51+
* @param json The content to write
52+
*/
53+
function writeModulePackageJson(module: LernaModule, json: any): void {
54+
writeFileSync(path.join(module.location, 'package.json'), JSON.stringify(json, null, 2) + '\n');
55+
}
56+
3957
/**
4058
* Increment the version for a single module based on the preid.
4159
*
@@ -51,8 +69,7 @@ function incrementVersionsForModuleLocation(
5169
tags: DistTags | undefined,
5270
allModules: LernaModule[]
5371
): string | undefined {
54-
const modulePath = module.location;
55-
const json = JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' }));
72+
const json = readModulePackageJson(module);
5673

5774
let prevTag: string | undefined = undefined;
5875

@@ -71,20 +88,23 @@ function incrementVersionsForModuleLocation(
7188
assert(typeof next === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`);
7289
console.log(`Setting next version for ${json.name} to ${next}`);
7390
json.version = next;
74-
writeFileSync(path.join(modulePath, 'package.json'), JSON.stringify(json, null, 2) + '\n');
91+
writeModulePackageJson(module, json);
7592

7693
// since we're manually setting new versions, we must also reconcile all other lerna packages to use the 'next' version for this module
7794
allModules.forEach((otherModule) => {
7895
// skip it for the current version
79-
if (otherModule.location === modulePath) {
96+
if (otherModule.location === module.location) {
8097
return;
8198
}
8299

83-
const otherJsonContent = readFileSync(path.join(otherModule.location, 'package.json'), { encoding: 'utf-8' });
84-
if (otherJsonContent.includes(json.name)) {
85-
const otherJson = JSON.parse(otherJsonContent);
100+
// Use readModulePackageJson here instead of direct readFileSync
101+
const otherJson = readModulePackageJson(otherModule);
102+
103+
// Check if this module depends on the one we're updating
104+
const otherJsonString = JSON.stringify(otherJson);
105+
if (otherJsonString.includes(json.name)) {
86106
setDependencyVersion(otherJson, json.name, next);
87-
writeFileSync(path.join(otherModule.location, 'package.json'), JSON.stringify(otherJson, null, 2) + '\n');
107+
writeModulePackageJson(otherModule, otherJson);
88108
}
89109
});
90110

@@ -100,15 +120,14 @@ function incrementVersionsForModuleLocation(
100120
* @param {LernaModule[]} lernaModules - The modules to update
101121
*/
102122
async function incrementVersions(preid: string, lernaModules: LernaModule[]): Promise<void> {
103-
const moduleLocations = lernaModules.map(({ location }) => location);
104-
const distTags = await getDistTagsForModuleLocations(moduleLocations);
123+
const distTags = await getDistTagsForModules(lernaModules);
105124

106-
for (let i = 0; i < lernaModules.length; i++) {
125+
for (const m of lernaModules) {
107126
try {
108-
incrementVersionsForModuleLocation(preid, lernaModules[i], distTags[i], lernaModules);
127+
incrementVersionsForModuleLocation(preid, m, distTags.get(m), lernaModules);
109128
} catch (e) {
110129
// it's not necessarily a blocking error. Let lerna try and publish anyways
111-
console.warn(`Couldn't set next version for ${lernaModules[i].name} at ${lernaModules[i].location}`, e);
130+
console.warn(`Couldn't set next version for ${m.name} at ${m.location}`, e);
112131
}
113132
}
114133
}

scripts/prepareRelease/distTags.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFileSync, writeFileSync, existsSync } from 'fs';
22
import * as path from 'path';
3+
import { LernaModule } from './getLernaModules';
34

45
export type DistTags = Record<string, string>;
56

@@ -69,10 +70,10 @@ export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<
6970
return tagsMap;
7071
}
7172

72-
export async function getDistTagsForModuleLocations(moduleLocations: string[]): Promise<(DistTags | undefined)[]> {
73-
const names: string[] = moduleLocations.map(
74-
(modulePath) => JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' })).name
73+
export async function getDistTagsForModules(modules: LernaModule[]): Promise<Map<LernaModule, DistTags | undefined>> {
74+
const names: string[] = modules.map(
75+
(m) => JSON.parse(readFileSync(path.join(m.location, 'package.json'), { encoding: 'utf-8' })).name
7576
);
76-
const map = await getDistTagsForModuleNames(names);
77-
return names.map((name) => map.get(name));
77+
const nameMap = await getDistTagsForModuleNames(names);
78+
return new Map<LernaModule, DistTags | undefined>(modules.map((m, i) => [m, nameMap.get(names[i])]));
7879
}

0 commit comments

Comments
 (0)