-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
121 lines (105 loc) · 3.21 KB
/
cli.ts
File metadata and controls
121 lines (105 loc) · 3.21 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
import { getInputEssays } from "./src/articleIndex";
import { bookFiles, getInputBooks, processBook } from "./src/book";
import { essayFiles, processEssay } from "./src/essay";
import * as gen from "./src/gen";
import { $ } from "bun";
import { parseArgs } from "util";
import { latexToPDF } from "./src/pdf";
import { asBookLatex } from "./src/bookLatex";
import { generateCover } from "./src/cover";
import type { Book } from "./src/types";
await main();
async function main() {
const { values } = parseArgs({
args: Bun.argv,
options: {
essay: {
type: "string",
},
book: {
type: "string",
},
cover: {
type: "string",
},
count: {
type: "boolean",
},
},
strict: true,
allowPositionals: true,
});
const essaySlug = values.essay;
if (essaySlug) {
console.log(`Build Essay: ${essaySlug}`);
await handleEssaySlug(essaySlug);
return;
}
const bookSlug = values.book;
if (bookSlug) {
console.log(`Build Book: ${bookSlug}`);
await handleBookSlug(bookSlug);
return;
}
const coverSlug = values.cover;
if (coverSlug) {
console.log(`Build Cover: ${coverSlug}`);
await handleCoverSlug(coverSlug);
return;
}
const count = values.count;
if (count != undefined) {
console.log(`Counting pdf length`);
const inputs = await getInputBooks();
await countPages(inputs);
return;
}
await handleAllBooks();
}
async function countPages(books: Book[]) {
for (const book of books) {
const pdfInfo =
await $`pdfinfo ${gen.fullPath(book.dir, bookFiles.pdf)} | grep Pages | awk '/Pages/ {print $2}'`.text();
console.log(`[${book.slug}] ${pdfInfo}`);
}
}
async function handleAllBooks() {
const inputs = await getInputBooks();
await Promise.all(inputs.map(processBook));
console.log(`Processed: ${inputs.map((x) => x.slug).join(",")}`);
await countPages(inputs);
}
async function handleCoverSlug(slug: string) {
const inputs = await getInputBooks();
const book = inputs.find((x) => x.slug == slug);
if (!book) {
throw new Error(`Could not find ${slug}`);
}
const coverFn = book.coverFn ?? generateCover;
await coverFn(gen.fullPath(book.dir, bookFiles.coverPaperback), book, "paperback");
await coverFn(gen.fullPath(book.dir, bookFiles.coverHardcover), book, "hardcover");
await $`open ${gen.fullPath(book.dir, bookFiles.coverPaperback)}`;
await $`open ${gen.fullPath(book.dir, bookFiles.coverHardcover)}`;
}
async function handleBookSlug(slug: string) {
const inputs = await getInputBooks();
const book = inputs.find((x) => x.slug == slug);
if (!book) {
throw new Error(`Could not find ${slug}`);
}
await processBook(book);
await $`open ${gen.fullPath(book.dir, bookFiles.pdf)}`;
}
async function handleEssaySlug(slug: string) {
const inputs = await getInputEssays();
const essay = inputs.find((x) => x.slug === slug);
if (!essay) {
throw new Error(`Could not find ${slug}`);
}
await processEssay(essay);
await latexToPDF(
gen.fullPath(essay.dir, essayFiles.pdf),
asBookLatex({ title: essay.title, latexChapters: [gen.readText(essay.dir, essayFiles.xfTex)] }),
);
await $`open ${gen.fullPath(essay.dir, essayFiles.pdf)}`;
}