-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathtypst.ts
More file actions
190 lines (171 loc) · 4.7 KB
/
typst.ts
File metadata and controls
190 lines (171 loc) · 4.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* typst.ts
*
* Copyright (C) 2022 Posit Software, PBC
*/
import { error, info } from "../deno_ral/log.ts";
import { basename } from "../deno_ral/path.ts";
import * as colors from "fmt/colors";
import { satisfies } from "semver/mod.ts";
import { execProcess } from "./process.ts";
import { architectureToolsPath } from "./resources.ts";
import { resourcePath } from "./resources.ts";
export function typstBinaryPath() {
return Deno.env.get("QUARTO_TYPST") ||
architectureToolsPath("typst");
}
function fontPathsArgs(fontPaths?: string[]) {
// orders matter and fontPathsQuarto should be first for our template to work
const fontPathsQuarto = ["--font-path", resourcePath("formats/typst/fonts")];
const fontPathsEnv = Deno.env.get("TYPST_FONT_PATHS");
let fontExtrasArgs: string[] = [];
if (fontPaths && fontPaths.length > 0) {
fontExtrasArgs = fontPaths.map((p) => ["--font-path", p]).flat();
} else if (fontPathsEnv) {
// Env var is used only if not specified in config by user
// to respect Typst behavior where `--font-path` has precedence over env var
return fontExtrasArgs = ["--font-path", fontPathsEnv];
}
return fontPathsQuarto.concat(fontExtrasArgs);
}
export type TypstCompileOptions = {
quiet?: boolean;
fontPaths?: string[];
rootDir?: string;
};
export async function typstCompile(
input: string,
output: string,
options: TypstCompileOptions = {},
) {
const quiet = options.quiet ?? false;
const fontPaths = options.fontPaths;
if (!quiet) {
typstProgress(input, output);
}
const cmd = [
typstBinaryPath(),
"compile",
];
if (options.rootDir) {
cmd.push("--root", options.rootDir);
}
cmd.push(
input,
...fontPathsArgs(fontPaths),
output,
);
const result = await execProcess({ cmd: cmd[0], args: cmd.slice(1) });
if (!quiet && result.success) {
typstProgressDone();
}
return result;
}
export async function typstVersion() {
const cmd = [typstBinaryPath(), "--version"];
try {
const result = await execProcess({
cmd: cmd[0],
args: cmd.slice(1),
stdout: "piped",
stderr: "piped",
});
if (result.success && result.stdout) {
const match = result.stdout.trim().match(/^typst (\d+\.\d+\.\d+)/);
if (match) {
return match[1];
} else {
return undefined;
}
} else {
return undefined;
}
} catch {
return undefined;
}
}
export async function validateRequiredTypstVersion() {
// only validate if we have a custom env var
if (Deno.env.get("QUARTO_TYPST")) {
const version = await typstVersion();
if (version) {
const required = ">=0.8";
if (!satisfies(version, required)) {
error(
"An updated version of the Typst CLI is required for rendering typst documents.\n",
);
info(colors.blue(
`You are running version ${version} and version ${required} is required.\n`,
));
info(colors.blue(
`Updating Typst: ${
colors.underline("https://github.com/typst/typst#installation")
}\n`,
));
throw new Error();
}
} else {
error(
"You need to install the Typst CLI in order to render typst documents.\n",
);
info(colors.blue(
`Installing Typst: ${
colors.underline("https://github.com/typst/typst#installation")
}\n`,
));
throw new Error();
}
}
}
// TODO: this doesn't yet work correctly (typst exits on the first change to the typ file)
// leaving the code here anyway as a foundation for getting it to work later
/*
export async function typstWatch(
input: string,
output: string,
quiet = false,
) {
if (!quiet) {
typstProgress(input, output);
}
// abort controller
const controller = new AbortController();
// setup command
const cmd = new Deno.Command("typst", {
args: [input, output, "--watch"],
cwd: dirname(input),
stdout: "piped",
stderr: "piped",
signal: controller.signal,
});
// spawn it
const child = cmd.spawn();
// wait for ready
let allOutput = "";
const decoder = new TextDecoder();
for await (const chunk of child.stderr) {
const text = decoder.decode(chunk);
allOutput += text;
if (allOutput.includes("compiled successfully")) {
if (!quiet) {
typstProgressDone();
}
child.status.then((status) => {
error(`typst exited with status ${status.code}`);
});
break;
}
}
// return the abort controller
return controller;
}
*/
function typstProgress(input: string, output: string) {
info(
`[typst]: Compiling ${basename(input)} to ${basename(output)}...`,
{ newline: false },
);
}
function typstProgressDone() {
info("DONE\n");
}