Skip to content

Commit 3ae6c72

Browse files
OttoAllmendingerllm-git
andcommitted
feat(scripts): implement dist-tags caching for prepare-release
Add caching to improve speed of dist-tags fetch. Enable cache path via an environment variable. Cache results in JSON format. Issue: DX-1201 Co-authored-by: llm-git <[email protected]>
1 parent ada3f8a commit 3ae6c72

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

scripts/prepareRelease/distTags.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync } from 'fs';
1+
import { readFileSync, writeFileSync, existsSync } from 'fs';
22
import * as path from 'path';
33

44
export type DistTags = Record<string, string>;
@@ -16,8 +16,24 @@ export async function getDistTags(packageName: string): Promise<DistTags> {
1616
return response.json();
1717
}
1818

19+
// Add this function to read from cache
20+
export function getDistTagsCache(): string | undefined {
21+
return process.env.BITGO_PREPARE_RELEASE_CACHE_DIST_TAGS;
22+
}
23+
1924
export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<Map<string, DistTags>> {
20-
return new Map(
25+
const cachePath = getDistTagsCache();
26+
27+
// If cache path is set and file exists, read from cache
28+
if (cachePath && existsSync(cachePath)) {
29+
console.log(`Reading dist tags from cache: ${cachePath}`);
30+
const cacheContent = readFileSync(cachePath, { encoding: 'utf-8' });
31+
const cachedTags = JSON.parse(cacheContent);
32+
return new Map(Object.entries(cachedTags));
33+
}
34+
35+
// Otherwise fetch from npm
36+
const tagsMap = new Map(
2137
(
2238
await Promise.all(
2339
moduleNames.map(async (moduleName): Promise<[string, DistTags][]> => {
@@ -38,6 +54,19 @@ export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<
3854
)
3955
).flat()
4056
);
57+
58+
// If cache path is set but file doesn't exist, write to cache
59+
if (cachePath) {
60+
console.log(`Writing dist tags to cache: ${cachePath}`);
61+
const cacheDir = path.dirname(cachePath);
62+
if (!existsSync(cacheDir)) {
63+
const { mkdirSync } = require('fs');
64+
mkdirSync(cacheDir, { recursive: true });
65+
}
66+
writeFileSync(cachePath, JSON.stringify(Object.fromEntries(tagsMap), null, 2), { encoding: 'utf-8' });
67+
}
68+
69+
return tagsMap;
4170
}
4271

4372
export async function getDistTagsForModuleLocations(moduleLocations: string[]): Promise<(DistTags | undefined)[]> {

0 commit comments

Comments
 (0)