Skip to content

Commit 1d6e922

Browse files
refactor(scripts): improve dist tags fetch with Map implementation
Refocus getDistTagsForModuleNames to return a Map for more efficient lookups. Update getDistTagsForModuleLocations to use the new Map-based approach. These changes make the code more maintainable and performant. Issue: BTC-1933
1 parent fdde58b commit 1d6e922

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

scripts/prepareRelease/distTags.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,34 @@ export async function getDistTags(packageName: string): Promise<DistTags> {
1616
return response.json();
1717
}
1818

19-
export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<(DistTags | undefined)[]> {
20-
return Promise.all(
21-
moduleNames.map(async (moduleName) => {
22-
switch (moduleName) {
23-
case '@bitgo-beta/express':
24-
case '@bitgo-beta/web-demo':
25-
case '@bitgo-beta/sdk-test':
26-
console.warn(`Skipping ${moduleName} as it's not published to npm`);
27-
return undefined;
28-
}
29-
try {
30-
return await getDistTags(moduleName);
31-
} catch (e) {
32-
console.warn(`Failed to fetch dist tags for ${moduleName}`, e);
33-
return undefined;
34-
}
35-
})
19+
export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<Map<string, DistTags>> {
20+
return new Map(
21+
(
22+
await Promise.all(
23+
moduleNames.map(async (moduleName): Promise<[string, DistTags][]> => {
24+
switch (moduleName) {
25+
case '@bitgo-beta/express':
26+
case '@bitgo-beta/web-demo':
27+
case '@bitgo-beta/sdk-test':
28+
console.warn(`Skipping ${moduleName} as it's not published to npm`);
29+
return [];
30+
}
31+
try {
32+
return [[moduleName, await getDistTags(moduleName)]];
33+
} catch (e) {
34+
console.warn(`Failed to fetch dist tags for ${moduleName}`, e);
35+
return [];
36+
}
37+
})
38+
)
39+
).flat()
3640
);
3741
}
3842

3943
export async function getDistTagsForModuleLocations(moduleLocations: string[]): Promise<(DistTags | undefined)[]> {
40-
return getDistTagsForModuleNames(
41-
moduleLocations.map(
42-
(modulePath) => JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' })).name
43-
)
44+
const names: string[] = moduleLocations.map(
45+
(modulePath) => JSON.parse(readFileSync(path.join(modulePath, 'package.json'), { encoding: 'utf-8' })).name
4446
);
47+
const map = await getDistTagsForModuleNames(names);
48+
return names.map((name) => map.get(name));
4549
}

0 commit comments

Comments
 (0)