Skip to content

Commit

Permalink
centralized cleanup handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Apr 30, 2022
1 parent 1615508 commit ccef322
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
25 changes: 25 additions & 0 deletions src/core/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* cleanup.ts
*
* Copyright (C) 2020 by RStudio, PBC
*
*/

import { info } from "log/mod.ts";

const cleanupHandlers: VoidFunction[] = [];

export function onCleanup(handler: VoidFunction) {
cleanupHandlers.push(handler);
}

export function exitWithCleanup(code: number) {
for (const handler of cleanupHandlers) {
try {
handler();
} catch (error) {
info("Error occurred during cleanup: " + error);
}
}
Deno.exit(code);
}
17 changes: 13 additions & 4 deletions src/core/quarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import { existsSync } from "fs/exists.ts";
import { extname, join } from "path/mod.ts";
import { info } from "log/mod.ts";
import * as colors from "fmt/colors.ts";

import { getenv } from "./env.ts";
import { exitWithCleanup } from "./cleanup.ts";

export const kLocalDevelopment = "99.9.9";

Expand All @@ -33,7 +35,7 @@ export const quartoConfig = {
},
};

export function monitorQuartoSrcChanges(cleanup: VoidFunction) {
export function monitorQuartoSrcChanges(cleanup?: VoidFunction) {
if (quartoConfig.isDebug()) {
const srcDir = Deno.realPathSync(
join(quartoConfig.binPath(), "../../../src"),
Expand All @@ -42,9 +44,16 @@ export function monitorQuartoSrcChanges(cleanup: VoidFunction) {
const watchForChanges = async () => {
for await (const event of watcher) {
if (event.paths.some((path) => extname(path).toLowerCase() === ".ts")) {
info("quarto src code changed: preview terminating");
cleanup();
Deno.exit(1);
info(
colors.bold(
colors.blue("\nquarto src code changed: preview terminating\n"),
),
);

if (cleanup) {
cleanup();
}
exitWithCleanup(1);
}
}
};
Expand Down
7 changes: 3 additions & 4 deletions src/project/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import { createTempContext, TempContext } from "../../core/temp.ts";
import { ServeRenderManager } from "./render.ts";
import { projectScratchPath } from "../project-scratch.ts";
import { monitorQuartoSrcChanges } from "../../core/quarto.ts";
import { onCleanup } from "../../core/cleanup.ts";

export const kRenderNone = "none";
export const kRenderDefault = "default";
Expand Down Expand Up @@ -126,7 +127,7 @@ export async function serveProject(
acquirePreviewLock(project);

// monitor the src dir
monitorQuartoSrcChanges(() => releasePreviewLock(project!));
monitorQuartoSrcChanges();

// clear the project index
clearProjectIndex(project.dir);
Expand Down Expand Up @@ -558,9 +559,7 @@ function acquirePreviewLock(project: ProjectContext) {
Deno.writeTextFileSync(lockfile, String(Deno.pid));

// rmeove the lockfile when we exit
addEventListener("unload", () => {
releasePreviewLock(project);
});
onCleanup(() => releasePreviewLock(project));
}

function releasePreviewLock(project: ProjectContext) {
Expand Down
11 changes: 4 additions & 7 deletions src/quarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
readSourceDevConfig,
reconfigureQuarto,
} from "./core/devconfig.ts";
import { exitWithCleanup, onCleanup } from "./core/cleanup.ts";

import { parse } from "flags/mod.ts";
import { runScript } from "./command/run/run.ts";
Expand Down Expand Up @@ -98,6 +99,7 @@ export async function quarto(

// init temp dir
initSessionTempDir();
onCleanup(cleanupSessionTempDir);

await quartoCommand.command("help", new HelpCommand().global())
.command("completions", new CompletionsCommand()).hidden().parse(args);
Expand All @@ -124,7 +126,7 @@ if (import.meta.main) {
await cleanupLogger();

// exit
Deno.exit(0);
exitWithCleanup(0);
} catch (e) {
if (e) {
logError(e);
Expand All @@ -134,10 +136,5 @@ if (import.meta.main) {
}

function abend() {
cleanup();
Deno.exit(1);
}

function cleanup() {
cleanupSessionTempDir();
exitWithCleanup(1);
}

0 comments on commit ccef322

Please sign in to comment.