This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.js
161 lines (147 loc) · 3.65 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var fs = require('fs')
// Metalsmith
var Metalsmith = require('metalsmith')
var permalinks = require('metalsmith-permalinks')
var metadata = require('metalsmith-metadata')
var collections = require('metalsmith-collections')
var markdown = require('metalsmith-markdownit')
var layouts = require('metalsmith-layouts')
var minify = require('metalsmith-html-minifier')
var defaults = require('metalsmith-default-values')
var drafts = require('metalsmith-drafts')
// PostCSS
var postcss = require('postcss')
// PDF
var pdf = require('html-pdf')
/* Metalsmith
******************************************************************************/
var siteBuild = Metalsmith(__dirname)
.source('source')
.destination('_build')
.use(metadata({
'site': 'site.yaml',
'person': 'person.yaml'
}))
.use(drafts())
.use(collections({
education: {
pattern: 'education/**/*.md',
sortBy: 'startDate',
reverse: true
},
experience: {
pattern: 'experience/**/*.md',
sortBy: 'startDate',
reverse: true
},
pages: {
pattern: '*.md'
}
}))
.use(defaults([{
pattern: '*/**/*.md',
defaults: {
draft: true
}
}]))
// HTML
.use(markdown({
html: true,
xhtmlOut: true,
typographer: true,
linkify: true
}))
.use(permalinks({
pattern: ':title',
relative: false
}))
.use(layouts({
engine: 'pug',
pretty: true,
moment: require('moment'),
contrast: require('get-contrast'),
directory: 'templates',
default: 'default.pug',
pattern: '**/*.html'
}))
.use(drafts())
if (process.env.NODE_ENV === 'production') {
siteBuild.use(minify())
}
siteBuild.build(function (err) {
if (err) {
console.log(err)
} else {
console.log('Metalsmith complete!\n')
stylesheets()
if (process.env.NODE_ENV === 'print') {
print()
}
}
})
/* PostCSS
******************************************************************************/
function stylesheets () {
var css = fs.readFileSync('css/main.css', 'utf-8')
var plugins = [
require('postcss-import'),
require('postcss-nested'),
require('postcss-custom-properties'),
require('postcss-custom-media'),
require('postcss-color-function'),
require('postcss-focus'),
require('autoprefixer')({
browsers: ['last 2 versions', '> 5%']
}),
require('css-mqpacker'),
require('cssnano')
]
if (process.env.NODE_ENV === 'production') {
plugins.push(
require('postcss-uncss')({
html: ['_build/**/*.html']
})
)
}
postcss(plugins)
.process(css, {
from: 'css/main.css',
to: '_build/css/main.css',
map: { inline: false }
})
.then(function (result) {
if (result.warnings()) {
result.warnings().forEach(warn => {
console.warn(warn.toString())
})
}
fs.mkdirSync('_build/css')
fs.writeFileSync('_build/css/main.css', result.css, 'utf-8')
if (result.map) fs.writeFileSync('_build/css/main.css.map', result.map, 'utf-8')
console.log('PostCSS Complete!\n')
})
}
/* PDF
******************************************************************************/
function print () {
var html = fs.readFileSync('_build/index.html', 'utf8')
var options = {
height: '11in',
width: '8.5in',
type: 'pdf',
base: 'http://localhost:8008'
}
var server = require('browser-sync').create()
server.init({
server: '_build',
port: 8008,
open: false,
ui: false
})
pdf.create(html, options).toFile('resume.pdf', function (err, res) {
if (err) return console.log(err)
server.exit()
console.log('\nPDF generation complete!\n')
process.exit()
})
}