-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
121 lines (101 loc) · 3.72 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const pdf = require('html-pdf');
const path = require('path');
const { marked } = require('marked');
const fs = require('fs-extra');
const os = require('os');
const exiftool = require('node-exiftool');
const exiftoolBinary = require('dist-exiftool');
const CONTENT_PLACEHOLDER = '[!!CONTENT_HERE!!]';
const SOURCE_DIRECTORY = 'src/';
const OUTPUT_DIRECTORY = 'dist/';
const HTML_OUTPUT_PATH = OUTPUT_DIRECTORY + 'index.html';
const PDF_OUTPUT_PATH = OUTPUT_DIRECTORY + 'mjwcv.pdf';
const PDF_OPTIONS = {
localUrlAccess: true,
format: 'A4',
orientation: 'portrait',
border: {
top: '1cm',
right: '1cm',
bottom: 0,
left: '1cm'
}
};
const PDF_METADATA = {
Title: 'Curriculum Vitae - Mark Webb',
Author: 'Mark Webb ([email protected])',
Subject: 'An overview of Mark\'s skills, experience and education',
Keywords: 'contract, cv, developer, resume, software, work'
}
async function clean() {
console.log('cleaning output');
await fs.rm(OUTPUT_DIRECTORY, {recursive:true, force:true});
await fs.mkdir(OUTPUT_DIRECTORY);
}
async function generateHtml() {
console.log('generating HTML from markdown');
const markdownContent = await fs.readFile(SOURCE_DIRECTORY + 'index.content.md', {encoding:'utf8'});
const htmlContent = marked(markdownContent);
const htmlTemplate = await fs.readFile(SOURCE_DIRECTORY + 'index.template.html', {encoding:'utf8'});
const htmlOutput = htmlTemplate.replace(CONTENT_PLACEHOLDER, htmlContent);
await fs.writeFile(HTML_OUTPUT_PATH, htmlOutput);
console.log('saved ' + HTML_OUTPUT_PATH);
}
async function copyAssets(){
console.log('copying assets');
await fs.copy(SOURCE_DIRECTORY + 'assets', OUTPUT_DIRECTORY + 'assets');
await fs.copy(SOURCE_DIRECTORY + 'assets/icons/favicon/favicon.ico', OUTPUT_DIRECTORY + 'favicon.ico');
console.log('copied ' + SOURCE_DIRECTORY + 'assets to ' + OUTPUT_DIRECTORY + 'assets');
}
async function generatePDF() {
await installFontsLocally()
let html = await fs.readFile(HTML_OUTPUT_PATH, {encoding:'utf8'})
let htmlWithAbsolutePaths = html
.replace(/href="\/assets\//g, 'href="file:///' + path.join(__dirname, OUTPUT_DIRECTORY, 'assets/'))
.replace('assets/print.css', 'assets/print-pdf.css');
const pdfResource = await new Promise((resolve, reject) => {
pdf.create(htmlWithAbsolutePaths, PDF_OPTIONS)
.toFile(PDF_OUTPUT_PATH, (err, res) => {
if(err) {
reject(err);
} else {
resolve(res);
}
})
});
console.log('saved PDF to ' + pdfResource.filename);
await updateMetadata(pdfResource.filename, PDF_METADATA);
}
async function installFontsLocally() {
if(os.platform() === 'linux') {
const fontPath = path.join(SOURCE_DIRECTORY, 'assets', 'fonts')
const installPath = path.join(os.homedir(), '.fonts', 'truetype');
if(!fs.existsSync(installPath)) {
await fs.mkdir(installPath, {recursive:true});
}
await fs.copy(fontPath, installPath);
}
}
async function updateMetadata(filePath, metadata) {
const exiftoolProcess = new exiftool.ExiftoolProcess(exiftoolBinary);
await exiftoolProcess.open();
try {
await exiftoolProcess.writeMetadata(filePath, metadata, ['overwrite_original', 'codedcharacterset=utf8']);
console.log(`updated metadata for ${filePath}`);
} finally {
await exiftoolProcess.close();
}
}
async function build() {
await clean();
await generateHtml();
await copyAssets();
await generatePDF()
}
(async () => {
try{
await build();
} catch (ex) {
console.error(ex);
}
})();