latex-kit 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -3
- package/args.js +3 -3
- package/boilerplates.d.ts +8 -0
- package/boilerplates.js +225 -0
- package/build.d.ts +28 -2
- package/build.js +233 -94
- package/check.js +4 -3
- package/cli.js +14 -3
- package/commands.js +7 -0
- package/config.d.ts +14 -3
- package/config.js +1 -1
- package/diff.d.ts +25 -0
- package/diff.js +112 -0
- package/esm/args.js +3 -3
- package/esm/boilerplates.d.ts +8 -0
- package/esm/boilerplates.js +220 -0
- package/esm/build.d.ts +28 -2
- package/esm/build.js +234 -97
- package/esm/check.js +4 -3
- package/esm/cli.js +14 -3
- package/esm/commands.js +8 -1
- package/esm/config.d.ts +14 -3
- package/esm/config.js +1 -1
- package/esm/diff.d.ts +25 -0
- package/esm/diff.js +108 -0
- package/esm/import.d.ts +24 -0
- package/esm/import.js +113 -0
- package/esm/index.d.ts +9 -2
- package/esm/index.js +8 -2
- package/esm/lock.d.ts +24 -0
- package/esm/lock.js +50 -0
- package/esm/project-commands.d.ts +14 -0
- package/esm/project-commands.js +232 -7
- package/esm/project.d.ts +20 -1
- package/esm/project.js +105 -4
- package/esm/render.js +6 -6
- package/esm/scaffold.d.ts +27 -0
- package/esm/scaffold.js +108 -0
- package/esm/usage.js +40 -7
- package/import.d.ts +24 -0
- package/import.js +118 -0
- package/index.d.ts +9 -2
- package/index.js +27 -2
- package/lock.d.ts +24 -0
- package/lock.js +57 -0
- package/package.json +6 -5
- package/project-commands.d.ts +14 -0
- package/project-commands.js +238 -5
- package/project.d.ts +20 -1
- package/project.js +103 -2
- package/render.js +5 -5
- package/scaffold.d.ts +27 -0
- package/scaffold.js +115 -0
- package/usage.js +40 -7
package/build.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BuildError = void 0;
|
|
4
|
+
exports.signerSlug = signerSlug;
|
|
5
|
+
exports.signaturesDir = signaturesDir;
|
|
4
6
|
exports.buildDocument = buildDocument;
|
|
5
7
|
exports.buildBlank = buildBlank;
|
|
6
8
|
const render_1 = require("@latex-kit/render");
|
|
@@ -9,6 +11,7 @@ const template_1 = require("@latex-kit/template");
|
|
|
9
11
|
const child_process_1 = require("child_process");
|
|
10
12
|
const crypto_1 = require("crypto");
|
|
11
13
|
const fs_1 = require("fs");
|
|
14
|
+
const os_1 = require("os");
|
|
12
15
|
const path_1 = require("path");
|
|
13
16
|
class BuildError extends Error {
|
|
14
17
|
diagnostics;
|
|
@@ -52,6 +55,42 @@ function gitShortHead(cwd) {
|
|
|
52
55
|
return 'no-git';
|
|
53
56
|
}
|
|
54
57
|
}
|
|
58
|
+
/** A folder name for a signer: "Avery Example" → "avery-example". */
|
|
59
|
+
function signerSlug(name) {
|
|
60
|
+
return name
|
|
61
|
+
.normalize('NFKD')
|
|
62
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
63
|
+
.toLowerCase()
|
|
64
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
65
|
+
.replace(/^-|-$/g, '');
|
|
66
|
+
}
|
|
67
|
+
const IMAGE_EXTENSIONS = ['.pdf', '.png', '.jpg', '.jpeg'];
|
|
68
|
+
/** The signatures folder: option, LATEX_KIT_SIGNATURES, then config. `~` is home. */
|
|
69
|
+
function signaturesDir(project, option) {
|
|
70
|
+
const raw = option ?? process.env.LATEX_KIT_SIGNATURES ?? project.config.signatures ?? null;
|
|
71
|
+
if (!raw)
|
|
72
|
+
return null;
|
|
73
|
+
const expanded = raw === '~' || raw.startsWith('~/') ? (0, path_1.join)((0, os_1.homedir)(), raw.slice(1)) : raw;
|
|
74
|
+
return (0, path_1.resolve)(project.root, expanded);
|
|
75
|
+
}
|
|
76
|
+
/** Every file under a folder, for the cache key. */
|
|
77
|
+
function filesUnder(dir) {
|
|
78
|
+
const out = [];
|
|
79
|
+
const walk = (d) => {
|
|
80
|
+
if (!(0, fs_1.existsSync)(d))
|
|
81
|
+
return;
|
|
82
|
+
for (const name of (0, fs_1.readdirSync)(d).sort()) {
|
|
83
|
+
const p = (0, path_1.join)(d, name);
|
|
84
|
+
if ((0, fs_1.statSync)(p).isDirectory())
|
|
85
|
+
walk(p);
|
|
86
|
+
else
|
|
87
|
+
out.push(p);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
walk(dir);
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
const posixRel = (root, p) => (0, path_1.relative)(root, p).split(path_1.sep).join(path_1.posix.sep);
|
|
55
94
|
async function run(job) {
|
|
56
95
|
const { project, template, variant, options } = job;
|
|
57
96
|
const diagnostics = job.diagnostics;
|
|
@@ -72,99 +111,194 @@ async function run(job) {
|
|
|
72
111
|
docId = (0, schema_1.mintDocId)({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
73
112
|
minted = true;
|
|
74
113
|
}
|
|
75
|
-
const
|
|
76
|
-
data: job.data,
|
|
77
|
-
variant,
|
|
78
|
-
locale: job.locale,
|
|
79
|
-
docId,
|
|
80
|
-
template: template.name,
|
|
81
|
-
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
82
|
-
loadSnippet,
|
|
83
|
-
});
|
|
84
|
-
diagnostics.push(...evaluated.diagnostics);
|
|
85
|
-
if ((0, template_1.hasErrors)(diagnostics))
|
|
86
|
-
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
87
|
-
// snippets count as inputs for the build date too
|
|
88
|
-
const finalEpoch = snippets.size ? buildEpoch([...files, ...snippets]) : { epoch, reproducible };
|
|
89
|
-
// 2. write the .tex next to latex-kit.sty
|
|
90
|
-
const outDir = (0, path_1.join)(project.outDir, job.name);
|
|
91
|
-
(0, fs_1.mkdirSync)(outDir, { recursive: true });
|
|
114
|
+
const outDir = options.outDir ?? (0, path_1.join)(project.outDir, job.name);
|
|
92
115
|
const job_ = variant === 'final' ? job.name : `${job.name}-${variant}`;
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
const
|
|
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
|
-
texInputs: [(0, path_1.relative)(project.root, outDir) || '.', (0, path_1.relative)(project.root, template.dir) || '.'],
|
|
123
|
-
onOutput: options.onOutput,
|
|
124
|
-
};
|
|
125
|
-
let rendered;
|
|
126
|
-
try {
|
|
127
|
-
rendered = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
128
|
-
}
|
|
129
|
-
catch (e) {
|
|
130
|
-
if (e instanceof render_1.RenderError) {
|
|
131
|
-
throw new BuildError(`${job.name}: ${e.message}`, [
|
|
132
|
-
...diagnostics,
|
|
133
|
-
...e.diagnostics.map((d) => (0, template_1.diag)(d.severity, d.message, d.file ? { file: project.rel((0, path_1.join)(outDir, (0, path_1.basename)(d.file))), line: d.line ?? 0, column: 0 } : undefined)),
|
|
134
|
-
]);
|
|
116
|
+
// signed builds: copy signature images next to the .tex for this build only
|
|
117
|
+
const stageDir = (0, path_1.join)(outDir, 'lk-signatures');
|
|
118
|
+
const staged = [];
|
|
119
|
+
let signatureImage;
|
|
120
|
+
const sigDir = variant === 'signed' ? signaturesDir(project, options.signatures) : null;
|
|
121
|
+
if (sigDir) {
|
|
122
|
+
const at = { file: project.rel(sigDir), line: 1, column: 1 };
|
|
123
|
+
if (!(0, fs_1.existsSync)(sigDir)) {
|
|
124
|
+
diagnostics.push((0, template_1.diag)('error', `signatures folder not found: ${project.rel(sigDir)}`, at));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const inside = (0, path_1.relative)(project.root, sigDir);
|
|
128
|
+
if (!inside.startsWith('..') && !(0, path_1.isAbsolute)(inside)) {
|
|
129
|
+
diagnostics.push((0, template_1.diag)('warning', 'the signatures folder is inside the project; keep signature images out of the repository', at));
|
|
130
|
+
}
|
|
131
|
+
signatureImage = (signer) => {
|
|
132
|
+
const names = signer.image ? [signer.image] : IMAGE_EXTENSIONS.map((e) => `${signerSlug(signer.name)}${e}`);
|
|
133
|
+
const found = names.find((n) => (0, path_1.basename)(n) === n && (0, fs_1.existsSync)((0, path_1.join)(sigDir, n)));
|
|
134
|
+
if (!found) {
|
|
135
|
+
diagnostics.push((0, template_1.diag)('warning', `no signature image for ${signer.name} in ${project.rel(sigDir)} (looked for ${names.join(', ')}); printed /s/ ${signer.name}`, at));
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
(0, fs_1.mkdirSync)(stageDir, { recursive: true });
|
|
139
|
+
const target = (0, path_1.join)(stageDir, found);
|
|
140
|
+
(0, fs_1.copyFileSync)((0, path_1.join)(sigDir, found), target);
|
|
141
|
+
if (!staged.includes(target))
|
|
142
|
+
staged.push(target);
|
|
143
|
+
return posixRel(project.root, target);
|
|
144
|
+
};
|
|
135
145
|
}
|
|
136
|
-
throw e;
|
|
137
146
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (options.verify) {
|
|
141
|
-
const verifyDir = `${outDir}.verify`;
|
|
142
|
-
const again = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, verifyDir) });
|
|
143
|
-
result.verified = again.sha256 === rendered.sha256;
|
|
144
|
-
(0, fs_1.rmSync)(verifyDir, { recursive: true, force: true });
|
|
147
|
+
try {
|
|
148
|
+
return await fill();
|
|
145
149
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
+
finally {
|
|
151
|
+
(0, fs_1.rmSync)(stageDir, { recursive: true, force: true });
|
|
152
|
+
}
|
|
153
|
+
async function fill() {
|
|
154
|
+
const evaluated = (0, template_1.evaluateTemplate)(template.compiled, {
|
|
155
|
+
data: job.data,
|
|
156
|
+
variant,
|
|
157
|
+
locale: job.locale,
|
|
158
|
+
docId,
|
|
159
|
+
template: template.name,
|
|
160
|
+
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
161
|
+
loadSnippet,
|
|
162
|
+
loadClause: project.loadClause,
|
|
163
|
+
components: project.components,
|
|
164
|
+
signatureImage,
|
|
165
|
+
signed: job.signed,
|
|
166
|
+
});
|
|
167
|
+
diagnostics.push(...evaluated.diagnostics);
|
|
168
|
+
if ((0, template_1.hasErrors)(diagnostics))
|
|
169
|
+
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
170
|
+
// snippets, clauses and components count as inputs for the build date too
|
|
171
|
+
const clauseFiles = evaluated.clauses.map((c) => (0, path_1.resolve)(c.file));
|
|
172
|
+
const componentFiles = evaluated.components.map((n) => project.componentFiles[n]).filter((f) => !!f);
|
|
173
|
+
const extra = [...snippets, ...clauseFiles, ...componentFiles];
|
|
174
|
+
const finalEpoch = extra.length ? buildEpoch([...files, ...extra]) : { epoch, reproducible };
|
|
175
|
+
// 2. write the .tex next to latex-kit.sty
|
|
176
|
+
(0, fs_1.mkdirSync)(outDir, { recursive: true });
|
|
177
|
+
const texPath = (0, path_1.join)(outDir, `${job_}.tex`);
|
|
178
|
+
const stamp = `${new Date(finalEpoch.epoch * 1000).toISOString().slice(0, 10)} ${gitShortHead(project.root)}`;
|
|
179
|
+
const tex = (0, template_1.wrapDocument)(evaluated.tex, { variant, stamp });
|
|
180
|
+
(0, fs_1.writeFileSync)(texPath, tex);
|
|
181
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(outDir, 'latex-kit.sty'), template_1.LATEX_KIT_STY);
|
|
182
|
+
const result = {
|
|
183
|
+
name: job.name,
|
|
184
|
+
template: template.name,
|
|
185
|
+
variant,
|
|
186
|
+
tex: texPath,
|
|
187
|
+
pdf: null,
|
|
188
|
+
sha256: null,
|
|
189
|
+
docId: docId ?? null,
|
|
190
|
+
minted: false,
|
|
191
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
192
|
+
reproducible: finalEpoch.reproducible,
|
|
193
|
+
engine: null,
|
|
194
|
+
engineReason: null,
|
|
195
|
+
cached: false,
|
|
196
|
+
diagnostics,
|
|
197
|
+
};
|
|
198
|
+
if (options.texOnly)
|
|
199
|
+
return result;
|
|
200
|
+
// 3. render, unless the last build had exactly these inputs
|
|
201
|
+
const choice = options.engine ?? template.meta.engine ?? project.config.engine ?? 'auto';
|
|
202
|
+
let engine;
|
|
203
|
+
let engineReason;
|
|
204
|
+
if (choice === 'auto') {
|
|
205
|
+
const d = (0, render_1.detectEngine)(tex);
|
|
206
|
+
engine = d.engine;
|
|
207
|
+
engineReason = `auto: ${d.reason}`;
|
|
208
|
+
for (const w of d.warnings)
|
|
209
|
+
diagnostics.push((0, template_1.diag)('warning', w, { file: template.compiled.file, line: 1, column: 1 }));
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
engine = choice;
|
|
213
|
+
engineReason = 'chosen';
|
|
214
|
+
}
|
|
215
|
+
result.engine = engine;
|
|
216
|
+
result.engineReason = engineReason;
|
|
217
|
+
const backend = options.backend ?? project.config.backend ?? 'docker';
|
|
218
|
+
const image = project.image(options.image);
|
|
219
|
+
const platform = options.platform !== undefined ? options.platform : project.config.platform;
|
|
220
|
+
const renderOpts = {
|
|
221
|
+
file: texPath,
|
|
222
|
+
root: project.root,
|
|
223
|
+
engine,
|
|
224
|
+
backend,
|
|
225
|
+
image,
|
|
226
|
+
platform,
|
|
227
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
228
|
+
texInputs: [(0, path_1.relative)(project.root, outDir) || '.', (0, path_1.relative)(project.root, template.dir) || '.'],
|
|
229
|
+
onOutput: options.onOutput,
|
|
230
|
+
};
|
|
231
|
+
// content-addressed: the PDF depends on the .tex, the .sty, the template
|
|
232
|
+
// folder (images, \\input files), signature images, the engine, the image
|
|
233
|
+
// and the build date. Same key → same bytes, so the last PDF is reused.
|
|
234
|
+
const keyInputs = [...filesUnder(template.dir), ...staged].map((f) => [posixRel(project.root, f), sha256((0, fs_1.readFileSync)(f))]);
|
|
235
|
+
const cacheKey = sha256(JSON.stringify({ v: 1, tex: sha256(tex), sty: sha256(template_1.LATEX_KIT_STY), engine, backend, image: backend === 'docker' ? (image ?? 'default') : null, platform: platform ?? null, epoch: finalEpoch.epoch, inputs: keyInputs }));
|
|
236
|
+
const manifestPath = (0, path_1.join)(outDir, `${job_}.manifest.json`);
|
|
237
|
+
const pdfPath = (0, path_1.join)(outDir, `${job_}.pdf`);
|
|
238
|
+
if (!options.force && !options.verify && (0, fs_1.existsSync)(manifestPath) && (0, fs_1.existsSync)(pdfPath)) {
|
|
239
|
+
try {
|
|
240
|
+
const m = JSON.parse((0, fs_1.readFileSync)(manifestPath, 'utf8'));
|
|
241
|
+
const pdfSha = sha256((0, fs_1.readFileSync)(pdfPath));
|
|
242
|
+
if (m.cacheKey === cacheKey && m.pdf?.sha256 === pdfSha) {
|
|
243
|
+
result.pdf = pdfPath;
|
|
244
|
+
result.sha256 = pdfSha;
|
|
245
|
+
result.cached = true;
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
// an unreadable manifest just means a rebuild
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
let rendered;
|
|
254
|
+
try {
|
|
255
|
+
rendered = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
if (e instanceof render_1.RenderError) {
|
|
259
|
+
throw new BuildError(`${job.name}: ${e.message}`, [
|
|
260
|
+
...diagnostics,
|
|
261
|
+
...e.diagnostics.map((d) => (0, template_1.diag)(d.severity, d.message, d.file ? { file: project.rel((0, path_1.join)(outDir, (0, path_1.basename)(d.file))), line: d.line ?? 0, column: 0 } : undefined)),
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
throw e;
|
|
265
|
+
}
|
|
266
|
+
result.pdf = rendered.pdf;
|
|
267
|
+
result.sha256 = rendered.sha256;
|
|
268
|
+
if (options.verify) {
|
|
269
|
+
// same output path: some tools (Ghostscript) hash the path into the PDF ID
|
|
270
|
+
const again = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
271
|
+
result.verified = again.sha256 === rendered.sha256;
|
|
272
|
+
}
|
|
273
|
+
// 4. keep the ID: only after a successful final build
|
|
274
|
+
if (minted && job.mintInto) {
|
|
275
|
+
(0, schema_1.writeDocId)(job.mintInto, docId);
|
|
276
|
+
result.minted = true;
|
|
277
|
+
}
|
|
278
|
+
// 5. manifest: what went in, what came out
|
|
279
|
+
const inputFiles = [...new Set([...files, ...extra])].filter(fs_1.existsSync).sort();
|
|
280
|
+
(0, fs_1.writeFileSync)(manifestPath, JSON.stringify({
|
|
281
|
+
document: job.name,
|
|
282
|
+
template: template.name,
|
|
283
|
+
variant,
|
|
284
|
+
docId: docId ?? null,
|
|
285
|
+
locale: job.locale,
|
|
286
|
+
engine,
|
|
287
|
+
engineReason,
|
|
288
|
+
image: rendered.image,
|
|
289
|
+
cacheKey,
|
|
290
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
291
|
+
inputs: inputFiles.map((f) => ({ file: (0, path_1.relative)(project.root, f), sha256: sha256((0, fs_1.readFileSync)(f)) })),
|
|
292
|
+
tex: { file: (0, path_1.relative)(project.root, texPath), sha256: sha256(tex) },
|
|
293
|
+
pdf: { file: (0, path_1.relative)(project.root, rendered.pdf), sha256: rendered.sha256 },
|
|
294
|
+
clauses: evaluated.clauses.map((c) => ({ id: c.id, version: c.version ?? null, file: (0, path_1.relative)(project.root, (0, path_1.resolve)(c.file)) })),
|
|
295
|
+
components: evaluated.components,
|
|
296
|
+
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
297
|
+
// which signers had an image; never the images or where they live
|
|
298
|
+
signatureImages: staged.map((f) => (0, path_1.basename)(f)),
|
|
299
|
+
}, null, 2) + '\n');
|
|
300
|
+
return result;
|
|
150
301
|
}
|
|
151
|
-
// 5. manifest: what went in, what came out
|
|
152
|
-
const inputFiles = [...new Set([...files, ...snippets])].filter(fs_1.existsSync).sort();
|
|
153
|
-
(0, fs_1.writeFileSync)((0, path_1.join)(outDir, `${job_}.manifest.json`), JSON.stringify({
|
|
154
|
-
document: job.name,
|
|
155
|
-
template: template.name,
|
|
156
|
-
variant,
|
|
157
|
-
docId: docId ?? null,
|
|
158
|
-
locale: job.locale,
|
|
159
|
-
engine,
|
|
160
|
-
image: rendered.image,
|
|
161
|
-
sourceDateEpoch: finalEpoch.epoch,
|
|
162
|
-
inputs: inputFiles.map((f) => ({ file: (0, path_1.relative)(project.root, f), sha256: sha256((0, fs_1.readFileSync)(f)) })),
|
|
163
|
-
tex: { file: (0, path_1.relative)(project.root, texPath), sha256: sha256(tex) },
|
|
164
|
-
pdf: { file: (0, path_1.relative)(project.root, rendered.pdf), sha256: rendered.sha256 },
|
|
165
|
-
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
166
|
-
}, null, 2) + '\n');
|
|
167
|
-
return result;
|
|
168
302
|
}
|
|
169
303
|
/** Build one document record. */
|
|
170
304
|
async function buildDocument(project, file, options = {}) {
|
|
@@ -185,20 +319,24 @@ async function buildDocument(project, file, options = {}) {
|
|
|
185
319
|
diagnostics.push((0, template_1.diag)(sev, `data.${i.path}: ${i.message}`, at));
|
|
186
320
|
}
|
|
187
321
|
}
|
|
188
|
-
const needsId = (variant === 'final' || variant === 'signed') && !doc.record.docId;
|
|
322
|
+
const needsId = (variant === 'final' || variant === 'signed') && !doc.record.docId && !options.docIdFallback;
|
|
189
323
|
if (needsId && !['.json', '.yaml', '.yml'].includes((0, path_1.extname)(file).toLowerCase())) {
|
|
190
324
|
const suggestion = (0, schema_1.mintDocId)({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
191
325
|
diagnostics.push((0, template_1.diag)('error', `a ${variant} build needs a document ID, and latex-kit does not edit code. Add: docId: '${suggestion}'`, at));
|
|
192
326
|
throw new BuildError(`${doc.record.template}: no document ID`, diagnostics);
|
|
193
327
|
}
|
|
328
|
+
const natural = doc.record.locale ?? template.meta.locale ?? project.config.locale;
|
|
329
|
+
const name = project.documentName(file);
|
|
194
330
|
return run({
|
|
195
331
|
project,
|
|
196
332
|
template,
|
|
197
|
-
|
|
333
|
+
// another language is another PDF, next to the usual one
|
|
334
|
+
name: options.locale && options.locale !== natural ? `${name}-${options.locale}` : name,
|
|
198
335
|
variant,
|
|
199
336
|
data,
|
|
200
|
-
locale:
|
|
201
|
-
docId: doc.record.docId,
|
|
337
|
+
locale: options.locale ?? natural,
|
|
338
|
+
docId: doc.record.docId ?? options.docIdFallback,
|
|
339
|
+
signed: doc.record.signed,
|
|
202
340
|
inputs: [file, ...doc.refFiles],
|
|
203
341
|
diagnostics,
|
|
204
342
|
options,
|
|
@@ -208,13 +346,14 @@ async function buildDocument(project, file, options = {}) {
|
|
|
208
346
|
/** Build a template as a blank form, without a record. */
|
|
209
347
|
async function buildBlank(project, templateName, options = {}) {
|
|
210
348
|
const template = await project.template(templateName);
|
|
349
|
+
const natural = template.meta.locale ?? project.config.locale;
|
|
211
350
|
return run({
|
|
212
351
|
project,
|
|
213
352
|
template,
|
|
214
|
-
name: `${templateName}
|
|
353
|
+
name: options.locale && options.locale !== natural ? `${templateName}-${options.locale}` : templateName,
|
|
215
354
|
variant: 'blank',
|
|
216
355
|
data: {},
|
|
217
|
-
locale:
|
|
356
|
+
locale: options.locale ?? natural,
|
|
218
357
|
inputs: [],
|
|
219
358
|
diagnostics: [],
|
|
220
359
|
options: { ...options, variant: 'blank' },
|
package/check.js
CHANGED
|
@@ -9,15 +9,16 @@ const template_1 = require("@latex-kit/template");
|
|
|
9
9
|
* against each schema, every document's data, references, and duplicate IDs.
|
|
10
10
|
*/
|
|
11
11
|
async function checkProject(project) {
|
|
12
|
-
const diagnostics = [];
|
|
12
|
+
const diagnostics = [...project.codeDiagnostics];
|
|
13
13
|
const templates = project.templateNames();
|
|
14
|
+
const components = project.componentNames();
|
|
14
15
|
for (const name of templates) {
|
|
15
16
|
try {
|
|
16
17
|
const t = await project.template(name);
|
|
17
18
|
if (!t.jsonSchema) {
|
|
18
19
|
diagnostics.push((0, template_1.diag)('warning', `template ${name} has no schema.ts, so field names are not checked`, { file: t.compiled.file, line: 1, column: 1 }));
|
|
19
20
|
}
|
|
20
|
-
diagnostics.push(...(0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet }).diagnostics);
|
|
21
|
+
diagnostics.push(...(0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause, components }).diagnostics);
|
|
21
22
|
}
|
|
22
23
|
catch (e) {
|
|
23
24
|
diagnostics.push((0, template_1.diag)('error', e.message, { file: project.rel(project.path(project.config.templates, name)), line: 1, column: 1 }));
|
|
@@ -53,6 +54,6 @@ async function checkProject(project) {
|
|
|
53
54
|
/** Every field a template reads, including through snippets. */
|
|
54
55
|
async function templateFields(project, name) {
|
|
55
56
|
const t = await project.template(name);
|
|
56
|
-
const r = (0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet });
|
|
57
|
+
const r = (0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause, components: project.componentNames() });
|
|
57
58
|
return { fields: r.fields, diagnostics: r.diagnostics };
|
|
58
59
|
}
|
package/cli.js
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const schema_1 = require("@latex-kit/schema");
|
|
4
5
|
const inquirerer_1 = require("inquirerer");
|
|
6
|
+
const path_1 = require("path");
|
|
5
7
|
const commands_1 = require("./commands");
|
|
8
|
+
// Project files import 'latex-kit' (and zod through it). Point those names at
|
|
9
|
+
// this CLI's own copies, so a project works without its own node_modules and
|
|
10
|
+
// every file shares one zod.
|
|
11
|
+
const schemaDir = (0, path_1.dirname)(require.resolve('@latex-kit/schema'));
|
|
12
|
+
(0, schema_1.setModuleAliases)({
|
|
13
|
+
'latex-kit': __dirname,
|
|
14
|
+
'@latex-kit/schema': schemaDir,
|
|
15
|
+
zod: (0, path_1.dirname)(require.resolve('zod/package.json', { paths: [schemaDir] })),
|
|
16
|
+
});
|
|
6
17
|
const app = new inquirerer_1.CLI(commands_1.commands, {
|
|
7
18
|
noTty: !process.stdin.isTTY,
|
|
8
19
|
// inquirerer answers --version itself; make it report latex-kit
|
|
9
20
|
version: (0, commands_1.version)(),
|
|
10
21
|
minimistOpts: {
|
|
11
22
|
alias: { h: 'help', v: 'version', o: 'out', e: 'engine' },
|
|
12
|
-
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only'],
|
|
13
|
-
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template'],
|
|
14
|
-
default: { strict: undefined },
|
|
23
|
+
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only', 'force', 'schema', 'paper', 'check'],
|
|
24
|
+
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template', 'name', 'against', 'only', 'locale', 'signatures'],
|
|
25
|
+
default: { strict: undefined, schema: true },
|
|
15
26
|
},
|
|
16
27
|
});
|
|
17
28
|
app.run().catch((error) => {
|
package/commands.js
CHANGED
|
@@ -25,11 +25,18 @@ function version() {
|
|
|
25
25
|
return 'unknown';
|
|
26
26
|
}
|
|
27
27
|
const COMMANDS = {
|
|
28
|
+
init: project_commands_1.initCommand,
|
|
29
|
+
new: project_commands_1.newCommand,
|
|
28
30
|
build: project_commands_1.buildCommand,
|
|
31
|
+
watch: project_commands_1.watchCommand,
|
|
32
|
+
clean: project_commands_1.cleanCommand,
|
|
29
33
|
check: project_commands_1.checkCommand,
|
|
30
34
|
fields: project_commands_1.fieldsCommand,
|
|
31
35
|
typegen: project_commands_1.typegenCommand,
|
|
36
|
+
import: project_commands_1.importCommand,
|
|
37
|
+
diff: project_commands_1.diffCommand,
|
|
32
38
|
sty: project_commands_1.styCommand,
|
|
39
|
+
lock: project_commands_1.lockCommand,
|
|
33
40
|
render: render_1.renderCommand,
|
|
34
41
|
doctor: doctor_1.doctorCommand,
|
|
35
42
|
};
|
package/config.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import type { Backend,
|
|
1
|
+
import type { Backend, EngineChoice } from '@latex-kit/render';
|
|
2
2
|
/** latex-kit.config.ts (or .js, .mjs, .cjs, .json) at the project root. */
|
|
3
3
|
export interface LatexKitConfig {
|
|
4
4
|
/** One folder per template: `<templates>/<name>/template.tex` and optional `schema.ts`. Default `templates`. */
|
|
5
5
|
templates?: string;
|
|
6
6
|
/** Shared pieces for \snippet{name}. Default `snippets`. */
|
|
7
7
|
snippets?: string;
|
|
8
|
+
/** The clause library for \\clause{id}: `<clauses>/<id>.tex` or `.ts`. Default `clauses`. */
|
|
9
|
+
clauses?: string;
|
|
10
|
+
/** TypeScript components for \\component{name}{path}: `<components>/<name>.ts`. Default `components`. */
|
|
11
|
+
components?: string;
|
|
8
12
|
/** Shared records that references point at: `<data>/<collection>/<id>.json`. Default `data`. */
|
|
9
13
|
data?: string;
|
|
10
14
|
/** Document records: `<documents>/<name>.json|yaml|ts`. Default `documents`. */
|
|
@@ -13,12 +17,19 @@ export interface LatexKitConfig {
|
|
|
13
17
|
out?: string;
|
|
14
18
|
/** Default locale for dates, numbers and money. Default `en-US`. */
|
|
15
19
|
locale?: string;
|
|
16
|
-
|
|
20
|
+
/** Default `auto`: chosen from the packages each document loads. */
|
|
21
|
+
engine?: EngineChoice;
|
|
22
|
+
/**
|
|
23
|
+
* Folder with signature images for signed builds (`<name>.png|pdf|jpg`).
|
|
24
|
+
* Keep it outside the repository; `--signatures` and LATEX_KIT_SIGNATURES
|
|
25
|
+
* override it. A leading ~ is the home folder.
|
|
26
|
+
*/
|
|
27
|
+
signatures?: string;
|
|
17
28
|
backend?: Backend;
|
|
18
29
|
image?: string;
|
|
19
30
|
platform?: string | null;
|
|
20
31
|
}
|
|
21
|
-
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform'> & {
|
|
32
|
+
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'clauses' | 'components' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform' | 'signatures'> & {
|
|
22
33
|
file: string | null;
|
|
23
34
|
};
|
|
24
35
|
export declare function defineConfig(config: LatexKitConfig): LatexKitConfig;
|
package/config.js
CHANGED
|
@@ -10,7 +10,7 @@ function defineConfig(config) {
|
|
|
10
10
|
return config;
|
|
11
11
|
}
|
|
12
12
|
exports.CONFIG_FILES = ['latex-kit.config.ts', 'latex-kit.config.mts', 'latex-kit.config.js', 'latex-kit.config.mjs', 'latex-kit.config.cjs', 'latex-kit.config.json'];
|
|
13
|
-
const DEFAULTS = { templates: 'templates', snippets: 'snippets', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
13
|
+
const DEFAULTS = { templates: 'templates', snippets: 'snippets', clauses: 'clauses', components: 'components', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
14
14
|
async function loadConfig(root) {
|
|
15
15
|
for (const name of exports.CONFIG_FILES) {
|
|
16
16
|
const file = (0, path_1.join)((0, path_1.resolve)(root), name);
|
package/diff.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { EngineChoice } from '@latex-kit/render';
|
|
2
|
+
import { Variant } from '@latex-kit/template';
|
|
3
|
+
import { Project } from './project';
|
|
4
|
+
export interface DiffOptions {
|
|
5
|
+
variant?: Variant;
|
|
6
|
+
engine?: EngineChoice;
|
|
7
|
+
backend?: 'docker' | 'local';
|
|
8
|
+
image?: string;
|
|
9
|
+
platform?: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface DiffResult {
|
|
12
|
+
/** The redline PDF. */
|
|
13
|
+
pdf: string;
|
|
14
|
+
/** latexdiff's marked-up .tex. */
|
|
15
|
+
tex: string;
|
|
16
|
+
old: string;
|
|
17
|
+
new: string;
|
|
18
|
+
}
|
|
19
|
+
/** Redline two .tex files inside the project. */
|
|
20
|
+
export declare function diffFiles(project: Project, oldFile: string, newFile: string, options?: DiffOptions): Promise<DiffResult>;
|
|
21
|
+
/**
|
|
22
|
+
* Redline a document against an earlier commit: build it as it was at `ref`
|
|
23
|
+
* (in a temporary git worktree) and as it is now, then latexdiff the two.
|
|
24
|
+
*/
|
|
25
|
+
export declare function diffDocument(project: Project, file: string, ref: string, options?: DiffOptions): Promise<DiffResult>;
|
package/diff.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.diffFiles = diffFiles;
|
|
4
|
+
exports.diffDocument = diffDocument;
|
|
5
|
+
const render_1 = require("@latex-kit/render");
|
|
6
|
+
const template_1 = require("@latex-kit/template");
|
|
7
|
+
const child_process_1 = require("child_process");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const os_1 = require("os");
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const build_1 = require("./build");
|
|
12
|
+
const project_1 = require("./project");
|
|
13
|
+
function git(cwd, args) {
|
|
14
|
+
return (0, child_process_1.execFileSync)('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
|
|
15
|
+
}
|
|
16
|
+
/** latexdiff old → new in `dir`, then render the redline. */
|
|
17
|
+
async function redline(project, oldTex, newTex, dir, name, texInputs, options) {
|
|
18
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
19
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(dir, 'latex-kit.sty'), template_1.LATEX_KIT_STY);
|
|
20
|
+
const tex = (0, path_1.join)(dir, `${name}.tex`);
|
|
21
|
+
const rel = (p) => (0, path_1.relative)(project.root, p);
|
|
22
|
+
await (0, render_1.runLatexdiff)({ root: project.root, old: rel(oldTex), new: rel(newTex), output: rel(tex), backend: options.backend, image: project.image(options.image), platform: options.platform });
|
|
23
|
+
const r = await (0, render_1.render)({
|
|
24
|
+
file: tex,
|
|
25
|
+
root: project.root,
|
|
26
|
+
outDir: rel(dir),
|
|
27
|
+
engine: options.engine ?? project.config.engine ?? 'auto',
|
|
28
|
+
backend: options.backend ?? project.config.backend,
|
|
29
|
+
image: project.image(options.image),
|
|
30
|
+
platform: options.platform !== undefined ? options.platform : project.config.platform,
|
|
31
|
+
texInputs: [rel(dir), ...texInputs.map(rel)],
|
|
32
|
+
});
|
|
33
|
+
return { pdf: r.pdf, tex, old: oldTex, new: newTex };
|
|
34
|
+
}
|
|
35
|
+
/** Redline two .tex files inside the project. */
|
|
36
|
+
async function diffFiles(project, oldFile, newFile, options = {}) {
|
|
37
|
+
for (const f of [oldFile, newFile])
|
|
38
|
+
if (!(0, fs_1.existsSync)(f))
|
|
39
|
+
throw new Error(`no such file: ${project.rel(f)}`);
|
|
40
|
+
const name = `${(0, path_1.basename)(newFile, '.tex')}-vs-${(0, path_1.basename)(oldFile, '.tex')}`;
|
|
41
|
+
const dir = (0, path_1.join)(project.outDir, 'diff', name);
|
|
42
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
43
|
+
// copies inside out/, so the files are always visible to Docker
|
|
44
|
+
const oldCopy = (0, path_1.join)(dir, 'old.tex');
|
|
45
|
+
const newCopy = (0, path_1.join)(dir, 'new.tex');
|
|
46
|
+
(0, fs_1.copyFileSync)(oldFile, oldCopy);
|
|
47
|
+
(0, fs_1.copyFileSync)(newFile, newCopy);
|
|
48
|
+
return redline(project, oldCopy, newCopy, dir, name, [(0, path_1.dirname)(newFile)], options);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Redline a document against an earlier commit: build it as it was at `ref`
|
|
52
|
+
* (in a temporary git worktree) and as it is now, then latexdiff the two.
|
|
53
|
+
*/
|
|
54
|
+
async function diffDocument(project, file, ref, options = {}) {
|
|
55
|
+
const variant = options.variant ?? 'final';
|
|
56
|
+
let top;
|
|
57
|
+
try {
|
|
58
|
+
top = git(project.root, ['rev-parse', '--show-toplevel']);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new Error('diff --against needs the project to be in a git repository');
|
|
62
|
+
}
|
|
63
|
+
const relDoc = (0, path_1.relative)(top, file);
|
|
64
|
+
const relProject = (0, path_1.relative)(top, project.root);
|
|
65
|
+
let sha;
|
|
66
|
+
try {
|
|
67
|
+
sha = git(top, ['rev-parse', '--short', `${ref}^{commit}`]);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new Error(`unknown git ref: ${ref}`);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
git(top, ['cat-file', '-e', `${sha}:${relDoc}`]);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
throw new Error(`${relDoc} does not exist at ${ref}`);
|
|
77
|
+
}
|
|
78
|
+
const name = project.documentName(file);
|
|
79
|
+
const dir = (0, path_1.join)(project.outDir, name, 'diff');
|
|
80
|
+
const common = { variant, texOnly: true, strict: false, docIdFallback: 'UNASSIGNED' };
|
|
81
|
+
const current = await (0, build_1.buildDocument)(project, file, { ...common, outDir: (0, path_1.join)(dir, 'new') }).catch((e) => {
|
|
82
|
+
if (e instanceof build_1.BuildError)
|
|
83
|
+
throw new build_1.BuildError(`the current ${name} does not build: ${e.message}`, e.diagnostics);
|
|
84
|
+
throw e;
|
|
85
|
+
});
|
|
86
|
+
const tree = (0, fs_1.mkdtempSync)((0, path_1.join)((0, os_1.tmpdir)(), 'latex-kit-diff-'));
|
|
87
|
+
try {
|
|
88
|
+
git(top, ['worktree', 'add', '--detach', '--quiet', tree, sha]);
|
|
89
|
+
const oldProject = await project_1.Project.open((0, path_1.join)(tree, relProject));
|
|
90
|
+
const old = await (0, build_1.buildDocument)(oldProject, (0, path_1.join)(tree, relDoc), { ...common, outDir: (0, path_1.join)(dir, 'old') }).catch((e) => {
|
|
91
|
+
if (e instanceof build_1.BuildError)
|
|
92
|
+
throw new build_1.BuildError(`${name} at ${ref} (${sha}) does not build: ${e.message}`, e.diagnostics);
|
|
93
|
+
throw e;
|
|
94
|
+
});
|
|
95
|
+
const template = await project.template(current.template);
|
|
96
|
+
return await redline(project, old.tex, current.tex, dir, `${name}-vs-${sha}`, [template.dir], options);
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
try {
|
|
100
|
+
git(top, ['worktree', 'remove', '--force', tree]);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
(0, fs_1.rmSync)(tree, { recursive: true, force: true });
|
|
104
|
+
try {
|
|
105
|
+
git(top, ['worktree', 'prune']);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// nothing left to clean
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|