@@ -50,7 +50,17 @@ export interface CachedNpmInfoClient {
50
50
51
51
export async function withNpmCache < T > (
52
52
uncachedClient : UncachedNpmInfoClient ,
53
- cb : ( client : CachedNpmInfoClient ) => Promise < T > ,
53
+ cb : ( client : CachedNpmInfoClient ) => T ,
54
+ cacheDir ?: string
55
+ ) : Promise < T > ;
56
+ export async function withNpmCache < T > (
57
+ uncachedClient : undefined ,
58
+ cb : ( offline : Omit < CachedNpmInfoClient , "fetchAndCacheNpmInfo" > ) => T ,
59
+ cacheDir ?: string
60
+ ) : Promise < T > ;
61
+ export async function withNpmCache < T > (
62
+ uncachedClient : UncachedNpmInfoClient | undefined ,
63
+ cb : ( client : CachedNpmInfoClient ) => T ,
54
64
cacheDir = defaultCacheDir
55
65
) : Promise < T > {
56
66
const log = loggerWithErrors ( ) [ 0 ] ;
@@ -69,9 +79,12 @@ export async function withNpmCache<T>(
69
79
}
70
80
71
81
const res = await cb ( { getNpmInfoFromCache, fetchAndCacheNpmInfo } ) ;
72
- log . info ( "Writing npm cache." ) ;
73
- await ensureFile ( cacheFile ) ;
74
- await writeJson ( cacheFile , mapToRecord ( unroll , jsonFromNpmInfo ) ) ;
82
+ // Don't bother writing if there's no way we could have gone to the origin.
83
+ if ( uncachedClient ) {
84
+ log . info ( "Writing npm cache." ) ;
85
+ await ensureFile ( cacheFile ) ;
86
+ await writeJson ( cacheFile , mapToRecord ( unroll , jsonFromNpmInfo ) ) ;
87
+ }
75
88
return res ;
76
89
77
90
/** May return old info -- caller should check that this looks up-to-date. */
@@ -81,7 +94,7 @@ export async function withNpmCache<T>(
81
94
82
95
/** Call this when the result of getNpmInfoFromCache looks potentially out-of-date. */
83
96
async function fetchAndCacheNpmInfo ( packageName : string ) : Promise < NpmInfo | undefined > {
84
- const info = await uncachedClient . fetchNpmInfo ( packageName ) ;
97
+ const info = await uncachedClient ! . fetchNpmInfo ( packageName ) ;
85
98
if ( info ) {
86
99
unroll . set ( packageName , info ) ;
87
100
}
0 commit comments