-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.mjs
63 lines (52 loc) · 1.47 KB
/
run.mjs
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 pug from "pug";
import fs from "fs/promises";
import puppeteer from "puppeteer";
import * as sass from "sass";
import { debounce } from "lodash-es";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { program } from "commander";
program.option("-w, --watch");
program.parse();
const ac = new AbortController();
const fn = async () => {
const browser = await puppeteer.launch();
const __dirname = dirname(fileURLToPath(import.meta.url));
const styling = sass.compile("./resume.scss");
const htmFile = "index.html";
const html = pug.renderFile("anthonyou.pug", {
pretty: false,
styling: styling.css,
});
await fs.writeFile(htmFile, html);
const page = await browser.newPage();
await page.goto(`file://${__dirname}/${htmFile}`);
await page.pdf({ path: "anthonyou.pdf" });
await browser.close();
};
const dfn = debounce(fn, 1000);
const options = program.opts();
if (options.watch) {
// Start watching the file
(async () => {
console.log("watching", new Date());
try {
const watcher = fs.watch("./", { signal: ac.signal });
for await (const event of watcher) {
if (event.filename.match("pug|scss")) {
console.log(`running ${new Date()}`);
await dfn();
}
}
} catch (err) {
if (err.name === "AbortError") return;
throw err;
}
})();
process.on("SIGINT", function () {
console.log("sigint");
ac.abort();
});
} else {
fn();
}