Skip to content

Commit a6c6d5c

Browse files
committed
inspect - cleanup project and project transients
1 parent 117d2a6 commit a6c6d5c

File tree

3 files changed

+174
-151
lines changed

3 files changed

+174
-151
lines changed

src/project/project-context.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { ExecutionEngine, kMarkdownEngine } from "../execute/types.ts";
6969
import { projectResourceFiles } from "./project-resources.ts";
7070

7171
import {
72+
cleanupFileInformationCache,
7273
ignoreFieldsForProjectType,
7374
normalizeFormatYaml,
7475
projectConfigFile,
@@ -267,6 +268,7 @@ export async function projectContext(
267268
const temp = createTempContext({
268269
dir: join(dir, ".quarto", "temp"),
269270
});
271+
const fileInformationCache = new Map();
270272
const result: ProjectContext = {
271273
resolveBrand: async (fileName?: string) =>
272274
projectResolveBrand(result, fileName),
@@ -286,7 +288,7 @@ export async function projectContext(
286288
},
287289
dir,
288290
engines: [],
289-
fileInformationCache: new Map(),
291+
fileInformationCache,
290292
files: {
291293
input: [],
292294
},
@@ -312,6 +314,7 @@ export async function projectContext(
312314
diskCache: await createProjectCache(join(dir, ".quarto")),
313315
temp,
314316
cleanup: () => {
317+
cleanupFileInformationCache(result);
315318
result.diskCache.close();
316319
},
317320
};
@@ -359,6 +362,7 @@ export async function projectContext(
359362
const temp = createTempContext({
360363
dir: join(dir, ".quarto", "temp"),
361364
});
365+
const fileInformationCache = new Map();
362366
const result: ProjectContext = {
363367
resolveBrand: async (fileName?: string) =>
364368
projectResolveBrand(result, fileName),
@@ -379,7 +383,7 @@ export async function projectContext(
379383
dir,
380384
config: projectConfig,
381385
engines: [],
382-
fileInformationCache: new Map(),
386+
fileInformationCache,
383387
files: {
384388
input: [],
385389
},
@@ -402,6 +406,7 @@ export async function projectContext(
402406
diskCache: await createProjectCache(join(dir, ".quarto")),
403407
temp,
404408
cleanup: () => {
409+
cleanupFileInformationCache(result);
405410
result.diskCache.close();
406411
},
407412
};
@@ -427,6 +432,7 @@ export async function projectContext(
427432
configResolvers.shift();
428433
} else if (force) {
429434
const temp = globalTempContext();
435+
const fileInformationCache = new Map();
430436
const context: ProjectContext = {
431437
resolveBrand: async (fileName?: string) =>
432438
projectResolveBrand(context, fileName),
@@ -451,7 +457,7 @@ export async function projectContext(
451457
[kProjectOutputDir]: flags?.outputDir,
452458
},
453459
},
454-
fileInformationCache: new Map(),
460+
fileInformationCache,
455461
files: {
456462
input: [],
457463
},
@@ -474,6 +480,7 @@ export async function projectContext(
474480
diskCache: await createProjectCache(join(temp.baseDir, ".quarto")),
475481
temp,
476482
cleanup: () => {
483+
cleanupFileInformationCache(context);
477484
context.diskCache.close();
478485
},
479486
};

src/project/project-shared.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2020-2022 Posit Software, PBC
55
*/
66

7-
import { existsSync } from "../deno_ral/fs.ts";
7+
import { existsSync, safeRemoveSync } from "../deno_ral/fs.ts";
88
import {
99
dirname,
1010
isAbsolute,
@@ -582,3 +582,27 @@ export async function projectResolveBrand(
582582
}
583583
}
584584
}
585+
586+
export function cleanupFileInformationCache(project: ProjectContext) {
587+
project.fileInformationCache.forEach((entry) => {
588+
if (entry?.target?.data) {
589+
const data = entry.target.data as {
590+
transient?: boolean;
591+
};
592+
if (data.transient && entry.target?.input) {
593+
safeRemoveSync(entry.target?.input);
594+
}
595+
}
596+
});
597+
}
598+
599+
export async function withProjectCleanup<T>(
600+
project: ProjectContext,
601+
fn: (project: ProjectContext) => Promise<T>,
602+
): Promise<T> {
603+
try {
604+
return await fn(project);
605+
} finally {
606+
project.cleanup();
607+
}
608+
}

0 commit comments

Comments
 (0)