Skip to content

Commit 893435c

Browse files
committed
Cleanup old Android APKs after each update
1 parent 00d28fc commit 893435c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/interceptors/android/android-adb-interceptor.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
stringAsStream
2222
} from './adb-commands';
2323
import { reportError } from '../../error-tracking';
24-
import { readDir, createTmp, renameFile } from '../../util';
24+
import { readDir, createTmp, renameFile, deleteFile } from '../../util';
2525

2626
function 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(/^httptoolkit-(.*).apk$/))
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

128150
async function streamLatestApk(config: HtkConfig): Promise<stream.Readable> {

0 commit comments

Comments
 (0)