-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathquarto.ts
168 lines (143 loc) · 4.61 KB
/
quarto.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* quarto.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import "./core/deno/monkey-patch.ts";
import {
Command,
CompletionsCommand,
HelpCommand,
} from "cliffy/command/mod.ts";
import { commands } from "./command/command.ts";
import { appendLogOptions } from "./core/log.ts";
import { debug } from "log/mod.ts";
import { cleanupSessionTempDir, initSessionTempDir } from "./core/temp.ts";
import { removeFlags } from "./core/flags.ts";
import { quartoConfig } from "./core/quarto.ts";
import { execProcess } from "./core/process.ts";
import { pandocBinaryPath } from "./core/resources.ts";
import { appendProfileArg, setProfileFromArg } from "./quarto-core/profile.ts";
import {
devConfigsEqual,
readInstalledDevConfig,
readSourceDevConfig,
reconfigureQuarto,
} from "./core/devconfig.ts";
import { typstBinaryPath } from "./core/typst.ts";
import { onCleanup } from "./core/cleanup.ts";
import { runScript } from "./command/run/run.ts";
// ensures run handlers are registered
import "./core/run/register.ts";
// ensures language handlers are registered
import "./core/handlers/handlers.ts";
// ensures project types are registered
import "./project/types/register.ts";
// ensures writer formats are registered
import "./format/imports.ts";
import { kCliffyImplicitCwd } from "./config/constants.ts";
import { mainRunner } from "./core/main.ts";
export async function quarto(
args: string[],
cmdHandler?: (command: Command) => Command,
env?: Record<string, string>,
) {
// check for need to reconfigure
if (quartoConfig.isDebug()) {
const installed = readInstalledDevConfig();
const source = readSourceDevConfig();
if (installed == null || !devConfigsEqual(installed, source)) {
await reconfigureQuarto(installed, source);
Deno.exit(1);
}
}
// passthrough to pandoc
if (args[0] === "pandoc" && args[1] !== "help") {
const result = await execProcess({
cmd: [pandocBinaryPath(), ...args.slice(1)],
env,
});
Deno.exit(result.code);
}
// passthrough to typst
if (args[0] === "typst") {
const result = await execProcess({
cmd: [typstBinaryPath(), ...args.slice(1)],
env,
});
Deno.exit(result.code);
}
// passthrough to run handlers
if (args[0] === "run" && args[1] !== "help" && args[1] !== "--help") {
const result = await runScript(args.slice(1));
Deno.exit(result.code);
}
// inject implicit cwd arg for quarto preview/render whose
// first argument is a command line parmaeter. this allows
// us to evade a cliffy cli parsing issue where it requires
// at least one defined argument to be parsed before it can
// access undefined arguments.
//
// we do this via a UUID so that we can detect this happened
// and issue a warning in the case where the user might
// be calling render with parameters in incorrect order.
//
// see https://github.com/quarto-dev/quarto-cli/issues/3581
if (
args.length > 1 &&
(args[0] === "render" || args[0] === "preview") &&
args[1].startsWith("-")
) {
args = [args[0], kCliffyImplicitCwd, ...args.slice(1)];
}
debug("Quarto version: " + quartoConfig.version());
const oldEnv: Record<string, string | undefined> = {};
for (const [key, value] of Object.entries(env || {})) {
const oldV = Deno.env.get(key);
oldEnv[key] = oldV;
Deno.env.set(key, value);
}
const quartoCommand = new Command()
.name("quarto")
.help({ colors: false })
.version(quartoConfig.version() + "\n")
.description("Quarto CLI")
.throwErrors();
commands().forEach((command) => {
// turn off colors
command.help({ colors: false });
quartoCommand.command(
command.getName(),
cmdHandler !== undefined ? cmdHandler(command) : command,
);
});
// init temp dir
initSessionTempDir();
onCleanup(cleanupSessionTempDir);
const promise = quartoCommand.command("help", new HelpCommand().global())
.command("completions", new CompletionsCommand()).hidden().parse(args);
for (const [key, value] of Object.entries(oldEnv)) {
if (value === undefined) {
Deno.env.delete(key);
} else {
Deno.env.set(key, value);
}
}
await promise;
}
if (import.meta.main) {
await mainRunner(async (args) => {
// initialize profile (remove from args)
let quartoArgs = [...Deno.args];
if (setProfileFromArg(args)) {
const removeArgs = new Map<string, boolean>();
removeArgs.set("--profile", true);
quartoArgs = removeFlags(quartoArgs, removeArgs);
}
// run quarto
await quarto(quartoArgs, (cmd) => {
cmd = appendLogOptions(cmd);
return appendProfileArg(cmd);
});
});
}