1616
1717import { logger } from "@atomist/automation-client" ;
1818import { GitProject } from "@atomist/automation-client/project/git/GitProject" ;
19- import * as fs from "fs" ;
19+ import * as fs from "fs-extra " ;
2020import { promisify } from "util" ;
21- import { ProjectLoader , ProjectLoadingParameters , WithLoadedProject } from "../../spi/project/ProjectLoader" ;
21+ import {
22+ ProjectLoader ,
23+ ProjectLoadingParameters ,
24+ WithLoadedProject ,
25+ } from "../../spi/project/ProjectLoader" ;
2226import { CloningProjectLoader } from "./cloningProjectLoader" ;
2327import { cacheKeyForSha } from "./support/cacheKey" ;
2428import { LruCache } from "./support/LruCache" ;
@@ -33,12 +37,16 @@ export class CachingProjectLoader implements ProjectLoader {
3337
3438 public async doWithProject < T > ( params : ProjectLoadingParameters , action : WithLoadedProject < T > ) : Promise < T > {
3539 if ( ! params . readOnly ) {
36- logger . info ( "CachingProjectLoader: Forcing fresh clone for non readonly use of %j " , params . id ) ;
40+ logger . info ( "Forcing fresh clone for non readonly use of '%j' " , params . id ) ;
3741 const p = await save ( this . delegate , params ) ;
38- return action ( p ) ;
42+ return action ( p )
43+ . then ( result => {
44+ cleanUp ( p ) ;
45+ return result ;
46+ } ) ;
3947 }
4048
41- logger . debug ( "CachingProjectLoader: Hoping to reuse clone for readonly use of %j " , params . id ) ;
49+ logger . debug ( "Attempting to reuse clone for readonly use of '%j' " , params . id ) ;
4250 const key = cacheKeyForSha ( params . id ) ;
4351 let project = this . cache . get ( key ) ;
4452 if ( ! ! project ) {
@@ -47,27 +55,37 @@ export class CachingProjectLoader implements ProjectLoader {
4755 await promisify ( fs . access ) ( project . baseDir ) ;
4856 } catch {
4957 this . cache . evict ( key ) ;
50- logger . warn ( "CachingProjectLoader: Invalid cache entry %s " , key ) ;
58+ logger . warn ( "Invalid cache entry '%s' " , key ) ;
5159 project = undefined ;
5260 }
5361 }
5462
5563 if ( ! project ) {
5664 project = await save ( this . delegate , params ) ;
57- logger . info ( "Caching project %j " , project . id ) ;
65+ logger . info ( "Caching project '%j' " , project . id ) ;
5866 this . cache . put ( key , project ) ;
5967 }
6068
61- logger . debug ( "CachingProjectLoader: About to invoke action. Cache stats: %j" , this . cache . stats ) ;
69+ logger . debug ( "About to invoke action. Cache stats: %j" , this . cache . stats ) ;
6270 return action ( project ) ;
6371 }
6472
6573 constructor (
6674 private readonly delegate : ProjectLoader = CloningProjectLoader ,
6775 maxEntries : number = 20 ) {
68- this . cache = new LruCache < GitProject > ( maxEntries ) ;
76+ this . cache = new LruCache < GitProject > ( maxEntries , cleanUp ) ;
6977 }
78+ }
7079
80+ function cleanUp ( p : GitProject ) : void {
81+ logger . debug ( `Evicting project '%j'` , p . id ) ;
82+ if ( p . baseDir && fs . accessSync ( p . baseDir ) ) {
83+ try {
84+ fs . removeSync ( p . baseDir ) ;
85+ } catch ( err ) {
86+ logger . warn ( err ) ;
87+ }
88+ }
7189}
7290
7391export function save ( pl : ProjectLoader , params : ProjectLoadingParameters ) : Promise < GitProject > {
0 commit comments