forked from salomonelli/best-resume-ever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.js
executable file
·83 lines (76 loc) · 2.09 KB
/
export.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
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
const Rx = require('rxjs/Rx');
const http = require('http');
const config = require('../config');
const fetchResponse = () => {
return new Promise((res, rej) => {
try {
const req = http.request(`http://localhost:${config.dev.port}/#/`, response => res(response.statusCode));
req.on('error', (err) => rej(err));
req.end();
} catch (err) {
rej(err);
}
});
};
const waitForServerReachable = () => {
return Rx.Observable
.interval(1000)
.mergeMap(async () => {
try {
const statusCode = await fetchResponse();
if (statusCode === 200) return true;
} catch (err) {}
return false;
})
.filter(ok => !!ok);
};
const timedOut = timeout => {
return new Promise(res => {
setTimeout(res, timeout);
});
};
const convert = async () => {
await waitForServerReachable().first().toPromise();
console.log('Connected to server ...');
console.log('Exporting ...');
try {
const directories = getResumesFromDirectories();
directories.forEach(async (dir) => {
const browser = await puppeteer.launch({
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.goto(`http://localhost:${config.dev.port}/#/resume/` + dir.name, {
waitUntil: 'networkidle2'
});
await page.pdf({
path: path.join(__dirname, '../pdf/' + dir.name + '.pdf'),
format: 'A4'
});
await browser.close();
});
} catch (err) {
throw new Error(err);
}
console.log('Finished exports.');
};
const getResumesFromDirectories = () => {
const directories = getDirectories();
return directories
.map(dir => {
let fileName = dir.replace('.vue', '');
return {
path: fileName,
name: fileName
};
});
};
const getDirectories = () => {
const srcpath = path.join(__dirname, '../src/resumes');
return fs.readdirSync(srcpath)
.filter(file => file !== 'resumes.js' && file !== 'template.vue' && file !== 'options.js');
};
convert();