-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathmain.ts
48 lines (41 loc) · 1.23 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* 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 { exitWithCleanup } from "./cleanup.ts";
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 runner(args);
// 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));
}
exitWithCleanup(0);
} catch (e) {
if (e) {
logError(e);
}
} finally {
abend();
}
}
function abend() {
exitWithCleanup(1);
}