-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-all.js
More file actions
257 lines (219 loc) · 9.89 KB
/
Copy pathrun-all.js
File metadata and controls
257 lines (219 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env node
// run-all.js — Cross-platform sample runner
//
// Renders every JSON sample in samples/render/**/*.json to
// samples/output/<category>/<name>.pdf, then reports a summary.
//
// Prerequisites:
// - pdfnative-cli installed globally: npm install -g pdfnative-cli
// - Node.js >= 20
//
// Usage (from the repo root):
// node samples/run-all.js
//
// Flags:
// --category <name> Only process samples in samples/render/<name>/
// --clean Delete samples/output/ before running
import { spawnSync } from 'node:child_process';
import { readdirSync, mkdirSync, rmSync, existsSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join, basename, extname, relative } from 'node:path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const RENDER_DIR = join(__dirname, 'render');
const OUTPUT_DIR = join(__dirname, 'output');
// Per-category extra CLI flags (v0.2.0+). Categories not listed here render with
// the default DocumentParams variant and no extra flags.
const CATEGORY_FLAGS = {
'table-variant': ['--variant', 'table'],
'headers-footers': [
'--header-left', '{title}',
'--header-right', '{date}',
'--footer-center', 'Page {page} of {pages}',
],
encryption: [], // file-name-driven (see FILE_FLAGS): aes128 vs aes256 per file.
attachments: [
'--tagged', 'pdfa3b',
'--attachment', join(__dirname, 'render', 'attachments', 'invoice.xml')
+ ':application/xml:Source:Structured invoice payload',
],
multilang: [], // file-name-driven; resolved below
// v0.3.0+ font samples are file-name-driven (see FILE_FLAGS): each JSON in
// font/ registers a different set of bundled fonts, so no category-wide flag
// applies — e.g. 02-new-scripts.json must NOT inherit the Latin-only flags,
// otherwise its non-Latin scripts render as .notdef tofu.
font: [],
};
// Categories whose JSON samples are intentionally skipped by run-all because
// they require multi-file orchestration or interactive input (e.g. --watch).
const SKIP_CATEGORIES = new Set(['watch', 'template']);
// Individual JSON files that are *not* renderable documents (e.g. an
// OutlineItem[] tree consumed by --outline) and must be skipped by discovery.
const SKIP_FILES = new Set(['02-outline-tree.json']);
/** Per-file overrides (within a category). */
// Note: --lang <code> for non-Latin scripts requires the matching bundled font
// to be registered first via --font (see samples/render/font/ and
// samples/render/multilang/ for guidance). Each font/ sample registers exactly
// the fonts its content needs so it renders real glyphs out of the box.
const FILE_FLAGS = {
// document/ — 06 caps the block ceiling as a large-report guard.
'06-max-blocks.json': ['--max-blocks', '10000'],
// font/ — each sample registers a different bundled font set.
'01-latin.json': ['--font', 'latin', '--lang', 'latin'],
'02-new-scripts.json': [
'--font', 'te', '--font', 'si', '--font', 'km', '--font', 'my',
'--font', 'bo', '--font', 'am', '--font', 'color-emoji',
'--lang', 'te,si,km,my,bo,am,color-emoji',
],
'03-emoji.json': ['--font', 'emoji', '--lang', 'emoji'],
// outline/ — 01 derives bookmarks from headings via --outline auto.
'01-headings.json': ['--outline', 'auto'],
// math/ — register the bundled math font so symbols render as real glyphs.
'01-math.json': ['--font', 'latin', '--font', 'math'],
// encryption/ — algorithm differs per file (passwords come from CATEGORY_ENV).
'01-aes128-protected.json': ['--encrypt-algorithm', 'aes128', '--encrypt-permissions', 'print'],
'02-aes256-protected.json': ['--encrypt-algorithm', 'aes256', '--encrypt-permissions', 'print'],
// watermark/ — 03 layers the stamp on entirely from the CLI.
'03-cli-flags.json': [
'--watermark-text', 'CONFIDENTIAL',
'--watermark-opacity', '0.15',
'--watermark-angle', '45',
'--watermark-color', '#FF3B30',
'--watermark-font-size', '64',
'--watermark-position', 'background',
],
};
// Per-category env-var bootstrap (e.g. encryption passwords).
const CATEGORY_ENV = {
encryption: {
PDFNATIVE_ENCRYPT_OWNER_PASS: process.env.PDFNATIVE_ENCRYPT_OWNER_PASS ?? 'owner-secret',
PDFNATIVE_ENCRYPT_USER_PASS: process.env.PDFNATIVE_ENCRYPT_USER_PASS ?? 'open-sesame',
},
};
// ── CLI flags ──────────────────────────────────────────────────────────────
const argv = process.argv.slice(2);
const categoryFilter = (() => {
const i = argv.indexOf('--category');
return i !== -1 ? argv[i + 1] : null;
})();
const doClean = argv.includes('--clean');
if (doClean && existsSync(OUTPUT_DIR)) {
process.stdout.write('→ Cleaning samples/output/ …\n');
rmSync(OUTPUT_DIR, { recursive: true, force: true });
}
mkdirSync(OUTPUT_DIR, { recursive: true });
// ── Discover sample files ──────────────────────────────────────────────────
/** @type {{ category: string; input: string; output: string }[]} */
const jobs = [];
for (const category of readdirSync(RENDER_DIR, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => d.name)) {
if (categoryFilter && category !== categoryFilter) continue;
if (SKIP_CATEGORIES.has(category)) continue;
const categoryDir = join(RENDER_DIR, category);
const outCategoryDir = join(OUTPUT_DIR, category);
mkdirSync(outCategoryDir, { recursive: true });
for (const entry of readdirSync(categoryDir, { withFileTypes: true })) {
if (!entry.isFile() || extname(entry.name) !== '.json') continue;
if (SKIP_FILES.has(entry.name)) continue;
const stem = basename(entry.name, '.json');
jobs.push({
category,
input: join(categoryDir, entry.name),
output: join(outCategoryDir, `${stem}.pdf`),
});
}
}
if (jobs.length === 0) {
process.stderr.write('No JSON samples found.\n');
process.exit(1);
}
// ── Run jobs ───────────────────────────────────────────────────────────────
const PAD = 52;
let passed = 0;
let failed = 0;
process.stdout.write(`\nRendering ${jobs.length} sample(s)…\n\n`);
for (const job of jobs) {
const label = relative(__dirname, job.input).replace(/\\/g, '/');
process.stdout.write(` ${label.padEnd(PAD)}`);
const extraCategoryFlags = CATEGORY_FLAGS[job.category] ?? [];
const extraFileFlags = FILE_FLAGS[basename(job.input)] ?? [];
const env = {
...process.env,
...(CATEGORY_ENV[job.category] ?? {}),
};
const result = spawnSync(
'pdfnative',
['render', '--input', job.input, '--output', job.output, ...extraCategoryFlags, ...extraFileFlags],
{
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
shell: true,
env,
},
);
if (result.status === 0) {
process.stdout.write('✓\n');
passed++;
} else {
process.stdout.write('✗\n');
const err = (result.stderr ?? '').trim();
if (err) process.stderr.write(` Error: ${err}\n`);
failed++;
}
}
// ── Summary ────────────────────────────────────────────────────────────────
process.stdout.write(`\n${'─'.repeat(PAD + 4)}\n`);
process.stdout.write(` ${passed} passed`);
if (failed > 0) process.stdout.write(`, ${failed} FAILED`);
process.stdout.write(`\n Output: ${OUTPUT_DIR}\n\n`);
if (failed > 0) process.exit(1);
// ── Node.js driver scripts (multilang and similar) ─────────────────────────
//
// Some samples cannot be rendered by the pdfnative CLI alone because they
// require programmatic font loader registration (registerFonts) before the
// render call. These are implemented as standalone Node.js scripts that import
// pdfnative directly. They live alongside the JSON samples in their category.
/** @type {string[]} */
const NODE_DRIVER_DIRS = ['multilang'];
/** @type {{ script: string }[]} */
const driverJobs = [];
for (const category of NODE_DRIVER_DIRS) {
if (categoryFilter && category !== categoryFilter) continue;
const categoryDir = join(RENDER_DIR, category);
if (!existsSync(categoryDir)) continue;
for (const entry of readdirSync(categoryDir, { withFileTypes: true })) {
if (!entry.isFile() || extname(entry.name) !== '.js') continue;
driverJobs.push({ script: join(categoryDir, entry.name) });
}
}
if (driverJobs.length > 0) {
process.stdout.write(`\nRunning ${driverJobs.length} Node.js driver script(s)…\n\n`);
let driverPassed = 0;
let driverFailed = 0;
for (const job of driverJobs) {
const label = relative(__dirname, job.script).replace(/\\/g, '/');
process.stdout.write(` ${label.padEnd(PAD)}`);
const result = spawnSync('node', [job.script], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
});
if (result.status === 0) {
process.stdout.write('✓\n');
driverPassed++;
} else {
process.stdout.write('✗\n');
const err = (result.stderr ?? '').trim();
if (err) process.stderr.write(` Error: ${err}\n`);
driverFailed++;
}
}
process.stdout.write(`\n${'─'.repeat(PAD + 4)}\n`);
process.stdout.write(` ${driverPassed} passed`);
if (driverFailed > 0) {
process.stdout.write(`, ${driverFailed} FAILED`);
process.stdout.write(`\n Output: ${OUTPUT_DIR}\n\n`);
process.exit(1);
}
process.stdout.write(`\n Output: ${OUTPUT_DIR}\n\n`);
}