Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main - use mainRunner and existing cleanup #8819

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions src/command/render/latexmk/quarto-latexmk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
kExeName,
kExeVersion,
} from "./quarto-latexmk-metadata.ts";
import { exitWithCleanup } from "../../../core/cleanup.ts";
import { mainRunner } from "../../../core/main.ts";

interface EngineOpts {
pdf: string[];
Expand Down Expand Up @@ -124,26 +126,9 @@ export async function pdf(args: string[]) {
}

if (import.meta.main) {
try {
// Parse the raw args to read globals and initialize logging
// const args = parse(Deno.args);
const args = parse(Deno.args);
await initializeLogger(logOptions(args));

// install termination signal handlers
if (Deno.build.os !== "windows") {
Deno.addSignalListener("SIGINT", cleanup);
Deno.addSignalListener("SIGTERM", cleanup);
}

await mainRunner(async () => {
await pdf(Deno.args);
} catch (e) {
if (e) {
logError(e);
}
} finally {
cleanup();
}
});
}

function mkOptions(
Expand Down Expand Up @@ -186,8 +171,3 @@ function bibEngine(bibEngine?: string): "biblatex" | "natbib" {
return "biblatex";
}
}

function cleanup() {
cleanupLogger();
Deno.exit(1);
}
3 changes: 3 additions & 0 deletions src/core/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Args } from "flags/mod.ts";
import { lines } from "./text.ts";
import { debug, error, getLogger, setup, warning } from "log/mod.ts";
import { asErrorEx, InternalError } from "./lib/error.ts";
import { onCleanup } from "./cleanup.ts";

export type LogLevel = "DEBUG" | "INFO" | "WARNING" | "ERROR";

Expand Down Expand Up @@ -285,6 +286,8 @@ export async function initializeLogger(logOptions: LogOptions) {
},
},
});

onCleanup(cleanupLogger);
}

export async function cleanupLogger() {
Expand Down
74 changes: 26 additions & 48 deletions src/core/main.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,48 @@
/*
* main.ts
*
* Utilities for main() functions (setup, cleanup, etc)
*
* Copyright (C) 2022 Posit Software, PBC
*
*/

import {
cleanupLogger,
initializeLogger,
logError,
logOptions,
} from "../../src/core/log.ts";

* main.ts
*
* Utilities for main() functions (setup, cleanup, etc)
*
* Copyright (C) 2022 Posit Software, PBC
*/

import { initializeLogger, logError, logOptions } from "../../src/core/log.ts";
import { Args } from "flags/mod.ts";
import { parse } from "flags/mod.ts";
import { cleanupSessionTempDir } from "./temp.ts";
// import { cleanupEsbuild } from "./esbuild.ts";

// const cleanupHandlers: (() => undefined)[] = [];
import { exitWithCleanup } from "./cleanup.ts";

// export function addCleanupHandler(handler: () => undefined) {
// cleanupHandlers.push(handler);
// }

export async function mainRunner(
runner: (() => Promise<unknown>),
): Promise<unknown> {
type Runner = (args: Args) => Promise<unknown>;
export async function mainRunner(runner: Runner) {
try {
// Parse the raw args to read globals and initialize logging
const args = parse(Deno.args);
await initializeLogger(logOptions(args));

// install termination signal handlers
if (Deno.build.os !== "windows") {
Deno.addSignalListener("SIGINT", abend);
Deno.addSignalListener("SIGTERM", abend);
}

await initializeLogger(logOptions(parse(Deno.args)));
await runner(args);

await runner();

await cleanupLogger();
cleanup();
// if profiling, wait for 10 seconds before quitting
if (Deno.env.get("QUARTO_TS_PROFILE") !== undefined) {
console.log("Program finished. Turn off the Chrome profiler now!");
console.log("Waiting for 10 seconds ...");
await new Promise((resolve) => setTimeout(resolve, 10000));
}

// exit
Deno.exit(0);
exitWithCleanup(0);
} catch (e) {
if (e) {
logError(e);
}
} finally {
abend();
}

// we never get here because of Deno.exit() on both sides
// of the try{} clause, but the typescript compiler doesn't
// like an async function without a return statement
return undefined;
}

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

function cleanup() {
cleanupSessionTempDir();
// cleanupEsbuild();
// for (const handler of cleanupHandlers) {
// handler();
// }
exitWithCleanup(1);
}
2 changes: 2 additions & 0 deletions src/core/quarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { onActiveProfileChanged } from "../project/project-profile.ts";
import { onDotenvChanged } from "../quarto-core/dotenv.ts";
import { normalizePath } from "./path.ts";
import { buildQuartoPreviewJs } from "./previewjs.ts";
import { parse } from "flags/mod.ts";
import { initializeLogger, logError, logOptions } from "./log.ts";

export const kLocalDevelopment = "99.9.9";

Expand Down
43 changes: 3 additions & 40 deletions src/quarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import "./project/types/register.ts";
import "./format/imports.ts";

import { kCliffyImplicitCwd } from "./config/constants.ts";
import { mainRunner } from "./core/main.ts";

export async function quarto(
args: string[],
Expand Down Expand Up @@ -156,24 +157,7 @@ export async function quarto(
}

if (import.meta.main) {
// we'd like to do this:
//
// await mainRunner(() => quarto(Deno.args, appendLogOptions));
//
// but it presently causes the bundler to generate bad JS.
try {
// install termination signal handlers
if (Deno.build.os !== "windows") {
Deno.addSignalListener("SIGINT", abend);
Deno.addSignalListener("SIGTERM", abend);
}

// parse args
const args = parse(Deno.args);

// initialize logger
await initializeLogger(logOptions(args));

await mainRunner(async (args) => {
// initialize profile (remove from args)
let quartoArgs = [...Deno.args];
if (setProfileFromArg(args)) {
Expand All @@ -187,26 +171,5 @@ if (import.meta.main) {
cmd = appendLogOptions(cmd);
return appendProfileArg(cmd);
});

await cleanupLogger();

// if profiling, wait for 10 seconds before quitting
if (Deno.env.get("QUARTO_TS_PROFILE") !== undefined) {
console.log("Program finished. Turn off the Chrome profiler now!");
console.log("Waiting for 10 seconds ...");
await new Promise((resolve) => setTimeout(resolve, 10000));
}

// exit
exitWithCleanup(0);
} catch (e) {
if (e) {
logError(e);
}
abend();
}
}

function abend() {
exitWithCleanup(1);
});
}
Loading