Skip to content

Commit fabdd7e

Browse files
committed
add and call project context cleanup method
1 parent c53914a commit fabdd7e

File tree

10 files changed

+66
-33
lines changed

10 files changed

+66
-33
lines changed

src/command/preview/preview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ export async function renderForPreview(
485485
[],
486486
));
487487

488+
renderResult.context.cleanup();
489+
488490
return {
489491
file,
490492
format: renderResult.files[0].format,

src/command/render/cmd.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ export const renderCommand = new Command()
245245
const services = renderServices(notebookContext());
246246
try {
247247
renderResultInput = relative(Deno.cwd(), walk.path) || ".";
248+
if (renderResult) {
249+
renderResult.context.cleanup();
250+
}
248251
renderResult = await render(renderResultInput, {
249252
services,
250253
flags,
@@ -255,6 +258,7 @@ export const renderCommand = new Command()
255258

256259
// check for error
257260
if (renderResult.error) {
261+
renderResult.context.cleanup();
258262
throw renderResult.error;
259263
}
260264
} finally {
@@ -275,6 +279,10 @@ export const renderCommand = new Command()
275279
if (finalOutput) {
276280
info("Output created: " + finalOutput + "\n");
277281
}
282+
283+
if (renderResult) {
284+
renderResult.context.cleanup();
285+
}
278286
}
279287
} else {
280288
throw new Error(`No valid input files passed to render`);

src/command/render/render-shared.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { kTextPlain } from "../../core/mime.ts";
3333
import { normalizePath } from "../../core/path.ts";
3434
import { notebookContext } from "../../render/notebook/notebook-context.ts";
3535
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";
36+
import { assert } from "testing/asserts";
3637

3738
export async function render(
3839
path: string,
@@ -95,6 +96,7 @@ export async function render(
9596
// validate that we didn't get any project-only options
9697
validateDocumentRenderFlags(options.flags);
9798

99+
assert(!context, "Expected no context here");
98100
// NB: singleFileProjectContext is currently not fully-featured
99101
context = await singleFileProjectContext(path, nbContext, options.flags);
100102

src/command/render/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export interface RenderResourceFiles {
102102
}
103103

104104
export interface RenderResult {
105-
context?: ProjectContext;
105+
context: ProjectContext;
106106
baseDir?: string;
107107
outputDir?: string;
108108
files: RenderResultFile[];

src/project/project-context.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ export async function projectContext(
310310
isSingleFile: false,
311311
diskCache: await createProjectCache(join(dir, ".quarto")),
312312
temp,
313+
cleanup: () => {
314+
result.diskCache.close();
315+
},
313316
};
314317

315318
// see if the project [kProjectType] wants to filter the project config
@@ -397,6 +400,9 @@ export async function projectContext(
397400
isSingleFile: false,
398401
diskCache: await createProjectCache(join(dir, ".quarto")),
399402
temp,
403+
cleanup: () => {
404+
result.diskCache.close();
405+
},
400406
};
401407
const { files, engines } = await projectInputFiles(
402408
result,
@@ -466,6 +472,9 @@ export async function projectContext(
466472
isSingleFile: false,
467473
diskCache: await createProjectCache(join(temp.baseDir, ".quarto")),
468474
temp,
475+
cleanup: () => {
476+
context.diskCache.close();
477+
},
469478
};
470479
if (Deno.statSync(path).isDirectory) {
471480
const { files, engines } = await projectInputFiles(context);

src/project/serve/serve.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ function previewControlChannelRequestHandler(
824824
})
825825
).then((result) => {
826826
if (result.error) {
827+
result.context.cleanup();
827828
renderManager.onRenderError(result.error);
828829
} else {
829830
// print output created
@@ -844,6 +845,7 @@ function previewControlChannelRequestHandler(
844845
resourceFiles,
845846
watcher.project(),
846847
);
848+
result.context.cleanup();
847849

848850
info("Output created: " + finalOutput + "\n");
849851

src/project/serve/watch.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -178,40 +178,42 @@ export function watchProject(
178178
});
179179

180180
if (result.error) {
181+
result.context.cleanup();
181182
renderManager.onRenderError(result.error);
182183
return undefined;
183-
} else {
184-
// record rendered hash
185-
for (const input of inputs.filter(existsSync1)) {
186-
rendered.set(input, md5Hash(Deno.readTextFileSync(input)));
187-
}
188-
renderManager.onRenderResult(
189-
result,
190-
extensionDirs,
191-
resourceFiles,
192-
project!,
193-
);
194-
195-
// Filter out supplmental files (e.g. files that were injected as supplements)
196-
// to the render. Instead, we should return the first non-supplemental file.
197-
// Example of supplemental file is a user rendering a post that appears in a listing
198-
// - the listing will be added as a supplement since changes in the post may change the
199-
// listing itself
200-
const nonSupplementalFiles = result.files.filter(
201-
(renderResultFile) => {
202-
return !renderResultFile.supplemental;
203-
},
204-
);
205-
206-
return {
207-
config: false,
208-
output: true,
209-
reloadTarget: (nonSupplementalFiles.length &&
210-
!isPdfContent(nonSupplementalFiles[0].file))
211-
? join(outputDir, nonSupplementalFiles[0].file)
212-
: undefined,
213-
};
214184
}
185+
186+
// record rendered hash
187+
for (const input of inputs.filter(existsSync1)) {
188+
rendered.set(input, md5Hash(Deno.readTextFileSync(input)));
189+
}
190+
renderManager.onRenderResult(
191+
result,
192+
extensionDirs,
193+
resourceFiles,
194+
project!,
195+
);
196+
197+
// Filter out supplmental files (e.g. files that were injected as supplements)
198+
// to the render. Instead, we should return the first non-supplemental file.
199+
// Example of supplemental file is a user rendering a post that appears in a listing
200+
// - the listing will be added as a supplement since changes in the post may change the
201+
// listing itself
202+
const nonSupplementalFiles = result.files.filter(
203+
(renderResultFile) => {
204+
return !renderResultFile.supplemental;
205+
},
206+
);
207+
result.context.cleanup();
208+
209+
return {
210+
config: false,
211+
output: true,
212+
reloadTarget: (nonSupplementalFiles.length &&
213+
!isPdfContent(nonSupplementalFiles[0].file))
214+
? join(outputDir, nonSupplementalFiles[0].file)
215+
: undefined,
216+
};
215217
} finally {
216218
services.cleanup();
217219
}

src/project/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ export interface ProjectContext {
116116

117117
diskCache: ProjectCache;
118118
temp: TempContext;
119+
120+
cleanup: () => void;
119121
}
120122

121123
export interface ProjectFiles {

src/project/types/single-file/single-file.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ export async function singleFileProjectContext(
7777
isSingleFile: true,
7878
diskCache: await createProjectCache(projectCacheBaseDir),
7979
temp,
80+
cleanup: () => {
81+
result.diskCache.close();
82+
},
8083
};
81-
temp.onCleanup(() => result.diskCache.close());
84+
temp.onCleanup(result.cleanup);
8285
return result;
8386
}

src/publish/publish.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export async function publishSite(
8888
{},
8989
);
9090

91+
result.context.cleanup();
9192
if (result.error) {
9293
throw result.error;
9394
}
@@ -169,6 +170,7 @@ export async function publishDocument(
169170
flags,
170171
});
171172
if (result.error) {
173+
result.context.cleanup();
172174
throw result.error;
173175
}
174176

@@ -258,6 +260,7 @@ export async function publishDocument(
258260
relBasePath,
259261
);
260262
}
263+
result.context.cleanup();
261264

262265
return normalizePublishFiles({
263266
baseDir: finalBaseDir,

0 commit comments

Comments
 (0)