-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
74 lines (62 loc) · 1.74 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const request = require('request')
const qs = require('querystring')
const CV = require('./cv')
const {
GITHUB_USER: githubUser,
GITHUB_REPO: githubRepo,
GITHUB_DATA_PATH: githubDataPath,
PDFLAYER_ACCESS_KEY: pdfLayerAccessKey
} = process.env
const path = `/${githubUser}/${githubRepo}/master/${githubDataPath}`
function getData (cb) {
if (!githubUser) throw new Error('Missing Github user')
if (!githubRepo) throw new Error('Missing Github repo name')
if (!githubDataPath) throw new Error('Missing Github data path')
request.get(`https://raw.githubusercontent.com${path}`, (err, res, body) => {
if (err) return cb(err)
if (res.statusCode !== 200) return cb(body)
try {
const parsed = JSON.parse(body)
cb(null, parsed)
} catch (err) {
console.error('Parse error', err)
cb(body)
}
})
}
function buildCV (data, opt, cb) {
if (!pdfLayerAccessKey) throw new Error('Missing PdfLayer access key')
const html = CV(data)
const queryString = qs.stringify(Object.assign({}, opt, {
access_key: pdfLayerAccessKey,
force: 1,
use_print_media: 1,
margin_top: 70,
margin_bottom: 70,
margin_left: 70,
margin_right: 70
}))
request.post({
url: `http://api.pdflayer.com/api/convert?${queryString}`,
form: { document_html: html },
encoding: null
}, (err, res, body) => {
if (err) return cb(err)
if (res.statusCode !== 200) return cb(body)
cb(null, body)
})
}
module.exports = (writeData, opt, cb) => {
if (typeof opt === 'function') {
cb = opt
opt = {}
}
getData((err, data) => {
if (err) return cb(err)
buildCV(data, opt, (err, buf) => {
if (err) return cb(err)
writeData(buf, cb)
})
})
}
module.exports.buildCV = buildCV