@@ -21,7 +21,7 @@ import {
2121 stringAsStream
2222} from './adb-commands' ;
2323import { reportError } from '../../error-tracking' ;
24- import { readDir , createTmp , renameFile } from '../../util' ;
24+ import { readDir , createTmp , renameFile , deleteFile } from '../../util' ;
2525
2626function urlSafeBase64 ( content : string ) {
2727 return Buffer . from ( content , 'utf8' ) . toString ( 'base64' )
@@ -123,6 +123,28 @@ async function updateLocalApk(
123123
124124 await renameFile ( tmpApk , path . join ( config . configPath , `httptoolkit-${ version } .apk` ) ) ;
125125 console . log ( `Local APK moved to ${ path . join ( config . configPath , `httptoolkit-${ version } .apk` ) } ` ) ;
126+ await cleanupLocalApks ( config ) ;
127+ }
128+
129+ // Delete all but the most recent APK version in the config directory.
130+ async function cleanupLocalApks ( config : HtkConfig ) {
131+ const apks = ( await readDir ( config . configPath ) )
132+ . map ( filename => filename . match ( / ^ h t t p t o o l k i t - ( .* ) .a p k $ / ) )
133+ . filter ( ( match ) : match is RegExpMatchArray => ! ! match )
134+ . map ( ( match ) => ( {
135+ path : path . join ( config . configPath , match [ 0 ] ) ,
136+ version : match [ 1 ]
137+ } ) ) ;
138+
139+ apks . sort ( ( apk1 , apk2 ) => {
140+ return - 1 * semver . compare ( apk1 . version , apk2 . version ) ;
141+ } ) ;
142+
143+ console . log ( `Deleting old APKs: ${ apks . slice ( 1 ) . map ( apk => apk . path ) . join ( ', ' ) } ` ) ;
144+
145+ return Promise . all (
146+ apks . slice ( 1 ) . map ( apk => deleteFile ( apk . path ) )
147+ ) ;
126148}
127149
128150async function streamLatestApk ( config : HtkConfig ) : Promise < stream . Readable > {
0 commit comments