-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-snippets-pdf.js
57 lines (46 loc) · 1.75 KB
/
create-snippets-pdf.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
const spawn = require("child_process").spawn
const path = require(`path`)
const fs = require(`fs`)
const puppeteer = require("puppeteer")
const languageFilters = ["all", "typescript", "javascript", "node", "csharp", "shell"]
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
const createPdfs = async () => {
const dir = path.join(__dirname, `public`, `exports`)
for (const index in languageFilters) {
const languageFilter = languageFilters[index];
const url = `http://localhost:9000/snippets-pdf-format?languageFilter=${languageFilter}`
const fileNameToWrite = `full-stack-snippets-${languageFilter}.pdf`
const file = path.join(__dirname, `public`, `exports`, fileNameToWrite)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
console.log("Issuing npm run serve...")
const child = spawn("npm run serve", { shell: true, detached: true })
await sleep(5000)
console.log("Launching puppeteer...")
const browser = await puppeteer.launch({headless: "new"})
const page = await browser.newPage()
await page.goto(url, {
waitUntil: "networkidle0",
})
await page.pdf({
printBackground: true,
path: file,
format: "Letter",
margin: {
top: "20px",
bottom: "20px",
left: "20px",
right: "20px",
},
})
console.log(`PDF with language filter "${languageFilter}" successfully exported...`)
process.kill(-child.pid)
console.log("Killed gatsby server.")
}
console.log("Done with all PDF exports! Exiting process...")
process.exit()
}
createPdfs()