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
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2026 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# latex-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/constructive-io/latex-kit/actions/workflows/ci.yml">
|
|
9
|
+
<img height="20" src="https://github.com/constructive-io/latex-kit/actions/workflows/ci.yml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/constructive-io/latex-kit/blob/main/LICENSE">
|
|
12
|
+
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/latex-kit">
|
|
15
|
+
<img height="20" src="https://img.shields.io/npm/v/latex-kit.svg"/>
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center">Build typed, reproducible LaTeX documents from the command line.</p>
|
|
20
|
+
|
|
21
|
+
The command-line front end for the `@latex-kit/*` packages. The binary is
|
|
22
|
+
`latex-kit`, not `latex`, so it never shadows TeX's own `latex`.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install -g latex-kit
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
latex-kit doctor # is Docker ready?
|
|
34
|
+
latex-kit render examples/letter/letter.tex # → out/letter.pdf
|
|
35
|
+
latex-kit render paper.tex --engine xelatex -o build
|
|
36
|
+
latex-kit render letter.tex --verify # build twice, fail if the bytes differ
|
|
37
|
+
latex-kit render letter.tex --json # machine-readable summary
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### `render <file.tex>`
|
|
41
|
+
|
|
42
|
+
| Flag | Default | |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `-o, --out <dir>` | `out` | Output directory, relative to `--root` |
|
|
45
|
+
| `-e, --engine <name>` | `lualatex` | `lualatex`, `xelatex`, `pdflatex` |
|
|
46
|
+
| `--root <dir>` | current directory | Directory TeX can see |
|
|
47
|
+
| `--image <ref>` | pinned `pyramation/latex` | Docker image |
|
|
48
|
+
| `--platform <os/arch>` | `linux/amd64` for the default image | Docker `--platform` |
|
|
49
|
+
| `--local` | | Use `latexmk` on `PATH` instead of Docker (not pinned) |
|
|
50
|
+
| `--source-date-epoch <s>` | `SOURCE_DATE_EPOCH`, else the last git commit | Build timestamp |
|
|
51
|
+
| `--verify` | | Build twice; exit 1 if the PDFs differ |
|
|
52
|
+
| `--json` | | Print a JSON summary |
|
|
53
|
+
|
|
54
|
+
### `doctor`
|
|
55
|
+
|
|
56
|
+
Checks Node, the Docker daemon, whether the image is pulled and runs
|
|
57
|
+
natively on this machine, and whether a local `latexmk` exists. Exits 1 if
|
|
58
|
+
rendering cannot work.
|
|
59
|
+
|
|
60
|
+
## Credits
|
|
61
|
+
|
|
62
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
63
|
+
|
|
64
|
+
## Disclaimer
|
|
65
|
+
|
|
66
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
67
|
+
|
|
68
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/args.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RenderOptions } from '@latex-kit/render';
|
|
2
|
+
import type { ParsedArgs } from 'minimist';
|
|
3
|
+
export declare class UsageError extends Error {
|
|
4
|
+
name: string;
|
|
5
|
+
}
|
|
6
|
+
/** Turn `latex-kit render` flags into render options. Pure, for testing. */
|
|
7
|
+
export declare function renderOptionsFromArgv(argv: Partial<ParsedArgs>): Omit<RenderOptions, 'file'> & {
|
|
8
|
+
file?: string;
|
|
9
|
+
};
|
package/args.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UsageError = void 0;
|
|
4
|
+
exports.renderOptionsFromArgv = renderOptionsFromArgv;
|
|
5
|
+
const render_1 = require("@latex-kit/render");
|
|
6
|
+
class UsageError extends Error {
|
|
7
|
+
name = 'UsageError';
|
|
8
|
+
}
|
|
9
|
+
exports.UsageError = UsageError;
|
|
10
|
+
/** Turn `latex-kit render` flags into render options. Pure, for testing. */
|
|
11
|
+
function renderOptionsFromArgv(argv) {
|
|
12
|
+
const engine = (argv.engine ?? 'lualatex');
|
|
13
|
+
if (!render_1.ENGINES.includes(engine)) {
|
|
14
|
+
throw new UsageError(`--engine must be one of ${render_1.ENGINES.join(', ')}`);
|
|
15
|
+
}
|
|
16
|
+
let sourceDateEpoch;
|
|
17
|
+
if (argv['source-date-epoch'] !== undefined) {
|
|
18
|
+
const raw = String(argv['source-date-epoch']);
|
|
19
|
+
if (!/^\d+$/.test(raw))
|
|
20
|
+
throw new UsageError('--source-date-epoch must be a whole number of seconds');
|
|
21
|
+
sourceDateEpoch = Number(raw);
|
|
22
|
+
}
|
|
23
|
+
const backend = argv.local ? 'local' : 'docker';
|
|
24
|
+
if (backend === 'local' && (argv.image || argv.platform)) {
|
|
25
|
+
throw new UsageError('--image and --platform only apply to the docker backend');
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
file: argv._?.[0] !== undefined ? String(argv._[0]) : argv.file,
|
|
29
|
+
root: argv.root,
|
|
30
|
+
outDir: argv.out ?? 'out',
|
|
31
|
+
engine,
|
|
32
|
+
backend,
|
|
33
|
+
image: argv.image,
|
|
34
|
+
platform: argv.platform,
|
|
35
|
+
sourceDateEpoch,
|
|
36
|
+
};
|
|
37
|
+
}
|
package/build.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Engine } from '@latex-kit/render';
|
|
2
|
+
import { Diagnostic, Variant } from '@latex-kit/template';
|
|
3
|
+
import { Project } from './project';
|
|
4
|
+
export interface BuildOptions {
|
|
5
|
+
variant?: Variant;
|
|
6
|
+
/** Fail on missing values even in drafts. Default: on when CI=true. */
|
|
7
|
+
strict?: boolean;
|
|
8
|
+
engine?: Engine;
|
|
9
|
+
backend?: 'docker' | 'local';
|
|
10
|
+
image?: string;
|
|
11
|
+
platform?: string | null;
|
|
12
|
+
/** Build twice and fail unless the PDFs are byte-identical. */
|
|
13
|
+
verify?: boolean;
|
|
14
|
+
/** Stop after writing the .tex (no TeX run). */
|
|
15
|
+
texOnly?: boolean;
|
|
16
|
+
onOutput?: (chunk: string) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface BuildResult {
|
|
19
|
+
name: string;
|
|
20
|
+
variant: Variant;
|
|
21
|
+
tex: string;
|
|
22
|
+
pdf: string | null;
|
|
23
|
+
sha256: string | null;
|
|
24
|
+
docId: string | null;
|
|
25
|
+
/** The ID was minted by this build and written to the record. */
|
|
26
|
+
minted: boolean;
|
|
27
|
+
sourceDateEpoch: number;
|
|
28
|
+
reproducible: boolean;
|
|
29
|
+
verified?: boolean;
|
|
30
|
+
diagnostics: Diagnostic[];
|
|
31
|
+
}
|
|
32
|
+
export declare class BuildError extends Error {
|
|
33
|
+
readonly diagnostics: Diagnostic[];
|
|
34
|
+
constructor(message: string, diagnostics: Diagnostic[]);
|
|
35
|
+
}
|
|
36
|
+
/** Build one document record. */
|
|
37
|
+
export declare function buildDocument(project: Project, file: string, options?: BuildOptions): Promise<BuildResult>;
|
|
38
|
+
/** Build a template as a blank form, without a record. */
|
|
39
|
+
export declare function buildBlank(project: Project, templateName: string, options?: BuildOptions): Promise<BuildResult>;
|
package/build.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BuildError = void 0;
|
|
4
|
+
exports.buildDocument = buildDocument;
|
|
5
|
+
exports.buildBlank = buildBlank;
|
|
6
|
+
const render_1 = require("@latex-kit/render");
|
|
7
|
+
const schema_1 = require("@latex-kit/schema");
|
|
8
|
+
const template_1 = require("@latex-kit/template");
|
|
9
|
+
const child_process_1 = require("child_process");
|
|
10
|
+
const crypto_1 = require("crypto");
|
|
11
|
+
const fs_1 = require("fs");
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
class BuildError extends Error {
|
|
14
|
+
diagnostics;
|
|
15
|
+
constructor(message, diagnostics) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.diagnostics = diagnostics;
|
|
18
|
+
this.name = 'BuildError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.BuildError = BuildError;
|
|
22
|
+
const sha256 = (data) => (0, crypto_1.createHash)('sha256').update(data).digest('hex');
|
|
23
|
+
function isCI() {
|
|
24
|
+
return process.env.CI === 'true' || process.env.CI === '1';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The build date: SOURCE_DATE_EPOCH if set, else the newest commit among
|
|
28
|
+
* every input file. Same commit → same date → same PDF bytes.
|
|
29
|
+
*/
|
|
30
|
+
function buildEpoch(files) {
|
|
31
|
+
const env = process.env.SOURCE_DATE_EPOCH;
|
|
32
|
+
if (env && /^\d+$/.test(env.trim()))
|
|
33
|
+
return { epoch: Number(env.trim()), reproducible: true };
|
|
34
|
+
let newest = -1;
|
|
35
|
+
let allCommitted = true;
|
|
36
|
+
for (const f of files) {
|
|
37
|
+
const t = (0, render_1.gitCommitTime)(f);
|
|
38
|
+
if (t && /^\d+$/.test(t))
|
|
39
|
+
newest = Math.max(newest, Number(t));
|
|
40
|
+
else
|
|
41
|
+
allCommitted = false;
|
|
42
|
+
}
|
|
43
|
+
if (newest < 0)
|
|
44
|
+
return { epoch: Math.floor(Date.now() / 1000), reproducible: false };
|
|
45
|
+
return { epoch: newest, reproducible: allCommitted };
|
|
46
|
+
}
|
|
47
|
+
function gitShortHead(cwd) {
|
|
48
|
+
try {
|
|
49
|
+
return (0, child_process_1.execFileSync)('git', ['rev-parse', '--short', 'HEAD'], { cwd, stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf8' }).trim();
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return 'no-git';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function run(job) {
|
|
56
|
+
const { project, template, variant, options } = job;
|
|
57
|
+
const diagnostics = job.diagnostics;
|
|
58
|
+
// 1. fill the template
|
|
59
|
+
const snippets = new Set();
|
|
60
|
+
const loadSnippet = (name, from) => {
|
|
61
|
+
const s = project.loadSnippet(name, from);
|
|
62
|
+
if (s)
|
|
63
|
+
snippets.add((0, path_1.resolve)(s.file));
|
|
64
|
+
return s;
|
|
65
|
+
};
|
|
66
|
+
const strict = options.strict ?? isCI();
|
|
67
|
+
const files = [...job.inputs, template.texFile, ...(template.schemaFile ? [template.schemaFile] : [])];
|
|
68
|
+
const { epoch, reproducible } = buildEpoch(files);
|
|
69
|
+
let docId = job.docId;
|
|
70
|
+
let minted = false;
|
|
71
|
+
if (!docId && job.mintInto) {
|
|
72
|
+
docId = (0, schema_1.mintDocId)({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
73
|
+
minted = true;
|
|
74
|
+
}
|
|
75
|
+
const evaluated = (0, template_1.evaluateTemplate)(template.compiled, {
|
|
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 });
|
|
92
|
+
const job_ = variant === 'final' ? job.name : `${job.name}-${variant}`;
|
|
93
|
+
const texPath = (0, path_1.join)(outDir, `${job_}.tex`);
|
|
94
|
+
const stamp = `${new Date(finalEpoch.epoch * 1000).toISOString().slice(0, 10)} ${gitShortHead(project.root)}`;
|
|
95
|
+
const tex = (0, template_1.wrapDocument)(evaluated.tex, { variant, stamp });
|
|
96
|
+
(0, fs_1.writeFileSync)(texPath, tex);
|
|
97
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(outDir, 'latex-kit.sty'), template_1.LATEX_KIT_STY);
|
|
98
|
+
const result = {
|
|
99
|
+
name: job.name,
|
|
100
|
+
variant,
|
|
101
|
+
tex: texPath,
|
|
102
|
+
pdf: null,
|
|
103
|
+
sha256: null,
|
|
104
|
+
docId: docId ?? null,
|
|
105
|
+
minted: false,
|
|
106
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
107
|
+
reproducible: finalEpoch.reproducible,
|
|
108
|
+
diagnostics,
|
|
109
|
+
};
|
|
110
|
+
if (options.texOnly)
|
|
111
|
+
return result;
|
|
112
|
+
// 3. render
|
|
113
|
+
const engine = options.engine ?? template.meta.engine ?? project.config.engine ?? 'lualatex';
|
|
114
|
+
const renderOpts = {
|
|
115
|
+
file: texPath,
|
|
116
|
+
root: project.root,
|
|
117
|
+
engine,
|
|
118
|
+
backend: options.backend ?? project.config.backend,
|
|
119
|
+
image: options.image ?? project.config.image,
|
|
120
|
+
platform: options.platform !== undefined ? options.platform : project.config.platform,
|
|
121
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
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
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
throw e;
|
|
137
|
+
}
|
|
138
|
+
result.pdf = rendered.pdf;
|
|
139
|
+
result.sha256 = rendered.sha256;
|
|
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 });
|
|
145
|
+
}
|
|
146
|
+
// 4. keep the ID: only after a successful final build
|
|
147
|
+
if (minted && job.mintInto) {
|
|
148
|
+
(0, schema_1.writeDocId)(job.mintInto, docId);
|
|
149
|
+
result.minted = true;
|
|
150
|
+
}
|
|
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
|
+
}
|
|
169
|
+
/** Build one document record. */
|
|
170
|
+
async function buildDocument(project, file, options = {}) {
|
|
171
|
+
const variant = options.variant ?? 'draft';
|
|
172
|
+
const diagnostics = [];
|
|
173
|
+
const doc = await (0, schema_1.loadDocument)(file, { dataDir: project.dataDir });
|
|
174
|
+
const template = await project.template(doc.record.template);
|
|
175
|
+
const at = { file: project.rel(file), line: 1, column: 1 };
|
|
176
|
+
let data = doc.data;
|
|
177
|
+
if (template.schema) {
|
|
178
|
+
const v = (0, schema_1.validate)(template.schema, doc.data);
|
|
179
|
+
if (v.ok) {
|
|
180
|
+
data = v.value;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
const sev = variant === 'final' || variant === 'signed' ? 'error' : 'warning';
|
|
184
|
+
for (const i of v.issues)
|
|
185
|
+
diagnostics.push((0, template_1.diag)(sev, `data.${i.path}: ${i.message}`, at));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const needsId = (variant === 'final' || variant === 'signed') && !doc.record.docId;
|
|
189
|
+
if (needsId && !['.json', '.yaml', '.yml'].includes((0, path_1.extname)(file).toLowerCase())) {
|
|
190
|
+
const suggestion = (0, schema_1.mintDocId)({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
191
|
+
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
|
+
throw new BuildError(`${doc.record.template}: no document ID`, diagnostics);
|
|
193
|
+
}
|
|
194
|
+
return run({
|
|
195
|
+
project,
|
|
196
|
+
template,
|
|
197
|
+
name: project.documentName(file),
|
|
198
|
+
variant,
|
|
199
|
+
data,
|
|
200
|
+
locale: doc.record.locale ?? template.meta.locale ?? project.config.locale,
|
|
201
|
+
docId: doc.record.docId,
|
|
202
|
+
inputs: [file, ...doc.refFiles],
|
|
203
|
+
diagnostics,
|
|
204
|
+
options,
|
|
205
|
+
mintInto: needsId ? file : undefined,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/** Build a template as a blank form, without a record. */
|
|
209
|
+
async function buildBlank(project, templateName, options = {}) {
|
|
210
|
+
const template = await project.template(templateName);
|
|
211
|
+
return run({
|
|
212
|
+
project,
|
|
213
|
+
template,
|
|
214
|
+
name: `${templateName}`,
|
|
215
|
+
variant: 'blank',
|
|
216
|
+
data: {},
|
|
217
|
+
locale: template.meta.locale ?? project.config.locale,
|
|
218
|
+
inputs: [],
|
|
219
|
+
diagnostics: [],
|
|
220
|
+
options: { ...options, variant: 'blank' },
|
|
221
|
+
});
|
|
222
|
+
}
|
package/check.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Diagnostic, FieldRef } from '@latex-kit/template';
|
|
2
|
+
import { Project } from './project';
|
|
3
|
+
export interface CheckResult {
|
|
4
|
+
diagnostics: Diagnostic[];
|
|
5
|
+
templates: string[];
|
|
6
|
+
documents: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Everything that can be checked without TeX: template syntax, field names
|
|
10
|
+
* against each schema, every document's data, references, and duplicate IDs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function checkProject(project: Project): Promise<CheckResult>;
|
|
13
|
+
/** Every field a template reads, including through snippets. */
|
|
14
|
+
export declare function templateFields(project: Project, name: string): Promise<{
|
|
15
|
+
fields: FieldRef[];
|
|
16
|
+
diagnostics: Diagnostic[];
|
|
17
|
+
}>;
|
package/check.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkProject = checkProject;
|
|
4
|
+
exports.templateFields = templateFields;
|
|
5
|
+
const schema_1 = require("@latex-kit/schema");
|
|
6
|
+
const template_1 = require("@latex-kit/template");
|
|
7
|
+
/**
|
|
8
|
+
* Everything that can be checked without TeX: template syntax, field names
|
|
9
|
+
* against each schema, every document's data, references, and duplicate IDs.
|
|
10
|
+
*/
|
|
11
|
+
async function checkProject(project) {
|
|
12
|
+
const diagnostics = [];
|
|
13
|
+
const templates = project.templateNames();
|
|
14
|
+
for (const name of templates) {
|
|
15
|
+
try {
|
|
16
|
+
const t = await project.template(name);
|
|
17
|
+
if (!t.jsonSchema) {
|
|
18
|
+
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
|
+
diagnostics.push(...(0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet }).diagnostics);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
diagnostics.push((0, template_1.diag)('error', e.message, { file: project.rel(project.path(project.config.templates, name)), line: 1, column: 1 }));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const files = project.documentFiles();
|
|
27
|
+
const ids = new Map();
|
|
28
|
+
for (const file of files) {
|
|
29
|
+
const at = { file: project.rel(file), line: 1, column: 1 };
|
|
30
|
+
try {
|
|
31
|
+
const doc = await (0, schema_1.loadDocument)(file, { dataDir: project.dataDir });
|
|
32
|
+
if (doc.record.docId) {
|
|
33
|
+
const prev = ids.get(doc.record.docId);
|
|
34
|
+
if (prev)
|
|
35
|
+
diagnostics.push((0, template_1.diag)('error', `document ID ${doc.record.docId} is also used by ${prev}; never reuse an ID`, at));
|
|
36
|
+
else
|
|
37
|
+
ids.set(doc.record.docId, project.rel(file));
|
|
38
|
+
}
|
|
39
|
+
const t = await project.template(doc.record.template);
|
|
40
|
+
if (t.schema) {
|
|
41
|
+
const v = (0, schema_1.validate)(t.schema, doc.data);
|
|
42
|
+
if (!v.ok)
|
|
43
|
+
for (const i of v.issues)
|
|
44
|
+
diagnostics.push((0, template_1.diag)('error', `data.${i.path}: ${i.message}`, at));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
diagnostics.push((0, template_1.diag)('error', e.message.replace(`${file}: `, ''), at));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { diagnostics, templates, documents: files.map((f) => project.rel(f)) };
|
|
52
|
+
}
|
|
53
|
+
/** Every field a template reads, including through snippets. */
|
|
54
|
+
async function templateFields(project, name) {
|
|
55
|
+
const t = await project.template(name);
|
|
56
|
+
const r = (0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet });
|
|
57
|
+
return { fields: r.fields, diagnostics: r.diagnostics };
|
|
58
|
+
}
|
package/cli.d.ts
ADDED
package/cli.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const inquirerer_1 = require("inquirerer");
|
|
5
|
+
const commands_1 = require("./commands");
|
|
6
|
+
const app = new inquirerer_1.CLI(commands_1.commands, {
|
|
7
|
+
noTty: !process.stdin.isTTY,
|
|
8
|
+
// inquirerer answers --version itself; make it report latex-kit
|
|
9
|
+
version: (0, commands_1.version)(),
|
|
10
|
+
minimistOpts: {
|
|
11
|
+
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 },
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
app.run().catch((error) => {
|
|
18
|
+
console.error(error instanceof Error ? error.message : error);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
});
|
package/commands.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CLIOptions, Inquirerer } from 'inquirerer';
|
|
2
|
+
import type { ParsedArgs } from 'minimist';
|
|
3
|
+
/** The package version, from package.json next to the built files. */
|
|
4
|
+
export declare function version(): string;
|
|
5
|
+
export declare const commands: (argv: Partial<ParsedArgs>, prompter: Inquirerer, _options: CLIOptions) => Promise<Partial<ParsedArgs>>;
|
package/commands.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
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.commands = void 0;
|
|
7
|
+
exports.version = version;
|
|
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
|
+
const doctor_1 = require("./doctor");
|
|
13
|
+
const project_commands_1 = require("./project-commands");
|
|
14
|
+
const render_1 = require("./render");
|
|
15
|
+
const usage_1 = require("./usage");
|
|
16
|
+
/** The package version, from package.json next to the built files. */
|
|
17
|
+
function version() {
|
|
18
|
+
for (const p of [(0, path_1.join)(__dirname, 'package.json'), (0, path_1.join)(__dirname, '..', 'package.json')]) {
|
|
19
|
+
if ((0, fs_1.existsSync)(p)) {
|
|
20
|
+
const pkg = JSON.parse((0, fs_1.readFileSync)(p, 'utf8'));
|
|
21
|
+
if (pkg.name === 'latex-kit')
|
|
22
|
+
return pkg.version;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return 'unknown';
|
|
26
|
+
}
|
|
27
|
+
const COMMANDS = {
|
|
28
|
+
build: project_commands_1.buildCommand,
|
|
29
|
+
check: project_commands_1.checkCommand,
|
|
30
|
+
fields: project_commands_1.fieldsCommand,
|
|
31
|
+
typegen: project_commands_1.typegenCommand,
|
|
32
|
+
sty: project_commands_1.styCommand,
|
|
33
|
+
render: render_1.renderCommand,
|
|
34
|
+
doctor: doctor_1.doctorCommand,
|
|
35
|
+
};
|
|
36
|
+
const commands = async (argv, prompter, _options) => {
|
|
37
|
+
const [command, ...rest] = (argv._ ?? []).map(String);
|
|
38
|
+
if (argv.version) {
|
|
39
|
+
console.log(version());
|
|
40
|
+
return argv;
|
|
41
|
+
}
|
|
42
|
+
if (!command || argv.help || command === 'help') {
|
|
43
|
+
(0, usage_1.help)(command === 'help' ? rest[0] : argv.help ? command : undefined);
|
|
44
|
+
return argv;
|
|
45
|
+
}
|
|
46
|
+
const run = COMMANDS[command];
|
|
47
|
+
if (!run) {
|
|
48
|
+
console.error(`unknown command: ${command}\n`);
|
|
49
|
+
(0, usage_1.help)();
|
|
50
|
+
process.exitCode = 2;
|
|
51
|
+
return argv;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const code = await run({ ...argv, _: rest }, prompter);
|
|
55
|
+
if (code !== 0)
|
|
56
|
+
process.exitCode = code;
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
if (e instanceof args_1.UsageError) {
|
|
60
|
+
console.error(yanse_1.default.red(e.message));
|
|
61
|
+
process.exitCode = 2;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return argv;
|
|
68
|
+
};
|
|
69
|
+
exports.commands = commands;
|
package/config.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Backend, Engine } from '@latex-kit/render';
|
|
2
|
+
/** latex-kit.config.ts (or .js, .mjs, .cjs, .json) at the project root. */
|
|
3
|
+
export interface LatexKitConfig {
|
|
4
|
+
/** One folder per template: `<templates>/<name>/template.tex` and optional `schema.ts`. Default `templates`. */
|
|
5
|
+
templates?: string;
|
|
6
|
+
/** Shared pieces for \snippet{name}. Default `snippets`. */
|
|
7
|
+
snippets?: string;
|
|
8
|
+
/** Shared records that references point at: `<data>/<collection>/<id>.json`. Default `data`. */
|
|
9
|
+
data?: string;
|
|
10
|
+
/** Document records: `<documents>/<name>.json|yaml|ts`. Default `documents`. */
|
|
11
|
+
documents?: string;
|
|
12
|
+
/** Generated .tex and PDFs. Default `out`. */
|
|
13
|
+
out?: string;
|
|
14
|
+
/** Default locale for dates, numbers and money. Default `en-US`. */
|
|
15
|
+
locale?: string;
|
|
16
|
+
engine?: Engine;
|
|
17
|
+
backend?: Backend;
|
|
18
|
+
image?: string;
|
|
19
|
+
platform?: string | null;
|
|
20
|
+
}
|
|
21
|
+
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform'> & {
|
|
22
|
+
file: string | null;
|
|
23
|
+
};
|
|
24
|
+
export declare function defineConfig(config: LatexKitConfig): LatexKitConfig;
|
|
25
|
+
export declare const CONFIG_FILES: string[];
|
|
26
|
+
export declare function loadConfig(root: string): Promise<ResolvedConfig>;
|
package/config.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONFIG_FILES = void 0;
|
|
4
|
+
exports.defineConfig = defineConfig;
|
|
5
|
+
exports.loadConfig = loadConfig;
|
|
6
|
+
const schema_1 = require("@latex-kit/schema");
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
function defineConfig(config) {
|
|
10
|
+
return config;
|
|
11
|
+
}
|
|
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' };
|
|
14
|
+
async function loadConfig(root) {
|
|
15
|
+
for (const name of exports.CONFIG_FILES) {
|
|
16
|
+
const file = (0, path_1.join)((0, path_1.resolve)(root), name);
|
|
17
|
+
if (!(0, fs_1.existsSync)(file))
|
|
18
|
+
continue;
|
|
19
|
+
let config;
|
|
20
|
+
if (name.endsWith('.json')) {
|
|
21
|
+
config = JSON.parse((0, fs_1.readFileSync)(file, 'utf8'));
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const mod = await (0, schema_1.loadModule)(file);
|
|
25
|
+
config = (mod.default ?? mod);
|
|
26
|
+
}
|
|
27
|
+
return { ...DEFAULTS, ...config, file };
|
|
28
|
+
}
|
|
29
|
+
return { ...DEFAULTS, file: null };
|
|
30
|
+
}
|
package/doctor.d.ts
ADDED
package/doctor.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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.doctorCommand = doctorCommand;
|
|
7
|
+
const render_1 = require("@latex-kit/render");
|
|
8
|
+
const yanse_1 = __importDefault(require("yanse"));
|
|
9
|
+
const MARK = { ok: yanse_1.default.green('✓'), warn: yanse_1.default.yellow('!'), fail: yanse_1.default.red('✗'), info: yanse_1.default.dim('·') };
|
|
10
|
+
async function doctorCommand(argv) {
|
|
11
|
+
const checks = await (0, render_1.doctor)(argv.image);
|
|
12
|
+
if (argv.json) {
|
|
13
|
+
console.log(JSON.stringify(checks, null, 2));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
for (const c of checks) {
|
|
17
|
+
console.log(`${MARK[c.status]} ${c.name.padEnd(14)} ${c.detail}`);
|
|
18
|
+
if (c.hint)
|
|
19
|
+
console.log(` ${''.padEnd(14)} ${yanse_1.default.dim(c.hint)}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return checks.some((c) => c.status === 'fail') ? 1 : 0;
|
|
23
|
+
}
|