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/lock.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LockError = exports.LOCK_FILE = void 0;
|
|
4
|
+
exports.currentPins = currentPins;
|
|
5
|
+
exports.readLock = readLock;
|
|
6
|
+
exports.writeLock = writeLock;
|
|
7
|
+
const render_1 = require("@latex-kit/render");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const path_1 = require("path");
|
|
10
|
+
/**
|
|
11
|
+
* latex-kit.lock: the exact images a project builds with. Commit it. A newer
|
|
12
|
+
* latex-kit with a newer default image keeps building this project with the
|
|
13
|
+
* locked one, so PDFs do not change until `latex-kit lock` moves the pins.
|
|
14
|
+
*/
|
|
15
|
+
exports.LOCK_FILE = 'latex-kit.lock';
|
|
16
|
+
class LockError extends Error {
|
|
17
|
+
name = 'LockError';
|
|
18
|
+
}
|
|
19
|
+
exports.LockError = LockError;
|
|
20
|
+
const PINNED = /@sha256:[0-9a-f]{64}$/;
|
|
21
|
+
/** The pins this version of latex-kit would write. */
|
|
22
|
+
function currentPins() {
|
|
23
|
+
return { lockfileVersion: 1, image: render_1.DEFAULT_IMAGE, pandocImage: render_1.PANDOC_IMAGE };
|
|
24
|
+
}
|
|
25
|
+
function readLock(root) {
|
|
26
|
+
const file = (0, path_1.join)(root, exports.LOCK_FILE);
|
|
27
|
+
if (!(0, fs_1.existsSync)(file))
|
|
28
|
+
return null;
|
|
29
|
+
let raw;
|
|
30
|
+
try {
|
|
31
|
+
raw = JSON.parse((0, fs_1.readFileSync)(file, 'utf8'));
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
throw new LockError(`${exports.LOCK_FILE} is not valid JSON: ${e.message}`);
|
|
35
|
+
}
|
|
36
|
+
const l = raw;
|
|
37
|
+
if (!l || typeof l !== 'object' || l.lockfileVersion !== 1)
|
|
38
|
+
throw new LockError(`${exports.LOCK_FILE}: unknown lockfileVersion (this latex-kit reads version 1)`);
|
|
39
|
+
for (const k of ['image', 'pandocImage']) {
|
|
40
|
+
if (typeof l[k] !== 'string' || !PINNED.test(l[k]))
|
|
41
|
+
throw new LockError(`${exports.LOCK_FILE}: ${k} must be an image pinned by digest (…@sha256:<64 hex>)`);
|
|
42
|
+
}
|
|
43
|
+
return l;
|
|
44
|
+
}
|
|
45
|
+
/** Write the current pins. Returns the previous lock, if any. */
|
|
46
|
+
function writeLock(root) {
|
|
47
|
+
let previous = null;
|
|
48
|
+
try {
|
|
49
|
+
previous = readLock(root);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// an unreadable lock is replaced
|
|
53
|
+
}
|
|
54
|
+
const lock = currentPins();
|
|
55
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(root, exports.LOCK_FILE), JSON.stringify(lock, null, 2) + '\n');
|
|
56
|
+
return { lock, previous };
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "latex-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Build typed, reproducible LaTeX documents from the command line",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"main": "index.js",
|
|
@@ -44,12 +44,13 @@
|
|
|
44
44
|
"latex-kit": "cli.js"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@latex-kit/
|
|
48
|
-
"@latex-kit/
|
|
49
|
-
"@latex-kit/
|
|
47
|
+
"@latex-kit/components": "0.2.0",
|
|
48
|
+
"@latex-kit/render": "0.5.0",
|
|
49
|
+
"@latex-kit/schema": "0.4.0",
|
|
50
|
+
"@latex-kit/template": "0.4.0",
|
|
50
51
|
"inquirerer": "^4.9.5",
|
|
51
52
|
"minimist": "^1.2.8",
|
|
52
53
|
"yanse": "^0.2.3"
|
|
53
54
|
},
|
|
54
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "b169d7b7a018ec26e5099e021b3529dcf6c006fa"
|
|
55
56
|
}
|
package/project-commands.d.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import type { ParsedArgs } from 'minimist';
|
|
2
|
+
/** `--only nda-*,invoice-0001`: comma-separated names, * matches anything. */
|
|
3
|
+
export declare function onlyMatcher(only: unknown): ((name: string) => boolean) | null;
|
|
2
4
|
export declare function buildCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
3
5
|
export declare function checkCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
4
6
|
export declare function fieldsCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
5
7
|
export declare function typegenCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
6
8
|
export declare function styCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
9
|
+
export declare function importCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
10
|
+
export declare function diffCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
11
|
+
/** Pin the TeX and pandoc images in latex-kit.lock (commit it). */
|
|
12
|
+
export declare function lockCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
13
|
+
export declare function initCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
14
|
+
export declare function newCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
15
|
+
export declare function cleanCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
|
16
|
+
/**
|
|
17
|
+
* Rebuild when templates, snippets, clauses, data or records change. Builds
|
|
18
|
+
* drafts unless --variant says otherwise; never mints IDs (use build --final).
|
|
19
|
+
*/
|
|
20
|
+
export declare function watchCommand(argv: Partial<ParsedArgs>): Promise<number>;
|
package/project-commands.js
CHANGED
|
@@ -3,11 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.onlyMatcher = onlyMatcher;
|
|
6
7
|
exports.buildCommand = buildCommand;
|
|
7
8
|
exports.checkCommand = checkCommand;
|
|
8
9
|
exports.fieldsCommand = fieldsCommand;
|
|
9
10
|
exports.typegenCommand = typegenCommand;
|
|
10
11
|
exports.styCommand = styCommand;
|
|
12
|
+
exports.importCommand = importCommand;
|
|
13
|
+
exports.diffCommand = diffCommand;
|
|
14
|
+
exports.lockCommand = lockCommand;
|
|
15
|
+
exports.initCommand = initCommand;
|
|
16
|
+
exports.newCommand = newCommand;
|
|
17
|
+
exports.cleanCommand = cleanCommand;
|
|
18
|
+
exports.watchCommand = watchCommand;
|
|
11
19
|
const render_1 = require("@latex-kit/render");
|
|
12
20
|
const schema_1 = require("@latex-kit/schema");
|
|
13
21
|
const template_1 = require("@latex-kit/template");
|
|
@@ -17,7 +25,12 @@ const yanse_1 = __importDefault(require("yanse"));
|
|
|
17
25
|
const args_1 = require("./args");
|
|
18
26
|
const build_1 = require("./build");
|
|
19
27
|
const check_1 = require("./check");
|
|
28
|
+
const commands_1 = require("./commands");
|
|
29
|
+
const diff_1 = require("./diff");
|
|
30
|
+
const import_1 = require("./import");
|
|
31
|
+
const lock_1 = require("./lock");
|
|
20
32
|
const project_1 = require("./project");
|
|
33
|
+
const scaffold_1 = require("./scaffold");
|
|
21
34
|
function printDiagnostics(ds, json) {
|
|
22
35
|
if (json)
|
|
23
36
|
return;
|
|
@@ -25,12 +38,35 @@ function printDiagnostics(ds, json) {
|
|
|
25
38
|
console.error((d.severity === 'error' ? yanse_1.default.red : yanse_1.default.yellow)((0, template_1.formatDiagnostic)(d)));
|
|
26
39
|
}
|
|
27
40
|
const rel = (p) => (p ? (0, path_1.relative)(process.cwd(), p) : null);
|
|
41
|
+
/** `--only nda-*,invoice-0001`: comma-separated names, * matches anything. */
|
|
42
|
+
function onlyMatcher(only) {
|
|
43
|
+
if (only === undefined || only === null || only === '' || only === true)
|
|
44
|
+
return null;
|
|
45
|
+
const patterns = (Array.isArray(only) ? only : [only]).flatMap((o) => String(o).split(',')).map((p) => p.trim()).filter(Boolean);
|
|
46
|
+
if (patterns.length === 0)
|
|
47
|
+
return null;
|
|
48
|
+
const res = patterns.map((p) => new RegExp(`^${p.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*')}$`));
|
|
49
|
+
return (name) => res.some((r) => r.test(name));
|
|
50
|
+
}
|
|
51
|
+
function checkLocale(locale) {
|
|
52
|
+
if (locale === undefined)
|
|
53
|
+
return undefined;
|
|
54
|
+
try {
|
|
55
|
+
return Intl.getCanonicalLocales(String(locale))[0];
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
throw new args_1.UsageError(`--locale ${locale} is not a language tag (like en-US or ja-JP)`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
28
61
|
async function buildCommand(argv) {
|
|
29
62
|
const variant = (argv.variant ?? (argv.final ? 'final' : 'draft'));
|
|
30
63
|
if (!template_1.VARIANTS.includes(variant))
|
|
31
64
|
throw new args_1.UsageError(`--variant must be one of ${template_1.VARIANTS.join(', ')}`);
|
|
32
|
-
if (argv.engine && !render_1.ENGINES.includes(argv.engine))
|
|
33
|
-
throw new args_1.UsageError(`--engine must be one of ${render_1.ENGINES.join(', ')}`);
|
|
65
|
+
if (argv.engine && argv.engine !== 'auto' && !render_1.ENGINES.includes(argv.engine))
|
|
66
|
+
throw new args_1.UsageError(`--engine must be auto or one of ${render_1.ENGINES.join(', ')}`);
|
|
67
|
+
if (argv.signatures !== undefined && variant !== 'signed')
|
|
68
|
+
throw new args_1.UsageError('--signatures only applies to --variant signed');
|
|
69
|
+
const only = onlyMatcher(argv.only);
|
|
34
70
|
const project = await project_1.Project.open(argv.root);
|
|
35
71
|
const options = {
|
|
36
72
|
variant,
|
|
@@ -41,13 +77,16 @@ async function buildCommand(argv) {
|
|
|
41
77
|
platform: argv.platform,
|
|
42
78
|
verify: Boolean(argv.verify),
|
|
43
79
|
texOnly: Boolean(argv['tex-only']),
|
|
80
|
+
locale: checkLocale(argv.locale),
|
|
81
|
+
force: Boolean(argv.force),
|
|
82
|
+
signatures: argv.signatures,
|
|
44
83
|
};
|
|
45
84
|
const json = Boolean(argv.json);
|
|
46
85
|
const log = (s) => !json && console.error(s);
|
|
47
86
|
const targets = [];
|
|
48
87
|
const files = (argv._ ?? []).map(String);
|
|
49
88
|
if (variant === 'blank' && files.length === 0) {
|
|
50
|
-
const names = argv.template ? [String(argv.template)] : project.templateNames();
|
|
89
|
+
const names = (argv.template ? [String(argv.template)] : project.templateNames()).filter((n) => !only || only(n));
|
|
51
90
|
for (const n of names)
|
|
52
91
|
targets.push({ label: `template ${n}`, run: () => (0, build_1.buildBlank)(project, n, options) });
|
|
53
92
|
}
|
|
@@ -64,13 +103,21 @@ async function buildCommand(argv) {
|
|
|
64
103
|
keep.push(f);
|
|
65
104
|
docs = keep;
|
|
66
105
|
}
|
|
106
|
+
if (only)
|
|
107
|
+
docs = docs.filter((f) => only(project.documentName(f)));
|
|
67
108
|
for (const f of docs)
|
|
68
109
|
targets.push({ label: rel(f), run: () => (0, build_1.buildDocument)(project, f, options) });
|
|
69
110
|
}
|
|
70
111
|
if (targets.length === 0) {
|
|
112
|
+
if (only) {
|
|
113
|
+
log(yanse_1.default.red(`--only ${argv.only} matched nothing`));
|
|
114
|
+
return 1;
|
|
115
|
+
}
|
|
71
116
|
log(yanse_1.default.yellow(`nothing to build (no documents in ${project.rel(project.documentsDir)}/)`));
|
|
72
117
|
return 0;
|
|
73
118
|
}
|
|
119
|
+
for (const d of project.codeDiagnostics)
|
|
120
|
+
printDiagnostics([d], json);
|
|
74
121
|
const results = [];
|
|
75
122
|
let failed = 0;
|
|
76
123
|
for (const t of targets) {
|
|
@@ -83,7 +130,8 @@ async function buildCommand(argv) {
|
|
|
83
130
|
failed++;
|
|
84
131
|
results.push({ ...r, tex: rel(r.tex), pdf: rel(r.pdf), diagnostics: r.diagnostics.length });
|
|
85
132
|
if (!json) {
|
|
86
|
-
|
|
133
|
+
const notes = [r.docId, r.cached ? 'unchanged' : null, r.engine && r.engineReason !== 'chosen' && r.engine !== 'lualatex' ? `${r.engine}, ${r.engineReason?.replace(/^auto: /, '')}` : null].filter(Boolean).join(' ');
|
|
134
|
+
console.log(`${bad ? yanse_1.default.red('✗') : yanse_1.default.green('✓')} ${rel(r.pdf ?? r.tex)}${notes ? yanse_1.default.dim(` ${notes}`) : ''}`);
|
|
87
135
|
if (r.minted)
|
|
88
136
|
console.log(yanse_1.default.dim(` minted ${r.docId} and saved it in ${t.label}`));
|
|
89
137
|
if (r.verified !== undefined)
|
|
@@ -140,7 +188,7 @@ async function fieldsCommand(argv) {
|
|
|
140
188
|
const width = Math.max(...fields.map((f) => f.absolute.length), 4);
|
|
141
189
|
for (const f of fields) {
|
|
142
190
|
const filters = f.filters.length ? yanse_1.default.cyan(` | ${f.filters.join(' | ')}`) : '';
|
|
143
|
-
console.log(`${f.absolute.padEnd(width)} ${yanse_1.default.dim(f.kind.padEnd(
|
|
191
|
+
console.log(`${f.absolute.padEnd(width)} ${yanse_1.default.dim(f.kind.padEnd(9))} ${yanse_1.default.dim(`${f.file}:${f.line}`)}${filters}`);
|
|
144
192
|
}
|
|
145
193
|
return diagnostics.some((d) => d.severity === 'error') ? 1 : 0;
|
|
146
194
|
}
|
|
@@ -171,3 +219,188 @@ async function styCommand(argv) {
|
|
|
171
219
|
console.log(`${yanse_1.default.green('✓')} ${rel(file)} (lets editors compile templates as blank previews)`);
|
|
172
220
|
return 0;
|
|
173
221
|
}
|
|
222
|
+
async function importCommand(argv) {
|
|
223
|
+
const file = argv._?.[0];
|
|
224
|
+
if (!file)
|
|
225
|
+
throw new args_1.UsageError('usage: latex-kit import <file.docx|.md|.html|.tex> [--name <template>] [--force]');
|
|
226
|
+
const project = await project_1.Project.open(argv.root);
|
|
227
|
+
const r = await (0, import_1.importDocument)(project, String(file), {
|
|
228
|
+
name: argv.name,
|
|
229
|
+
force: Boolean(argv.force),
|
|
230
|
+
backend: argv.local ? 'local' : undefined,
|
|
231
|
+
schema: argv.schema !== false,
|
|
232
|
+
});
|
|
233
|
+
console.log(`${yanse_1.default.green('✓')} ${rel(r.template)}`);
|
|
234
|
+
if (r.media)
|
|
235
|
+
console.log(yanse_1.default.dim(` images in ${rel(r.media)}`));
|
|
236
|
+
console.log(yanse_1.default.dim(` ${r.converted.length} placeholder${r.converted.length === 1 ? '' : 's'} became \\field{…}`));
|
|
237
|
+
for (const s of r.skipped)
|
|
238
|
+
console.log(yanse_1.default.yellow(` line ${s.line}: left ${s.from} as is (not a valid field path)`));
|
|
239
|
+
if (r.schema)
|
|
240
|
+
console.log(yanse_1.default.dim(` starter schema: ${rel(r.schema)} (every field is a string; tighten it)`));
|
|
241
|
+
console.log(yanse_1.default.dim(` next: latex-kit fields ${r.name}, then latex-kit build --variant blank --template ${r.name}`));
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
|
244
|
+
async function diffCommand(argv) {
|
|
245
|
+
const args = (argv._ ?? []).map(String);
|
|
246
|
+
const variant = (argv.variant ?? 'final');
|
|
247
|
+
if (!template_1.VARIANTS.includes(variant))
|
|
248
|
+
throw new args_1.UsageError(`--variant must be one of ${template_1.VARIANTS.join(', ')}`);
|
|
249
|
+
const project = await project_1.Project.open(argv.root);
|
|
250
|
+
const options = { variant, engine: argv.engine, backend: argv.local ? 'local' : undefined, image: argv.image, platform: argv.platform };
|
|
251
|
+
const abs = (p) => (0, path_1.join)(process.cwd(), p);
|
|
252
|
+
let result;
|
|
253
|
+
try {
|
|
254
|
+
if (args.length === 2 && !argv.against) {
|
|
255
|
+
result = await (0, diff_1.diffFiles)(project, abs(args[0]), abs(args[1]), options);
|
|
256
|
+
}
|
|
257
|
+
else if (args.length === 1 && argv.against) {
|
|
258
|
+
result = await (0, diff_1.diffDocument)(project, abs(args[0]), String(argv.against), options);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
throw new args_1.UsageError('usage: latex-kit diff <old.tex> <new.tex>\n latex-kit diff <document> --against <git-ref>');
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
if (e instanceof build_1.BuildError) {
|
|
266
|
+
printDiagnostics(e.diagnostics, Boolean(argv.json));
|
|
267
|
+
console.error(yanse_1.default.red(`✗ ${e.message}`));
|
|
268
|
+
return 1;
|
|
269
|
+
}
|
|
270
|
+
throw e;
|
|
271
|
+
}
|
|
272
|
+
if (argv.json)
|
|
273
|
+
console.log(JSON.stringify({ pdf: rel(result.pdf), tex: rel(result.tex), old: rel(result.old), new: rel(result.new) }, null, 2));
|
|
274
|
+
else
|
|
275
|
+
console.log(`${yanse_1.default.green('✓')} ${rel(result.pdf)} ${yanse_1.default.dim('(insertions underlined in blue, deletions struck through in red)')}`);
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
/** Pin the TeX and pandoc images in latex-kit.lock (commit it). */
|
|
279
|
+
async function lockCommand(argv) {
|
|
280
|
+
const project = await project_1.Project.open(argv.root);
|
|
281
|
+
const pins = (0, lock_1.currentPins)();
|
|
282
|
+
const file = (0, path_1.join)(project.root, lock_1.LOCK_FILE);
|
|
283
|
+
if (argv.check) {
|
|
284
|
+
if (!project.lock) {
|
|
285
|
+
console.log(yanse_1.default.yellow(`no ${lock_1.LOCK_FILE}: builds use this latex-kit's default image`));
|
|
286
|
+
return 1;
|
|
287
|
+
}
|
|
288
|
+
const stale = project.lock.image !== pins.image || project.lock.pandocImage !== pins.pandocImage;
|
|
289
|
+
console.log(stale ? yanse_1.default.yellow(`${rel(file)} pins older images than this latex-kit's defaults; run latex-kit lock to move them (PDF bytes will change)`) : `${yanse_1.default.green('✓')} ${rel(file)} matches this latex-kit`);
|
|
290
|
+
return stale ? 1 : 0;
|
|
291
|
+
}
|
|
292
|
+
const { lock, previous } = (0, lock_1.writeLock)(project.root);
|
|
293
|
+
console.log(`${yanse_1.default.green('✓')} ${rel(file)}`);
|
|
294
|
+
if (previous && previous.image !== lock.image)
|
|
295
|
+
console.log(yanse_1.default.dim(` image ${previous.image}\n → ${lock.image}\n rebuilt PDFs will differ from earlier ones`));
|
|
296
|
+
else if (!previous)
|
|
297
|
+
console.log(yanse_1.default.dim(` image ${lock.image}`));
|
|
298
|
+
else
|
|
299
|
+
console.log(yanse_1.default.dim(' already up to date'));
|
|
300
|
+
return 0;
|
|
301
|
+
}
|
|
302
|
+
async function initCommand(argv) {
|
|
303
|
+
const dir = (0, path_1.join)(process.cwd(), argv._?.[0] ? String(argv._[0]) : '.');
|
|
304
|
+
const r = (0, scaffold_1.initProject)(dir, { paper: Boolean(argv.paper), force: Boolean(argv.force), version: (0, commands_1.version)() });
|
|
305
|
+
if (!argv.paper && !(0, fs_1.existsSync)((0, path_1.join)(dir, lock_1.LOCK_FILE))) {
|
|
306
|
+
(0, lock_1.writeLock)(dir);
|
|
307
|
+
r.written.push((0, path_1.join)(dir, lock_1.LOCK_FILE));
|
|
308
|
+
}
|
|
309
|
+
for (const f of r.written)
|
|
310
|
+
console.log(`${yanse_1.default.green('+')} ${rel(f)}`);
|
|
311
|
+
for (const f of r.skipped)
|
|
312
|
+
console.log(yanse_1.default.dim(` kept ${rel(f)} (exists)`));
|
|
313
|
+
const cd = rel(dir) === '.' ? '' : `cd ${rel(dir)} && `;
|
|
314
|
+
console.log(argv.paper
|
|
315
|
+
? yanse_1.default.dim(`\nnext: ${cd}latex-kit render paper/main.tex --verify`)
|
|
316
|
+
: yanse_1.default.dim(`\nnext: ${cd}latex-kit check && latex-kit build`));
|
|
317
|
+
return 0;
|
|
318
|
+
}
|
|
319
|
+
async function newCommand(argv) {
|
|
320
|
+
const [kind, name] = (argv._ ?? []).map(String);
|
|
321
|
+
const project = await project_1.Project.open(argv.root);
|
|
322
|
+
if (kind === 'template' && name) {
|
|
323
|
+
const r = (0, scaffold_1.newTemplate)(project, name, Boolean(argv.force));
|
|
324
|
+
for (const f of r.written)
|
|
325
|
+
console.log(`${yanse_1.default.green('+')} ${rel(f)}`);
|
|
326
|
+
console.log(yanse_1.default.dim(`\nnext: latex-kit new document <name> --template ${name}`));
|
|
327
|
+
return 0;
|
|
328
|
+
}
|
|
329
|
+
if (kind === 'document' && name) {
|
|
330
|
+
if (!argv.template)
|
|
331
|
+
throw new args_1.UsageError('usage: latex-kit new document <name> --template <template>');
|
|
332
|
+
const r = await (0, scaffold_1.newDocument)(project, name, String(argv.template), Boolean(argv.force));
|
|
333
|
+
for (const f of r.written)
|
|
334
|
+
console.log(`${yanse_1.default.green('+')} ${rel(f)}`);
|
|
335
|
+
if (r.schemaFile)
|
|
336
|
+
console.log(yanse_1.default.dim(` editors autocomplete it from ${rel(r.schemaFile)}`));
|
|
337
|
+
console.log(yanse_1.default.dim(`\nnext: fill in the data, then latex-kit build ${r.written[0] ? rel(r.written[0]) : ''}`));
|
|
338
|
+
return 0;
|
|
339
|
+
}
|
|
340
|
+
throw new args_1.UsageError('usage: latex-kit new template <name>\n latex-kit new document <name> --template <template>');
|
|
341
|
+
}
|
|
342
|
+
async function cleanCommand(argv) {
|
|
343
|
+
const project = await project_1.Project.open(argv.root);
|
|
344
|
+
const rel = (0, path_1.relative)(project.root, project.outDir);
|
|
345
|
+
if (rel === '' || rel.startsWith('..') || (0, path_1.isAbsolute)(rel))
|
|
346
|
+
throw new args_1.UsageError(`refusing to delete ${project.outDir}: the out folder must be inside the project`);
|
|
347
|
+
if (!(0, fs_1.existsSync)(project.outDir)) {
|
|
348
|
+
console.log(yanse_1.default.dim(`nothing to clean (${project.rel(project.outDir)}/ does not exist)`));
|
|
349
|
+
return 0;
|
|
350
|
+
}
|
|
351
|
+
(0, fs_1.rmSync)(project.outDir, { recursive: true, force: true });
|
|
352
|
+
console.log(`${yanse_1.default.green('✓')} removed ${project.rel(project.outDir)}/`);
|
|
353
|
+
return 0;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Rebuild when templates, snippets, clauses, data or records change. Builds
|
|
357
|
+
* drafts unless --variant says otherwise; never mints IDs (use build --final).
|
|
358
|
+
*/
|
|
359
|
+
async function watchCommand(argv) {
|
|
360
|
+
const variant = (argv.variant ?? 'draft');
|
|
361
|
+
if (variant === 'final' || variant === 'signed')
|
|
362
|
+
throw new args_1.UsageError('watch builds drafts or blanks; run latex-kit build --final when you are done');
|
|
363
|
+
const project = await project_1.Project.open(argv.root);
|
|
364
|
+
const dirs = [project.config.templates, project.config.snippets, project.config.clauses, project.config.data, project.config.documents]
|
|
365
|
+
.map((d) => project.path(d))
|
|
366
|
+
.filter((d) => (0, fs_1.existsSync)(d));
|
|
367
|
+
if (dirs.length === 0)
|
|
368
|
+
throw new args_1.UsageError('nothing to watch: no templates/, data/ or documents/ here');
|
|
369
|
+
let running = false;
|
|
370
|
+
let again = false;
|
|
371
|
+
let timer = null;
|
|
372
|
+
const run = async () => {
|
|
373
|
+
if (running) {
|
|
374
|
+
again = true;
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
running = true;
|
|
378
|
+
console.log(yanse_1.default.dim(`\n— ${new Date().toLocaleTimeString()} —`));
|
|
379
|
+
try {
|
|
380
|
+
await buildCommand({ ...argv, variant });
|
|
381
|
+
}
|
|
382
|
+
catch (e) {
|
|
383
|
+
console.error(yanse_1.default.red(e.message));
|
|
384
|
+
}
|
|
385
|
+
running = false;
|
|
386
|
+
if (again) {
|
|
387
|
+
again = false;
|
|
388
|
+
void run();
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
const schedule = (file) => {
|
|
392
|
+
// generated files and editor swap files are not changes worth a build
|
|
393
|
+
if (file && /(\.schema\.json|types\.d\.ts|~|\.swp)$/.test(file))
|
|
394
|
+
return;
|
|
395
|
+
if (timer)
|
|
396
|
+
clearTimeout(timer);
|
|
397
|
+
timer = setTimeout(() => void run(), 250);
|
|
398
|
+
};
|
|
399
|
+
for (const d of dirs)
|
|
400
|
+
(0, fs_1.watch)(d, { recursive: true }, (_event, file) => schedule(file ? String(file) : null));
|
|
401
|
+
console.log(yanse_1.default.dim(`watching ${dirs.map((d) => project.rel(d)).join(', ')} (Ctrl-C to stop)`));
|
|
402
|
+
await run();
|
|
403
|
+
return new Promise(() => {
|
|
404
|
+
// runs until interrupted
|
|
405
|
+
});
|
|
406
|
+
}
|
package/project.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { JsonSchema, TemplateMeta, z } from '@latex-kit/schema';
|
|
2
|
-
import { CompiledTemplate } from '@latex-kit/template';
|
|
2
|
+
import { CompiledTemplate, Component, Diagnostic, LoadedClause } from '@latex-kit/template';
|
|
3
3
|
import { ResolvedConfig } from './config';
|
|
4
|
+
import { Lock } from './lock';
|
|
4
5
|
export interface Template {
|
|
5
6
|
name: string;
|
|
6
7
|
dir: string;
|
|
@@ -15,10 +16,26 @@ export interface Template {
|
|
|
15
16
|
export declare class Project {
|
|
16
17
|
readonly root: string;
|
|
17
18
|
readonly config: ResolvedConfig;
|
|
19
|
+
/** latex-kit.lock, when the project has one. */
|
|
20
|
+
readonly lock: Lock | null;
|
|
18
21
|
private templates;
|
|
19
22
|
private snippets;
|
|
23
|
+
private clauses;
|
|
24
|
+
/** Clauses written in TypeScript, loaded when the project opens. */
|
|
25
|
+
private codeClauses;
|
|
26
|
+
/** Built-in components, then the project's own (which win). */
|
|
27
|
+
readonly components: Record<string, Component>;
|
|
28
|
+
/** Source files of the project's components, by name. */
|
|
29
|
+
readonly componentFiles: Record<string, string>;
|
|
30
|
+
/** Problems loading components/ and TypeScript clauses. */
|
|
31
|
+
readonly codeDiagnostics: Diagnostic[];
|
|
20
32
|
private constructor();
|
|
21
33
|
static open(root?: string): Promise<Project>;
|
|
34
|
+
/** The Docker image to build with: --image, then config, then latex-kit.lock. */
|
|
35
|
+
image(override?: string): string | undefined;
|
|
36
|
+
private loadCode;
|
|
37
|
+
/** Names \\component can use. */
|
|
38
|
+
componentNames(): string[];
|
|
22
39
|
path(...parts: string[]): string;
|
|
23
40
|
rel(file: string): string;
|
|
24
41
|
get templatesDir(): string;
|
|
@@ -38,6 +55,8 @@ export declare class Project {
|
|
|
38
55
|
* `.tex` is added when the name has no extension. Never outside the project.
|
|
39
56
|
*/
|
|
40
57
|
loadSnippet: (name: string, from: string) => CompiledTemplate | null;
|
|
58
|
+
/** \\clause{id}: `clauses/<id>.tex` (or `.ts`) from the clause library. */
|
|
59
|
+
loadClause: (id: string) => LoadedClause | null;
|
|
41
60
|
/** Absolute paths of every snippet loaded so far. */
|
|
42
61
|
snippetFiles(): string[];
|
|
43
62
|
}
|
package/project.js
CHANGED
|
@@ -1,25 +1,110 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Project = void 0;
|
|
4
|
+
const components_1 = require("@latex-kit/components");
|
|
4
5
|
const schema_1 = require("@latex-kit/schema");
|
|
5
6
|
const template_1 = require("@latex-kit/template");
|
|
6
7
|
const fs_1 = require("fs");
|
|
7
8
|
const path_1 = require("path");
|
|
8
9
|
const config_1 = require("./config");
|
|
10
|
+
const lock_1 = require("./lock");
|
|
11
|
+
const CODE_EXTENSIONS = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs'];
|
|
12
|
+
/** Every code file under `dir`, as [name without extension, absolute path]. */
|
|
13
|
+
function codeFiles(dir) {
|
|
14
|
+
const out = [];
|
|
15
|
+
const walk = (d, prefix) => {
|
|
16
|
+
if (!(0, fs_1.existsSync)(d))
|
|
17
|
+
return;
|
|
18
|
+
for (const name of (0, fs_1.readdirSync)(d).sort()) {
|
|
19
|
+
if (name.startsWith('.') || name.startsWith('_'))
|
|
20
|
+
continue;
|
|
21
|
+
const p = (0, path_1.join)(d, name);
|
|
22
|
+
if ((0, fs_1.statSync)(p).isDirectory())
|
|
23
|
+
walk(p, `${prefix}${name}/`);
|
|
24
|
+
else if (CODE_EXTENSIONS.includes((0, path_1.extname)(name)) && !name.endsWith('.d.ts'))
|
|
25
|
+
out.push([`${prefix}${name.slice(0, -(0, path_1.extname)(name).length)}`, p]);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
walk(dir, '');
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
9
31
|
const SCHEMA_FILES = ['schema.ts', 'schema.mts', 'schema.js', 'schema.mjs', 'schema.cjs'];
|
|
10
32
|
/** A latex-kit project: a folder with templates, data and documents. */
|
|
11
33
|
class Project {
|
|
12
34
|
root;
|
|
13
35
|
config;
|
|
36
|
+
lock;
|
|
14
37
|
templates = new Map();
|
|
15
38
|
snippets = new Map();
|
|
16
|
-
|
|
39
|
+
clauses = new Map();
|
|
40
|
+
/** Clauses written in TypeScript, loaded when the project opens. */
|
|
41
|
+
codeClauses = new Map();
|
|
42
|
+
/** Built-in components, then the project's own (which win). */
|
|
43
|
+
components = { ...components_1.BUILTIN_COMPONENTS };
|
|
44
|
+
/** Source files of the project's components, by name. */
|
|
45
|
+
componentFiles = {};
|
|
46
|
+
/** Problems loading components/ and TypeScript clauses. */
|
|
47
|
+
codeDiagnostics = [];
|
|
48
|
+
constructor(root, config,
|
|
49
|
+
/** latex-kit.lock, when the project has one. */
|
|
50
|
+
lock) {
|
|
17
51
|
this.root = root;
|
|
18
52
|
this.config = config;
|
|
53
|
+
this.lock = lock;
|
|
19
54
|
}
|
|
20
55
|
static async open(root = process.cwd()) {
|
|
21
56
|
const abs = (0, path_1.resolve)(root);
|
|
22
|
-
|
|
57
|
+
const project = new Project(abs, await (0, config_1.loadConfig)(abs), (0, lock_1.readLock)(abs));
|
|
58
|
+
await project.loadCode();
|
|
59
|
+
return project;
|
|
60
|
+
}
|
|
61
|
+
/** The Docker image to build with: --image, then config, then latex-kit.lock. */
|
|
62
|
+
image(override) {
|
|
63
|
+
return override ?? this.config.image ?? this.lock?.image;
|
|
64
|
+
}
|
|
65
|
+
async loadCode() {
|
|
66
|
+
for (const [name, file] of codeFiles(this.path(this.config.components))) {
|
|
67
|
+
const at = { file: this.rel(file), line: 1, column: 1 };
|
|
68
|
+
this.componentFiles[name] = file;
|
|
69
|
+
try {
|
|
70
|
+
const mod = await (0, schema_1.loadModule)(file);
|
|
71
|
+
const fn = mod.default;
|
|
72
|
+
if (typeof fn !== 'function')
|
|
73
|
+
throw new Error('the default export must be a component function (use defineComponent)');
|
|
74
|
+
this.components[name] = fn;
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
this.codeDiagnostics.push((0, template_1.diag)('error', `component ${name}: ${e.message}`, at));
|
|
78
|
+
const message = e.message;
|
|
79
|
+
this.components[name] = () => {
|
|
80
|
+
throw new Error(`failed to load: ${message}`);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const [id, file] of codeFiles(this.path(this.config.clauses))) {
|
|
85
|
+
const at = { file: this.rel(file), line: 1, column: 1 };
|
|
86
|
+
try {
|
|
87
|
+
const mod = await (0, schema_1.loadModule)(file);
|
|
88
|
+
const def = mod.default;
|
|
89
|
+
if (!def || typeof def !== 'object' || (typeof def.body !== 'string' && typeof def.render !== 'function')) {
|
|
90
|
+
throw new Error('the default export must be defineClause({ title, version, body | render })');
|
|
91
|
+
}
|
|
92
|
+
this.codeClauses.set(id, {
|
|
93
|
+
id,
|
|
94
|
+
title: def.title,
|
|
95
|
+
version: def.version === undefined ? undefined : String(def.version),
|
|
96
|
+
template: (0, template_1.compileTemplate)(def.body ?? '', this.rel(file)),
|
|
97
|
+
render: def.render,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
this.codeDiagnostics.push((0, template_1.diag)('error', `clause ${id}: ${e.message}`, at));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Names \\component can use. */
|
|
106
|
+
componentNames() {
|
|
107
|
+
return Object.keys(this.components).sort();
|
|
23
108
|
}
|
|
24
109
|
path(...parts) {
|
|
25
110
|
return (0, path_1.join)(this.root, ...parts);
|
|
@@ -121,6 +206,22 @@ class Project {
|
|
|
121
206
|
}
|
|
122
207
|
return null;
|
|
123
208
|
};
|
|
209
|
+
/** \\clause{id}: `clauses/<id>.tex` (or `.ts`) from the clause library. */
|
|
210
|
+
loadClause = (id) => {
|
|
211
|
+
if (this.clauses.has(id))
|
|
212
|
+
return this.clauses.get(id);
|
|
213
|
+
let loaded = null;
|
|
214
|
+
const file = (0, path_1.join)(this.path(this.config.clauses), `${id}.tex`);
|
|
215
|
+
if ((0, template_1.isRelativeName)(id) && (0, fs_1.existsSync)(file)) {
|
|
216
|
+
const text = (0, fs_1.readFileSync)(file, 'utf8');
|
|
217
|
+
loaded = { id, template: (0, template_1.compileTemplate)(text, this.rel(file)), ...(0, template_1.parseClauseHeader)(text) };
|
|
218
|
+
}
|
|
219
|
+
else if (this.codeClauses.has(id)) {
|
|
220
|
+
loaded = this.codeClauses.get(id);
|
|
221
|
+
}
|
|
222
|
+
this.clauses.set(id, loaded);
|
|
223
|
+
return loaded;
|
|
224
|
+
};
|
|
124
225
|
/** Absolute paths of every snippet loaded so far. */
|
|
125
226
|
snippetFiles() {
|
|
126
227
|
return [...this.snippets.keys()];
|
package/render.js
CHANGED
|
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.renderCommand = renderCommand;
|
|
7
7
|
const render_1 = require("@latex-kit/render");
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
8
|
const path_1 = require("path");
|
|
10
9
|
const yanse_1 = __importDefault(require("yanse"));
|
|
11
10
|
const args_1 = require("./args");
|
|
@@ -14,6 +13,7 @@ function summary(r) {
|
|
|
14
13
|
pdf: (0, path_1.relative)(process.cwd(), r.pdf),
|
|
15
14
|
sha256: r.sha256,
|
|
16
15
|
engine: r.engine,
|
|
16
|
+
engineReason: r.engineReason,
|
|
17
17
|
backend: r.backend,
|
|
18
18
|
image: r.image,
|
|
19
19
|
sourceDateEpoch: r.sourceDateEpoch,
|
|
@@ -45,8 +45,9 @@ async function renderCommand(argv, prompter) {
|
|
|
45
45
|
console.error(s);
|
|
46
46
|
};
|
|
47
47
|
try {
|
|
48
|
-
log(yanse_1.default.dim(`rendering ${opts.file}
|
|
48
|
+
log(yanse_1.default.dim(`rendering ${opts.file} (${opts.backend})…`));
|
|
49
49
|
const first = await (0, render_1.render)({ ...opts, file: opts.file });
|
|
50
|
+
log(yanse_1.default.dim(` engine ${first.engine}${first.engineReason === 'chosen' ? '' : ` (${first.engineReason})`}`));
|
|
50
51
|
for (const d of first.diagnostics)
|
|
51
52
|
log(yanse_1.default.yellow((0, render_1.formatDiagnostic)(d)));
|
|
52
53
|
if (!first.reproducible) {
|
|
@@ -54,11 +55,10 @@ async function renderCommand(argv, prompter) {
|
|
|
54
55
|
}
|
|
55
56
|
let verified;
|
|
56
57
|
if (argv.verify) {
|
|
57
|
-
|
|
58
|
+
// same output path: some tools (Ghostscript) hash the path into the PDF ID
|
|
58
59
|
log(yanse_1.default.dim('rendering again to check the output is byte-identical…'));
|
|
59
|
-
const second = await (0, render_1.render)({ ...opts, file: opts.file,
|
|
60
|
+
const second = await (0, render_1.render)({ ...opts, file: opts.file, engine: first.engine, sourceDateEpoch: first.sourceDateEpoch });
|
|
60
61
|
verified = second.sha256 === first.sha256;
|
|
61
|
-
(0, fs_1.rmSync)((0, path_1.dirname)(second.pdf), { recursive: true, force: true });
|
|
62
62
|
}
|
|
63
63
|
if (argv.json) {
|
|
64
64
|
console.log(JSON.stringify({ ...summary(first), verified }, null, 2));
|
package/scaffold.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FileMap } from './boilerplates';
|
|
2
|
+
import { Project } from './project';
|
|
3
|
+
export interface WriteResult {
|
|
4
|
+
written: string[];
|
|
5
|
+
skipped: string[];
|
|
6
|
+
}
|
|
7
|
+
/** Write files under `dir`. Existing files are kept unless `force`. */
|
|
8
|
+
export declare function writeFiles(dir: string, files: FileMap, force?: boolean): WriteResult;
|
|
9
|
+
/** Scaffold a latex-kit project (or a paper) in `dir`. */
|
|
10
|
+
export declare function initProject(dir: string, options: {
|
|
11
|
+
paper?: boolean;
|
|
12
|
+
force?: boolean;
|
|
13
|
+
version: string;
|
|
14
|
+
}): WriteResult;
|
|
15
|
+
export declare function newTemplate(project: Project, name: string, force?: boolean): WriteResult;
|
|
16
|
+
type Schema = Record<string, unknown>;
|
|
17
|
+
/** A placeholder value for a JSON Schema: required fields only, typed. */
|
|
18
|
+
export declare function skeleton(schema: Schema | undefined, root: Schema, depth?: number): unknown;
|
|
19
|
+
/**
|
|
20
|
+
* documents/<name>.json for a template: every required field with a
|
|
21
|
+
* placeholder, and `$schema` pointing at the generated JSON Schema so editors
|
|
22
|
+
* autocomplete and validate it.
|
|
23
|
+
*/
|
|
24
|
+
export declare function newDocument(project: Project, name: string, templateName: string, force?: boolean): Promise<WriteResult & {
|
|
25
|
+
schemaFile: string | null;
|
|
26
|
+
}>;
|
|
27
|
+
export {};
|