Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (176 loc) · 5.62 KB
/
Copy pathindex.js
File metadata and controls
198 lines (176 loc) · 5.62 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
191
192
193
194
195
196
197
198
"use strict";
const fs = require("fs");
const os = require("os");
const path = require("path");
const { spawn } = require("child_process");
const {
platformPackageName,
resolveBinaryPath,
} = require("./lib/binary");
const { buildCompileArguments } = require("./lib/arguments");
const { diagnoseCompileFailure } = require("./lib/diagnostics");
const { TECTONIC_VERSION } = require("./lib/platforms");
function binaryPath() {
return resolveBinaryPath();
}
/**
* Run the tectonic binary with raw arguments. Resolves with the captured
* output whatever the exit code; rejects only if the process fails to spawn.
*/
function run(args, options = {}) {
if (!Array.isArray(args) || args.some((arg) => typeof arg !== "string")) {
throw new TypeError("run() requires an array of string arguments.");
}
if (
options.timeout != null &&
(!Number.isFinite(options.timeout) || options.timeout <= 0)
) {
throw new TypeError("`timeout` must be a positive number of milliseconds.");
}
const bin = resolveBinaryPath();
return new Promise((resolve, reject) => {
const child = spawn(bin, args, {
cwd: options.cwd,
env: options.env,
killSignal: options.killSignal,
windowsHide: options.windowsHide !== false,
});
let stdout = "";
let stderr = "";
let settled = false;
let timedOut = false;
let timeoutId = null;
if (options.timeout != null) {
timeoutId = setTimeout(() => {
timedOut = child.kill(options.killSignal || "SIGTERM");
}, options.timeout);
timeoutId.unref();
}
child.stdout.on("data", (d) => {
const s = d.toString();
stdout += s;
if (options.onStdout) options.onStdout(s);
});
child.stderr.on("data", (d) => {
const s = d.toString();
stderr += s;
if (options.onStderr) options.onStderr(s);
});
child.on("error", (error) => {
if (settled) return;
settled = true;
if (timeoutId) clearTimeout(timeoutId);
reject(error);
});
child.on("close", (code, signal) => {
if (settled) return;
settled = true;
if (timeoutId) clearTimeout(timeoutId);
resolve({
exitCode: code,
signal,
timedOut,
stdout,
stderr,
});
});
// Close stdin so a document that reads from the terminal errors out
// instead of hanging.
child.stdin.end(options.input != null ? options.input : undefined);
});
}
/**
* Compile a LaTeX document to PDF. Takes exactly one of `tex` (source string)
* or `texFile` (path). See index.d.ts for the full option list.
*/
async function compile(config = {}) {
const { tex, texFile } = config;
if ((tex == null) === (texFile == null)) {
throw new Error("compile() requires exactly one of `tex` or `texFile`.");
}
if (tex != null && typeof tex !== "string" && !Buffer.isBuffer(tex)) {
throw new TypeError("`tex` must be a string or Buffer.");
}
if (texFile != null && typeof texFile !== "string") {
throw new TypeError("`texFile` must be a path string.");
}
if (
config.args != null &&
(!Array.isArray(config.args) ||
config.args.some((arg) => typeof arg !== "string"))
) {
throw new TypeError("`args` must be an array of strings.");
}
const cleanup = new Set();
try {
let inputPath;
if (texFile != null) {
inputPath = path.resolve(texFile);
if (!fs.existsSync(inputPath)) {
throw new Error(`texFile not found: ${inputPath}`);
}
} else {
const srcDir = fs.mkdtempSync(path.join(os.tmpdir(), "node-tectonic-src-"));
cleanup.add(srcDir);
inputPath = path.join(srcDir, "input.tex");
fs.writeFileSync(inputPath, tex, "utf8");
}
// An explicit outputDir belongs to the caller. With returnBuffer and no
// outputDir we build into a temp dir and delete it after reading the PDF
// back. Otherwise artifacts land next to the input, or in cwd for string
// input.
let outputDir;
if (config.outputDir) {
outputDir = path.resolve(config.outputDir);
} else if (config.returnBuffer) {
outputDir = fs.mkdtempSync(path.join(os.tmpdir(), "node-tectonic-out-"));
cleanup.add(outputDir);
} else {
outputDir = texFile != null ? path.dirname(inputPath) : process.cwd();
}
fs.mkdirSync(outputDir, { recursive: true });
const args = buildCompileArguments(inputPath, outputDir, config);
const result = await run(args, {
cwd: config.cwd,
env: config.env,
killSignal: config.killSignal,
timeout: config.timeout,
windowsHide: config.windowsHide,
onStdout: config.onStdout,
onStderr: config.onStderr,
});
const base = path.basename(inputPath, path.extname(inputPath));
const pdfPath = path.join(outputDir, `${base}.pdf`);
const pdfExists = fs.existsSync(pdfPath);
const success = result.exitCode === 0 && pdfExists;
const failure = diagnoseCompileFailure(result, pdfExists);
let pdfBuffer = null;
if (success && config.returnBuffer) {
pdfBuffer = fs.readFileSync(pdfPath);
}
// Never return a path into a temp dir we're about to delete.
const pdfPersists = success && !cleanup.has(outputDir);
return {
success,
exitCode: result.exitCode,
signal: result.signal,
timedOut: result.timedOut,
failure,
pdfPath: pdfPersists ? pdfPath : null,
pdfBuffer,
stdout: result.stdout,
stderr: result.stderr,
};
} finally {
for (const dir of cleanup) {
fs.rmSync(dir, { recursive: true, force: true });
}
}
}
module.exports = {
compile,
run,
binaryPath,
platformPackageName,
tectonicVersion: TECTONIC_VERSION,
};