Skip to content

Commit

Permalink
Dont remove assets still referenced by other recordings (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored May 28, 2024
1 parent 39676e3 commit b3c797a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-cars-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"replayio": patch
---

Assets (like source maps) still referenced by other recordings won't be removed prematurely when removing a recording
2 changes: 1 addition & 1 deletion packages/replayio/src/commands/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function record(url: string = "about:blank") {
}
}

const recordingsAfter = await getRecordings(processGroupId);
const recordingsAfter = getRecordings(processGroupId);

const nextCrashedRecordings: LocalRecording[] = [];
const nextRecordings: LocalRecording[] = [];
Expand Down
6 changes: 3 additions & 3 deletions packages/replayio/src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ registerCommand("remove")
.action(remove);

async function remove(shortIds: string[], { all = false }: { all?: boolean }) {
const allRecordings = await getRecordings();
const allRecordings = getRecordings();

if (allRecordings.length === 0) {
console.log("No recordings found");
Expand Down Expand Up @@ -47,10 +47,10 @@ async function remove(shortIds: string[], { all = false }: { all?: boolean }) {
console.log(printRecordings(selectedRecordings, { showHeaderRow: false }));

for (const recording of selectedRecordings) {
await removeFromDisk(recording.id);
removeFromDisk(recording.id);
}

const countAfter = (await getRecordings()).length;
const countAfter = getRecordings().length;

if (countAfter < countBefore) {
console.log("%s recording(s) deleted", countBefore - countAfter);
Expand Down
2 changes: 1 addition & 1 deletion packages/replayio/src/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function upload(
all?: boolean;
} = {}
) {
const recordings = await getRecordings();
const recordings = getRecordings();

let selectedRecordings: LocalRecording[] = [];
if (shortIds.length > 0) {
Expand Down
34 changes: 29 additions & 5 deletions packages/replayio/src/utils/recordings/removeFromDisk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@ import { recordingLogPath, recordingsPath } from "./config";
import { debug } from "./debug";
import { getRecordings } from "./getRecordings";
import { readRecordingLogLines } from "./readRecordingLogLines";
import { LogEntry, RECORDING_LOG_KIND } from "./types";
import { LocalRecording, LogEntry, RECORDING_LOG_KIND } from "./types";

function getAssetsUsageMap(recordings: LocalRecording[]) {
const usageMap: Record<string, number> = {};

for (const recording of recordings) {
for (const sourceMap of recording.metadata.sourceMaps) {
usageMap[sourceMap.path] ??= 0;
usageMap[sourceMap.path]++;

for (const originalSource of sourceMap.originalSources) {
usageMap[originalSource.path] ??= 0;
usageMap[originalSource.path]++;
}
}
}
return usageMap;
}

export function removeFromDisk(id?: string) {
if (id) {
Expand All @@ -13,15 +30,22 @@ export function removeFromDisk(id?: string) {
const recordings = getRecordings();
const recording = recordings.find(recording => recording.id.startsWith(id));
if (recording) {
const assetsUsageMap = getAssetsUsageMap(recordings);

const { metadata, path } = recording;

metadata.sourceMaps.forEach(sourceMap => {
debug("Removing recording source-map file %s", sourceMap.path);
removeSync(sourceMap.path);
if (assetsUsageMap[sourceMap.path] === 1) {
debug("Removing recording source-map file %s", sourceMap.path);
removeSync(sourceMap.path);
removeSync(sourceMap.path.replace(/\.map$/, ".lookup"));
}

sourceMap.originalSources.forEach(source => {
debug("Removing recording original source file %s", source.path);
removeSync(source.path);
if (assetsUsageMap[source.path] === 1) {
debug("Removing recording original source file %s", source.path);
removeSync(source.path);
}
});
});

Expand Down

0 comments on commit b3c797a

Please sign in to comment.