-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
277 lines (228 loc) · 7.25 KB
/
index.js
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import fs from "node:fs/promises";
import nodePath from "node:path";
import JSON5 from "json5";
import { Liquid } from "liquidjs";
import YAML from "yaml";
import { pathExists } from "find-up";
import puppeteer from "puppeteer";
import watcher from "@parcel/watcher";
// These are the downstream error messages thrown due to the 'fs.readFile' in
// 'getData' sometimes returning an empty string when used together with
// @parcel/watcher. They are disregarded for now until a better solution can be
// made.
const errorsToDisregard = [
"'data' must be an object or undefined, given 'null'",
"JSON5: invalid end of input at 1:1"
];
/** @type {import("./index").develop} */
export async function develop(args, rootDir = process.cwd()) {
let builder;
let subscription;
try {
const pathsToWatch = [
absolutizePath(args.data, rootDir),
absolutizePath(args.template, rootDir)
];
for (const pathToWatch of pathsToWatch) {
// eslint-disable-next-line no-await-in-loop
if (!(await pathExists(pathToWatch))) {
throw new Error(`'${pathToWatch}' does not exist.`);
}
}
builder = await getBuilder();
await builder.build(args, rootDir);
subscription = await watcher.subscribe(
rootDir,
async (watcherError, events) => {
if (watcherError !== null) {
throw watcherError;
}
const match = events.find(({ path }) => pathsToWatch.includes(path));
if (typeof match === "undefined" || match.type !== "update") {
return;
}
try {
await builder.build(args, rootDir);
} catch (error) {
if (errorsToDisregard.includes(error.message)) {
return;
}
throw error;
}
}
);
return async () => {
await subscription.unsubscribe();
await builder.close();
};
} catch (error) {
await subscription?.unsubscribe();
await builder?.close();
throw error;
}
}
/** @type {import("./index").build} */
export async function build(args, rootDir = process.cwd()) {
let builder;
try {
builder = await getBuilder();
await builder.build(args, rootDir);
} finally {
await builder?.close();
}
}
/** @type {import("./index").getBuilder} */
export async function getBuilder() {
const pdfRenderer = await getPDFRenderer();
let isClosed = false;
return {
async build({ data, template, output, options }, rootDir = process.cwd()) {
if (isClosed) {
return;
}
if (typeof rootDir !== "string") {
throw new TypeError(
`'rootDir' must be a string, given '${typeof rootDir}'`
);
}
if (!(await pathExists(rootDir))) {
throw new Error(`Root directory '${rootDir}' does not exist`);
}
const html = await renderHTML(
absolutizePath(template, rootDir),
(options?.getTemplateRenderer ?? getDefaultTemplateRenderer)(
// This should be fine as long as 'T' is set when using a custom
// renderer and left to the default 'LiquidOptions' when using the
// default renderer.
// @ts-expect-error
options?.templateOptions
),
await getData(absolutizePath(data, rootDir))
);
const outputFile = absolutizePath(output, rootDir);
const outputDir = nodePath.dirname(outputFile);
if (!(await pathExists(outputDir))) {
await fs.mkdir(outputDir, { recursive: true });
}
await fs.writeFile(
outputFile,
await pdfRenderer.render(encodeHTML(html), options?.pdfOptions)
);
},
async close() {
if (isClosed) {
return;
}
await pdfRenderer.close();
// 'isClosed' is meant to only be reassigned here and nowhere else.
// eslint-disable-next-line require-atomic-updates
isClosed = true;
}
};
}
/**
* @param {import("liquidjs/dist/liquid-options").LiquidOptions} [options]
* @returns {import("./index").RenderTemplate}
*/
function getDefaultTemplateRenderer(options) {
const liquid = new Liquid(options);
return (template, data) => liquid.parseAndRender(template, data);
}
/** @type {import("./index").getData} */
export async function getData(path) {
if (typeof path !== "string") {
throw new TypeError(`'path' must be a string, given '${typeof path}'`);
}
if (!(await pathExists(path))) {
throw new Error(`Data file '${path}' does not exist`);
}
const ext = nodePath.extname(path);
const data = await fs.readFile(path, "utf-8");
if ([".yml", ".yaml"].includes(ext)) {
return parseData(data);
}
if ([".json", ".jsonc", ".json5"].includes(ext)) {
return parseData(data, "json");
}
throw new Error(
`Only YAML, JSON, JSONC, and JSON5 formats are accepted, given ${ext}`
);
}
/** @type {import("./index").parseData} */
export function parseData(data, type = "yaml") {
if (typeof data !== "string") {
throw new TypeError(`'data' must be a string, given '${typeof data}'`);
}
if (!["yaml", "json"].includes(type)) {
throw new Error(`'type' must either be 'yaml' or 'json', given '${type}'`);
}
return type === "yaml" ? YAML.parse(data) : JSON5.parse(data);
}
/** @type {import("./index").renderHTML} */
export async function renderHTML(path, render, data) {
if (typeof path !== "string") {
throw new TypeError(`'path' must be a string, given '${typeof path}'`);
}
if (!(await pathExists(path))) {
throw new Error(`Template file '${path}' does not exist`);
}
if (typeof render !== "function") {
throw new TypeError(
`'render' must be a function, given '${typeof render}'`
);
}
if (typeof data !== "object" && typeof data !== "undefined") {
throw new TypeError(
`'data' must be an object or undefined, given '${typeof data}'`
);
}
if (data === null) {
throw new TypeError("'data' must be an object or undefined, given 'null'");
}
return render(await fs.readFile(path, "utf-8"), data);
}
/** @type {import("./index").encodeHTML} */
export function encodeHTML(html) {
if (typeof html !== "string") {
throw new TypeError(`'html' must be a string, given '${typeof html}'`);
}
return `data:text/html,${encodeURIComponent(html)}`;
}
/** @type {import("./index").getPDFRenderer} */
export async function getPDFRenderer() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
let isClosed = false;
return {
async render(url, options) {
if (isClosed) {
return Buffer.from("");
}
await page.goto(url, { waitUntil: "load" });
return page.pdf(options);
},
async close() {
if (isClosed) {
return;
}
await browser.close();
// 'isClosed' is meant to only be reassigned here and nowhere else.
// eslint-disable-next-line require-atomic-updates
isClosed = true;
}
};
}
/** @type {import("./index").absolutizePath} */
export function absolutizePath(path, rootDir = process.cwd()) {
if (typeof path !== "string") {
throw new TypeError(`'path' must be a string, given '${typeof path}'`);
}
if (typeof rootDir !== "string") {
throw new TypeError(
`'rootDir' must be a string, given '${typeof rootDir}'`
);
}
return nodePath.isAbsolute(path)
? nodePath.normalize(path)
: nodePath.join(rootDir, path);
}