-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePdfLoader.ts
63 lines (49 loc) · 1.67 KB
/
generatePdfLoader.ts
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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { z } from 'zod';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const infoSchema = z.object({
title: z.string(),
speaker: z.object({
name: z.string(),
description: z.string(),
imageUrl: z.union([z.string().url(), z.literal('')]),
links: z.array(z.string().url()),
}),
appendixLinks: z.array(z.string().url()),
});
const pdfsDir = path.join(__dirname, 'pdfs');
const directories = fs
.readdirSync(pdfsDir)
.filter((file) => fs.statSync(path.join(pdfsDir, file)).isDirectory());
const imports: string[] = [];
const pdfEntries: string[] = [];
directories.forEach((dir, index) => {
const infoPath = path.join(pdfsDir, dir, 'info.json');
const pdfPath = path.join(pdfsDir, dir, 'presentation.pdf');
if (!fs.existsSync(infoPath)) {
throw new Error(`Missing info.json in directory ${dir}`);
}
if (!fs.existsSync(pdfPath)) {
throw new Error(`Missing presentation.pdf in directory ${dir}`);
}
const infoData = JSON.parse(fs.readFileSync(infoPath, 'utf-8'));
try {
infoSchema.parse(infoData);
} catch (e) {
throw new Error(`Invalid info.json in directory ${dir}: ${e}`);
}
imports.push(`import f${index} from './${dir}/info.json';`);
imports.push(`import p${index} from './${dir}/presentation.pdf';`);
pdfEntries.push(` { file: p${index}, info: f${index} }`);
});
const loaderContent = `
${imports.join('\n')}
export const pdfs = [
${pdfEntries.join(',\n')}
];
`;
fs.writeFileSync(path.join(pdfsDir, 'loader.ts'), loaderContent.trim());
console.log('loader.ts has been generated successfully.');