latex-kit 0.0.0-stage → 0.3.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/LICENSE +23 -0
- package/README.md +67 -2
- package/args.d.ts +9 -0
- package/args.js +37 -0
- package/build.d.ts +39 -0
- package/build.js +222 -0
- package/check.d.ts +17 -0
- package/check.js +58 -0
- package/cli.d.ts +2 -0
- package/cli.js +20 -0
- package/commands.d.ts +5 -0
- package/commands.js +69 -0
- package/config.d.ts +26 -0
- package/config.js +30 -0
- package/doctor.d.ts +2 -0
- package/doctor.js +23 -0
- package/esm/args.d.ts +9 -0
- package/esm/args.js +32 -0
- package/esm/build.d.ts +39 -0
- package/esm/build.js +216 -0
- package/esm/check.d.ts +17 -0
- package/esm/check.js +54 -0
- package/esm/cli.d.ts +2 -0
- package/esm/cli.js +18 -0
- package/esm/commands.d.ts +5 -0
- package/esm/commands.js +61 -0
- package/esm/config.d.ts +26 -0
- package/esm/config.js +25 -0
- package/esm/doctor.d.ts +2 -0
- package/esm/doctor.js +17 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +12 -0
- package/esm/project-commands.d.ts +6 -0
- package/esm/project-commands.js +163 -0
- package/esm/project.d.ts +43 -0
- package/esm/project.js +125 -0
- package/esm/render.d.ts +3 -0
- package/esm/render.js +82 -0
- package/esm/usage.d.ts +1 -0
- package/esm/usage.js +73 -0
- package/index.d.ts +12 -0
- package/index.js +28 -0
- package/package.json +53 -4
- package/project-commands.d.ts +6 -0
- package/project-commands.js +173 -0
- package/project.d.ts +43 -0
- package/project.js +129 -0
- package/render.d.ts +3 -0
- package/render.js +88 -0
- package/usage.d.ts +1 -0
- package/usage.js +76 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildCommand = buildCommand;
|
|
7
|
+
exports.checkCommand = checkCommand;
|
|
8
|
+
exports.fieldsCommand = fieldsCommand;
|
|
9
|
+
exports.typegenCommand = typegenCommand;
|
|
10
|
+
exports.styCommand = styCommand;
|
|
11
|
+
const render_1 = require("@latex-kit/render");
|
|
12
|
+
const schema_1 = require("@latex-kit/schema");
|
|
13
|
+
const template_1 = require("@latex-kit/template");
|
|
14
|
+
const fs_1 = require("fs");
|
|
15
|
+
const path_1 = require("path");
|
|
16
|
+
const yanse_1 = __importDefault(require("yanse"));
|
|
17
|
+
const args_1 = require("./args");
|
|
18
|
+
const build_1 = require("./build");
|
|
19
|
+
const check_1 = require("./check");
|
|
20
|
+
const project_1 = require("./project");
|
|
21
|
+
function printDiagnostics(ds, json) {
|
|
22
|
+
if (json)
|
|
23
|
+
return;
|
|
24
|
+
for (const d of ds)
|
|
25
|
+
console.error((d.severity === 'error' ? yanse_1.default.red : yanse_1.default.yellow)((0, template_1.formatDiagnostic)(d)));
|
|
26
|
+
}
|
|
27
|
+
const rel = (p) => (p ? (0, path_1.relative)(process.cwd(), p) : null);
|
|
28
|
+
async function buildCommand(argv) {
|
|
29
|
+
const variant = (argv.variant ?? (argv.final ? 'final' : 'draft'));
|
|
30
|
+
if (!template_1.VARIANTS.includes(variant))
|
|
31
|
+
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(', ')}`);
|
|
34
|
+
const project = await project_1.Project.open(argv.root);
|
|
35
|
+
const options = {
|
|
36
|
+
variant,
|
|
37
|
+
strict: argv.strict === undefined ? undefined : Boolean(argv.strict),
|
|
38
|
+
engine: argv.engine,
|
|
39
|
+
backend: argv.local ? 'local' : undefined,
|
|
40
|
+
image: argv.image,
|
|
41
|
+
platform: argv.platform,
|
|
42
|
+
verify: Boolean(argv.verify),
|
|
43
|
+
texOnly: Boolean(argv['tex-only']),
|
|
44
|
+
};
|
|
45
|
+
const json = Boolean(argv.json);
|
|
46
|
+
const log = (s) => !json && console.error(s);
|
|
47
|
+
const targets = [];
|
|
48
|
+
const files = (argv._ ?? []).map(String);
|
|
49
|
+
if (variant === 'blank' && files.length === 0) {
|
|
50
|
+
const names = argv.template ? [String(argv.template)] : project.templateNames();
|
|
51
|
+
for (const n of names)
|
|
52
|
+
targets.push({ label: `template ${n}`, run: () => (0, build_1.buildBlank)(project, n, options) });
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
let docs = files.length ? files.map((f) => (0, path_1.join)(process.cwd(), f)) : project.documentFiles();
|
|
56
|
+
for (const f of docs)
|
|
57
|
+
if (!(0, fs_1.existsSync)(f))
|
|
58
|
+
throw new args_1.UsageError(`no such document: ${rel(f)}`);
|
|
59
|
+
if (argv.template) {
|
|
60
|
+
const wanted = String(argv.template);
|
|
61
|
+
const keep = [];
|
|
62
|
+
for (const f of docs)
|
|
63
|
+
if ((await (0, schema_1.loadDocument)(f, { dataDir: project.dataDir })).record.template === wanted)
|
|
64
|
+
keep.push(f);
|
|
65
|
+
docs = keep;
|
|
66
|
+
}
|
|
67
|
+
for (const f of docs)
|
|
68
|
+
targets.push({ label: rel(f), run: () => (0, build_1.buildDocument)(project, f, options) });
|
|
69
|
+
}
|
|
70
|
+
if (targets.length === 0) {
|
|
71
|
+
log(yanse_1.default.yellow(`nothing to build (no documents in ${project.rel(project.documentsDir)}/)`));
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
const results = [];
|
|
75
|
+
let failed = 0;
|
|
76
|
+
for (const t of targets) {
|
|
77
|
+
log(yanse_1.default.dim(`building ${t.label} (${variant})…`));
|
|
78
|
+
try {
|
|
79
|
+
const r = await t.run();
|
|
80
|
+
printDiagnostics(r.diagnostics, json);
|
|
81
|
+
const bad = r.verified === false;
|
|
82
|
+
if (bad)
|
|
83
|
+
failed++;
|
|
84
|
+
results.push({ ...r, tex: rel(r.tex), pdf: rel(r.pdf), diagnostics: r.diagnostics.length });
|
|
85
|
+
if (!json) {
|
|
86
|
+
console.log(`${bad ? yanse_1.default.red('✗') : yanse_1.default.green('✓')} ${rel(r.pdf ?? r.tex)}${r.docId ? yanse_1.default.dim(` ${r.docId}`) : ''}`);
|
|
87
|
+
if (r.minted)
|
|
88
|
+
console.log(yanse_1.default.dim(` minted ${r.docId} and saved it in ${t.label}`));
|
|
89
|
+
if (r.verified !== undefined)
|
|
90
|
+
console.log(r.verified ? yanse_1.default.dim(' byte-identical on a second build') : yanse_1.default.red(' second build differs'));
|
|
91
|
+
if (!r.reproducible)
|
|
92
|
+
console.log(yanse_1.default.yellow(' not reproducible: some inputs are not committed, so the build date is "now"'));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
failed++;
|
|
97
|
+
if (e instanceof build_1.BuildError) {
|
|
98
|
+
printDiagnostics(e.diagnostics, json);
|
|
99
|
+
if (!json)
|
|
100
|
+
console.error(yanse_1.default.red(`✗ ${t.label}: ${e.message}`));
|
|
101
|
+
results.push({ target: t.label, error: e.message, diagnostics: e.diagnostics });
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
if (!json)
|
|
105
|
+
console.error(yanse_1.default.red(`✗ ${t.label}: ${e.message}`));
|
|
106
|
+
results.push({ target: t.label, error: e.message });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (json)
|
|
111
|
+
console.log(JSON.stringify(results, null, 2));
|
|
112
|
+
return failed ? 1 : 0;
|
|
113
|
+
}
|
|
114
|
+
async function checkCommand(argv) {
|
|
115
|
+
const project = await project_1.Project.open(argv.root);
|
|
116
|
+
const r = await (0, check_1.checkProject)(project);
|
|
117
|
+
const errors = r.diagnostics.filter((d) => d.severity === 'error').length;
|
|
118
|
+
const warnings = r.diagnostics.length - errors;
|
|
119
|
+
if (argv.json) {
|
|
120
|
+
console.log(JSON.stringify(r, null, 2));
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
printDiagnostics(r.diagnostics, false);
|
|
124
|
+
const summary = `${r.templates.length} template${r.templates.length === 1 ? '' : 's'}, ${r.documents.length} document${r.documents.length === 1 ? '' : 's'}: ${errors} error${errors === 1 ? '' : 's'}, ${warnings} warning${warnings === 1 ? '' : 's'}`;
|
|
125
|
+
console.log(errors ? yanse_1.default.red(`✗ ${summary}`) : `${yanse_1.default.green('✓')} ${summary}`);
|
|
126
|
+
}
|
|
127
|
+
return errors ? 1 : 0;
|
|
128
|
+
}
|
|
129
|
+
async function fieldsCommand(argv) {
|
|
130
|
+
const name = argv._?.[0];
|
|
131
|
+
if (!name)
|
|
132
|
+
throw new args_1.UsageError('usage: latex-kit fields <template>');
|
|
133
|
+
const project = await project_1.Project.open(argv.root);
|
|
134
|
+
const { fields, diagnostics } = await (0, check_1.templateFields)(project, String(name));
|
|
135
|
+
if (argv.json) {
|
|
136
|
+
console.log(JSON.stringify(fields, null, 2));
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
printDiagnostics(diagnostics, false);
|
|
140
|
+
const width = Math.max(...fields.map((f) => f.absolute.length), 4);
|
|
141
|
+
for (const f of fields) {
|
|
142
|
+
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(5))} ${yanse_1.default.dim(`${f.file}:${f.line}`)}${filters}`);
|
|
144
|
+
}
|
|
145
|
+
return diagnostics.some((d) => d.severity === 'error') ? 1 : 0;
|
|
146
|
+
}
|
|
147
|
+
async function typegenCommand(argv) {
|
|
148
|
+
const project = await project_1.Project.open(argv.root);
|
|
149
|
+
let n = 0;
|
|
150
|
+
for (const name of project.templateNames()) {
|
|
151
|
+
const t = await project.template(name);
|
|
152
|
+
if (!t.jsonSchema)
|
|
153
|
+
continue;
|
|
154
|
+
const schemaFile = (0, path_1.join)(t.dir, `${name}.schema.json`);
|
|
155
|
+
const typesFile = (0, path_1.join)(t.dir, 'types.d.ts');
|
|
156
|
+
(0, fs_1.writeFileSync)(schemaFile, JSON.stringify((0, schema_1.documentJsonSchema)(name, t.jsonSchema), null, 2) + '\n');
|
|
157
|
+
(0, fs_1.writeFileSync)(typesFile, `// Generated by latex-kit typegen from schema.ts. Do not edit.\n${(0, schema_1.generateTypes)(name, t.jsonSchema)}\n`);
|
|
158
|
+
console.log(`${yanse_1.default.green('✓')} ${rel(schemaFile)} ${rel(typesFile)}`);
|
|
159
|
+
n++;
|
|
160
|
+
}
|
|
161
|
+
if (n === 0)
|
|
162
|
+
console.log(yanse_1.default.yellow('no templates with a schema.ts'));
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
async function styCommand(argv) {
|
|
166
|
+
const project = await project_1.Project.open(argv.root);
|
|
167
|
+
const dir = argv._?.[0] ? (0, path_1.join)(process.cwd(), String(argv._[0])) : project.templatesDir;
|
|
168
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
169
|
+
const file = (0, path_1.join)(dir, 'latex-kit.sty');
|
|
170
|
+
(0, fs_1.writeFileSync)(file, template_1.LATEX_KIT_STY);
|
|
171
|
+
console.log(`${yanse_1.default.green('✓')} ${rel(file)} (lets editors compile templates as blank previews)`);
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
package/project.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { JsonSchema, TemplateMeta, z } from '@latex-kit/schema';
|
|
2
|
+
import { CompiledTemplate } from '@latex-kit/template';
|
|
3
|
+
import { ResolvedConfig } from './config';
|
|
4
|
+
export interface Template {
|
|
5
|
+
name: string;
|
|
6
|
+
dir: string;
|
|
7
|
+
texFile: string;
|
|
8
|
+
compiled: CompiledTemplate;
|
|
9
|
+
schemaFile: string | null;
|
|
10
|
+
schema: z.ZodType | null;
|
|
11
|
+
jsonSchema: JsonSchema | null;
|
|
12
|
+
meta: TemplateMeta;
|
|
13
|
+
}
|
|
14
|
+
/** A latex-kit project: a folder with templates, data and documents. */
|
|
15
|
+
export declare class Project {
|
|
16
|
+
readonly root: string;
|
|
17
|
+
readonly config: ResolvedConfig;
|
|
18
|
+
private templates;
|
|
19
|
+
private snippets;
|
|
20
|
+
private constructor();
|
|
21
|
+
static open(root?: string): Promise<Project>;
|
|
22
|
+
path(...parts: string[]): string;
|
|
23
|
+
rel(file: string): string;
|
|
24
|
+
get templatesDir(): string;
|
|
25
|
+
get dataDir(): string;
|
|
26
|
+
get documentsDir(): string;
|
|
27
|
+
get outDir(): string;
|
|
28
|
+
/** Template names: folders under templates/ that contain template.tex. */
|
|
29
|
+
templateNames(): string[];
|
|
30
|
+
/** Document record files under documents/ (any depth). */
|
|
31
|
+
documentFiles(): string[];
|
|
32
|
+
/** A document's name: its path under documents/ without extension, `/` → `-`. */
|
|
33
|
+
documentName(file: string): string;
|
|
34
|
+
template(name: string): Promise<Template>;
|
|
35
|
+
private loadTemplate;
|
|
36
|
+
/**
|
|
37
|
+
* \snippet{name}: first next to the file that uses it, then in snippets/.
|
|
38
|
+
* `.tex` is added when the name has no extension. Never outside the project.
|
|
39
|
+
*/
|
|
40
|
+
loadSnippet: (name: string, from: string) => CompiledTemplate | null;
|
|
41
|
+
/** Absolute paths of every snippet loaded so far. */
|
|
42
|
+
snippetFiles(): string[];
|
|
43
|
+
}
|
package/project.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Project = void 0;
|
|
4
|
+
const schema_1 = require("@latex-kit/schema");
|
|
5
|
+
const template_1 = require("@latex-kit/template");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
const SCHEMA_FILES = ['schema.ts', 'schema.mts', 'schema.js', 'schema.mjs', 'schema.cjs'];
|
|
10
|
+
/** A latex-kit project: a folder with templates, data and documents. */
|
|
11
|
+
class Project {
|
|
12
|
+
root;
|
|
13
|
+
config;
|
|
14
|
+
templates = new Map();
|
|
15
|
+
snippets = new Map();
|
|
16
|
+
constructor(root, config) {
|
|
17
|
+
this.root = root;
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
static async open(root = process.cwd()) {
|
|
21
|
+
const abs = (0, path_1.resolve)(root);
|
|
22
|
+
return new Project(abs, await (0, config_1.loadConfig)(abs));
|
|
23
|
+
}
|
|
24
|
+
path(...parts) {
|
|
25
|
+
return (0, path_1.join)(this.root, ...parts);
|
|
26
|
+
}
|
|
27
|
+
rel(file) {
|
|
28
|
+
return (0, path_1.relative)(process.cwd(), file) || '.';
|
|
29
|
+
}
|
|
30
|
+
get templatesDir() {
|
|
31
|
+
return this.path(this.config.templates);
|
|
32
|
+
}
|
|
33
|
+
get dataDir() {
|
|
34
|
+
return this.path(this.config.data);
|
|
35
|
+
}
|
|
36
|
+
get documentsDir() {
|
|
37
|
+
return this.path(this.config.documents);
|
|
38
|
+
}
|
|
39
|
+
get outDir() {
|
|
40
|
+
return this.path(this.config.out);
|
|
41
|
+
}
|
|
42
|
+
/** Template names: folders under templates/ that contain template.tex. */
|
|
43
|
+
templateNames() {
|
|
44
|
+
if (!(0, fs_1.existsSync)(this.templatesDir))
|
|
45
|
+
return [];
|
|
46
|
+
return (0, fs_1.readdirSync)(this.templatesDir)
|
|
47
|
+
.filter((n) => !n.startsWith('.') && !n.startsWith('_'))
|
|
48
|
+
.filter((n) => (0, fs_1.existsSync)((0, path_1.join)(this.templatesDir, n, 'template.tex')))
|
|
49
|
+
.sort();
|
|
50
|
+
}
|
|
51
|
+
/** Document record files under documents/ (any depth). */
|
|
52
|
+
documentFiles() {
|
|
53
|
+
const out = [];
|
|
54
|
+
const walk = (dir) => {
|
|
55
|
+
if (!(0, fs_1.existsSync)(dir))
|
|
56
|
+
return;
|
|
57
|
+
for (const name of (0, fs_1.readdirSync)(dir).sort()) {
|
|
58
|
+
if (name.startsWith('.') || name.startsWith('_'))
|
|
59
|
+
continue;
|
|
60
|
+
const p = (0, path_1.join)(dir, name);
|
|
61
|
+
if ((0, fs_1.statSync)(p).isDirectory())
|
|
62
|
+
walk(p);
|
|
63
|
+
else if (schema_1.DATA_EXTENSIONS.includes((0, path_1.extname)(name).toLowerCase()) && !name.endsWith('.d.ts'))
|
|
64
|
+
out.push(p);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
walk(this.documentsDir);
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/** A document's name: its path under documents/ without extension, `/` → `-`. */
|
|
71
|
+
documentName(file) {
|
|
72
|
+
const rel = (0, path_1.relative)(this.documentsDir, file);
|
|
73
|
+
const base = rel.startsWith('..') || (0, path_1.isAbsolute)(rel) ? (0, path_1.basename)(file) : rel;
|
|
74
|
+
return base.slice(0, -(0, path_1.extname)(base).length).split(/[\\/]/).join('-');
|
|
75
|
+
}
|
|
76
|
+
template(name) {
|
|
77
|
+
let t = this.templates.get(name);
|
|
78
|
+
if (!t) {
|
|
79
|
+
t = this.loadTemplate(name);
|
|
80
|
+
this.templates.set(name, t);
|
|
81
|
+
}
|
|
82
|
+
return t;
|
|
83
|
+
}
|
|
84
|
+
async loadTemplate(name) {
|
|
85
|
+
if (!/^[A-Za-z0-9_-]+$/.test(name))
|
|
86
|
+
throw new Error(`invalid template name "${name}"`);
|
|
87
|
+
const dir = (0, path_1.join)(this.templatesDir, name);
|
|
88
|
+
const texFile = (0, path_1.join)(dir, 'template.tex');
|
|
89
|
+
if (!(0, fs_1.existsSync)(texFile)) {
|
|
90
|
+
const known = this.templateNames();
|
|
91
|
+
throw new Error(`no template "${name}" (expected ${this.rel(texFile)}${known.length ? `; templates: ${known.join(', ')}` : ''})`);
|
|
92
|
+
}
|
|
93
|
+
const compiled = (0, template_1.compileTemplate)((0, fs_1.readFileSync)(texFile, 'utf8'), this.rel(texFile));
|
|
94
|
+
const schemaFile = SCHEMA_FILES.map((f) => (0, path_1.join)(dir, f)).find((f) => (0, fs_1.existsSync)(f)) ?? null;
|
|
95
|
+
let schema = null;
|
|
96
|
+
let meta = {};
|
|
97
|
+
let jsonSchema = null;
|
|
98
|
+
if (schemaFile) {
|
|
99
|
+
({ schema, meta } = await (0, schema_1.loadTemplateSchema)(schemaFile));
|
|
100
|
+
jsonSchema = (0, schema_1.toJsonSchema)(schema);
|
|
101
|
+
}
|
|
102
|
+
return { name, dir, texFile, compiled, schemaFile, schema, jsonSchema, meta };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* \snippet{name}: first next to the file that uses it, then in snippets/.
|
|
106
|
+
* `.tex` is added when the name has no extension. Never outside the project.
|
|
107
|
+
*/
|
|
108
|
+
loadSnippet = (name, from) => {
|
|
109
|
+
const file = name.endsWith('.tex') ? name : `${name}.tex`;
|
|
110
|
+
const candidates = [(0, path_1.resolve)((0, path_1.dirname)((0, path_1.resolve)(from)), file), (0, path_1.join)(this.path(this.config.snippets), file)];
|
|
111
|
+
for (const c of candidates) {
|
|
112
|
+
const rel = (0, path_1.relative)(this.root, c);
|
|
113
|
+
if (rel.startsWith('..') || (0, path_1.isAbsolute)(rel) || !(0, fs_1.existsSync)(c))
|
|
114
|
+
continue;
|
|
115
|
+
let compiled = this.snippets.get(c);
|
|
116
|
+
if (!compiled) {
|
|
117
|
+
compiled = (0, template_1.compileTemplate)((0, fs_1.readFileSync)(c, 'utf8'), this.rel(c));
|
|
118
|
+
this.snippets.set(c, compiled);
|
|
119
|
+
}
|
|
120
|
+
return compiled;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
124
|
+
/** Absolute paths of every snippet loaded so far. */
|
|
125
|
+
snippetFiles() {
|
|
126
|
+
return [...this.snippets.keys()];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.Project = Project;
|
package/render.d.ts
ADDED
package/render.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.renderCommand = renderCommand;
|
|
7
|
+
const render_1 = require("@latex-kit/render");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const path_1 = require("path");
|
|
10
|
+
const yanse_1 = __importDefault(require("yanse"));
|
|
11
|
+
const args_1 = require("./args");
|
|
12
|
+
function summary(r) {
|
|
13
|
+
return {
|
|
14
|
+
pdf: (0, path_1.relative)(process.cwd(), r.pdf),
|
|
15
|
+
sha256: r.sha256,
|
|
16
|
+
engine: r.engine,
|
|
17
|
+
backend: r.backend,
|
|
18
|
+
image: r.image,
|
|
19
|
+
sourceDateEpoch: r.sourceDateEpoch,
|
|
20
|
+
epochSource: r.epochSource,
|
|
21
|
+
reproducible: r.reproducible,
|
|
22
|
+
warnings: r.diagnostics.filter((d) => d.severity === 'warning').length,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
async function renderCommand(argv, prompter) {
|
|
26
|
+
let opts;
|
|
27
|
+
try {
|
|
28
|
+
opts = (0, args_1.renderOptionsFromArgv)(argv);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
if (e instanceof args_1.UsageError) {
|
|
32
|
+
console.error(yanse_1.default.red(e.message));
|
|
33
|
+
return 2;
|
|
34
|
+
}
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
if (!opts.file) {
|
|
38
|
+
const answers = await prompter.prompt({}, [
|
|
39
|
+
{ type: 'text', name: 'file', message: 'Which .tex file?', required: true },
|
|
40
|
+
]);
|
|
41
|
+
opts.file = answers.file;
|
|
42
|
+
}
|
|
43
|
+
const log = (s) => {
|
|
44
|
+
if (!argv.json)
|
|
45
|
+
console.error(s);
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
log(yanse_1.default.dim(`rendering ${opts.file} with ${opts.engine} (${opts.backend})…`));
|
|
49
|
+
const first = await (0, render_1.render)({ ...opts, file: opts.file });
|
|
50
|
+
for (const d of first.diagnostics)
|
|
51
|
+
log(yanse_1.default.yellow((0, render_1.formatDiagnostic)(d)));
|
|
52
|
+
if (!first.reproducible) {
|
|
53
|
+
log(yanse_1.default.yellow('warning: no SOURCE_DATE_EPOCH and the file is not committed; the build date is "now", so builds will differ'));
|
|
54
|
+
}
|
|
55
|
+
let verified;
|
|
56
|
+
if (argv.verify) {
|
|
57
|
+
const verifyDir = `${opts.outDir}.verify`;
|
|
58
|
+
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, outDir: verifyDir, sourceDateEpoch: first.sourceDateEpoch });
|
|
60
|
+
verified = second.sha256 === first.sha256;
|
|
61
|
+
(0, fs_1.rmSync)((0, path_1.dirname)(second.pdf), { recursive: true, force: true });
|
|
62
|
+
}
|
|
63
|
+
if (argv.json) {
|
|
64
|
+
console.log(JSON.stringify({ ...summary(first), verified }, null, 2));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
console.log(`${yanse_1.default.green('✓')} ${(0, path_1.relative)(process.cwd(), first.pdf)}`);
|
|
68
|
+
console.log(yanse_1.default.dim(` sha256 ${first.sha256}`));
|
|
69
|
+
console.log(yanse_1.default.dim(` date ${new Date(first.sourceDateEpoch * 1000).toISOString()} (from ${first.epochSource})`));
|
|
70
|
+
if (verified !== undefined) {
|
|
71
|
+
console.log(verified ? `${yanse_1.default.green('✓')} byte-identical on a second build` : yanse_1.default.red('✗ second build differs'));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return verified === false ? 1 : 0;
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
if (e instanceof render_1.RenderError) {
|
|
78
|
+
console.error(yanse_1.default.red(e.message));
|
|
79
|
+
for (const d of e.diagnostics)
|
|
80
|
+
console.error((0, render_1.formatDiagnostic)(d));
|
|
81
|
+
if (e.diagnostics.length === 0 && e.output) {
|
|
82
|
+
console.error(yanse_1.default.dim(e.output.split('\n').slice(-25).join('\n')));
|
|
83
|
+
}
|
|
84
|
+
return 1;
|
|
85
|
+
}
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
88
|
+
}
|
package/usage.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function help(command?: string): void;
|
package/usage.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.help = help;
|
|
4
|
+
const MAIN = `latex-kit — build typed, reproducible LaTeX documents
|
|
5
|
+
|
|
6
|
+
Usage
|
|
7
|
+
latex-kit <command> [options]
|
|
8
|
+
|
|
9
|
+
Project commands (run in a folder with templates/, data/, documents/)
|
|
10
|
+
build [documents…] Fill templates with records and build PDFs
|
|
11
|
+
check Check templates, schemas, records and IDs without TeX
|
|
12
|
+
fields <template> List every field a template reads
|
|
13
|
+
typegen Write JSON Schema and .d.ts for each template's data
|
|
14
|
+
sty [dir] Write latex-kit.sty so editors can preview templates
|
|
15
|
+
|
|
16
|
+
Other commands
|
|
17
|
+
render <file.tex> Build a plain .tex file with latexmk in a pinned image
|
|
18
|
+
doctor Check Docker, the image and local TeX
|
|
19
|
+
|
|
20
|
+
Options
|
|
21
|
+
-h, --help Show help (latex-kit help <command> for details)
|
|
22
|
+
-v, --version Show the version
|
|
23
|
+
`;
|
|
24
|
+
const RENDER = `latex-kit render <file.tex> [options]
|
|
25
|
+
|
|
26
|
+
Builds <file.tex> with latexmk. The build date is taken from
|
|
27
|
+
SOURCE_DATE_EPOCH, else the file's last git commit, so the same commit
|
|
28
|
+
always produces the same PDF bytes.
|
|
29
|
+
|
|
30
|
+
Options
|
|
31
|
+
-o, --out <dir> Output directory, relative to --root (default: out)
|
|
32
|
+
-e, --engine <name> lualatex (default), xelatex or pdflatex
|
|
33
|
+
--root <dir> Directory TeX can see (default: current directory)
|
|
34
|
+
--image <ref> Docker image (default: pinned pyramation/latex digest)
|
|
35
|
+
--platform <os/arch> Docker --platform (default: linux/amd64 for the default image)
|
|
36
|
+
--local Use latexmk on PATH instead of Docker (not pinned)
|
|
37
|
+
--source-date-epoch <s> Build timestamp in seconds since 1970
|
|
38
|
+
--verify Build twice and fail if the PDFs differ
|
|
39
|
+
--json Print a JSON summary on stdout
|
|
40
|
+
|
|
41
|
+
Only lualatex is byte-reproducible in the default image.
|
|
42
|
+
`;
|
|
43
|
+
const BUILD = `latex-kit build [documents…] [options]
|
|
44
|
+
|
|
45
|
+
Fills each document's template with its data and builds a PDF in
|
|
46
|
+
out/<document>/. Without arguments, builds every record in documents/.
|
|
47
|
+
|
|
48
|
+
Variants
|
|
49
|
+
--variant draft (default) DRAFT watermark and stamp; missing values show
|
|
50
|
+
as red markers (they fail instead when CI=true or --strict)
|
|
51
|
+
--variant final No marks. Missing values fail. Mints a document ID on the
|
|
52
|
+
--final first build and saves it in the record (JSON/YAML)
|
|
53
|
+
--variant signed Like final; templates can test \\when{$variant == "signed"}
|
|
54
|
+
--variant blank Every field is a labelled line. With no documents, builds
|
|
55
|
+
each template (or --template <name>) as an empty form
|
|
56
|
+
|
|
57
|
+
Options
|
|
58
|
+
--template <name> Only documents that use this template
|
|
59
|
+
--strict Fail on missing values in drafts too
|
|
60
|
+
-e, --engine <name> lualatex (default), xelatex or pdflatex
|
|
61
|
+
--local Use latexmk on PATH instead of Docker
|
|
62
|
+
--image <ref> Docker image (default: pinned pyramation/latex)
|
|
63
|
+
--verify Build twice and fail unless the PDFs are identical
|
|
64
|
+
--tex-only Write the .tex without running TeX
|
|
65
|
+
--json JSON results on stdout
|
|
66
|
+
--root <dir> Project folder (default: current directory)
|
|
67
|
+
`;
|
|
68
|
+
const DOCTOR = `latex-kit doctor [--image <ref>] [--json]
|
|
69
|
+
|
|
70
|
+
Reports the Node version, whether Docker is running, whether the image is
|
|
71
|
+
pulled and whether it runs natively on this machine, and whether latexmk
|
|
72
|
+
is available for --local builds. Exits 1 if rendering cannot work.
|
|
73
|
+
`;
|
|
74
|
+
function help(command) {
|
|
75
|
+
console.log(command === 'render' ? RENDER : command === 'doctor' ? DOCTOR : command === 'build' ? BUILD : MAIN);
|
|
76
|
+
}
|