forked from antfu/resume
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
60 lines (54 loc) · 1.56 KB
/
build.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
const fs = require('fs-extra')
const axios = require('axios')
const puppeteer = require('puppeteer')
const gist = 'loiaconof/ce50e4bb902546205db9cd9dfc468865'
async function buildHTML() {
await fs.remove('./dist')
await fs.ensureDir('./dist')
let resume
if (fs.existsSync('./resume.json')) {
console.log(`Loading from locale "resume.json"`)
resume = JSON.parse(fs.readFileSync('./resume.json', 'utf-8'))
} else {
console.log(`Downloading resume... [${gist}]`)
const { data } = await axios.get(`https://gist.githubusercontent.com/${gist}/raw/resume.json`)
resume = data
}
console.log('Rendering...')
const html = await require("./index.js").render(resume)
console.log('Saving file...')
fs.writeFileSync('./dist/index.html', html, 'utf-8')
console.log('Done')
return html
}
async function buildPDF(html) {
const browser = await puppeteer.launch({ headless: true })
const page = await browser.newPage();
console.log('Opening puppeteer...')
await page.setContent(html, { waitUntil: 'networkidle0' })
console.log('Generating PDF...')
const pdf = await page.pdf({
format: 'A4',
displayHeaderFooter: false,
printBackground: true,
margin: {
top: '0.4in',
bottom: '0.4in',
left: '0.4in',
right: '0.4in',
}
})
await browser.close()
console.log('Saving file...')
fs.writeFileSync('./dist/resume.pdf', pdf)
console.log('Done')
return pdf
}
async function buildAll() {
const html = await buildHTML()
await buildPDF(html)
}
buildAll().catch(e => {
console.error(e)
process.exit(1)
})