1- import { readFileSync } from 'fs' ;
1+ import { readFileSync , writeFileSync , existsSync } from 'fs' ;
22import * as path from 'path' ;
33
44export 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+
1924export 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
4372export async function getDistTagsForModuleLocations ( moduleLocations : string [ ] ) : Promise < ( DistTags | undefined ) [ ] > {
0 commit comments