forked from mayo/oyam.ca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
281 lines (225 loc) · 6.15 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
var Metalsmith = require('metalsmith');
var permalinks = require('metalsmith-permalinks');
var stencils = require('metalsmith-stencils');
var drafts = require('metalsmith-drafts');
var watch = require('metalsmith-watch');
var serve = require('metalsmith-serve');
var branch = require('metalsmith-branch');
var collections = require('metalsmith-collections');
var title = require('metalsmith-title');
var ignore = require('metalsmith-ignore');
var excerpts = require('metalsmith-excerpts');
var asset = require('metalsmith-static');
var debug = require('metalsmith-debug');
var beautify = require('metalsmith-beautify');
var autoprefixer = require('metalsmith-autoprefixer');
var markdown = require('metalsmith-markdownit');
var md_implicit_figures = require('markdown-it-implicit-figures')
var minify_css = require('metalsmith-clean-css');
//var minify_js = require('metalsmith-uglify');
//var minify_html = require('metalsmith-html-minifier');
/**
* Environment helper
*/
var buildEnv = function() {
this.PRODUCTION = "production";
this.DEVELOPMENT = "development";
this.isProduction = process.env.ENVIRONMENT == this.PRODUCTION;
this.isDevelopment = process.env.ENVIRONMENT =! this.PRODUCTION;
this.environment = (this.isProduction) ? this.PRODUCTION : this.DEVELOPMENT;
this.watch = process.env.WATCH;
this.serve = process.env.SERVE;
return this;
}();
console.log("Build environment: " + buildEnv.environment );
/**
* Build.
*/
var metalsmith = Metalsmith(__dirname);
var sourceDir = "content";
metalsmith
.source(sourceDir)
.destination("./deploy")
.clean(true)
//set global metadata
.metadata({
"author": {
"name": "Mayo Jordanov",
"email": "[email protected]",
"twitter": "@oyam"
},
"site": {
"description": "Mayo Jordanov; software developer, photographer, climber, runner, hiker, adventurer, explorer",
"keywords": "mayo jordanov software development photography adventure explore climbing running hiking consulting tech javascript design",
"source_dir": sourceDir
},
"github": {
"base": "https://github.com/mayo/oyam.ca",
"edit": "/edit/master"
},
"analytics": {
"google": {
"id": "UA-803388-1"
}
},
"now": new Date(),
"production": buildEnv.isProduction,
"slides": require("./metadata/slides.json"),
"links": require("./metadata/links.json")
})
.use(asset([
//font-awesome.io
{
"src": "node_modules/font-awesome/css/font-awesome.min.css",
"dest": "media/css/font-awesome.min.css"
},
{
"src": "node_modules/font-awesome/fonts",
"dest": "media/fonts"
},
//load normalize.css from module to keep it up to date more easily
{
"src": "node_modules/normalize.css/normalize.css",
"dest": "media/css/normalize.css"
},
//unsemantic fluid layout
{
"src": "node_modules/unsemantic/assets/stylesheets/unsemantic-grid-responsive-no-ie7.css",
"dest": "media/css/unsemantic-grid-responsive-no-ie7.css"
},
//microevent for slideshow
{
"src": "node_modules/microevent/microevent.js",
"dest": "media/js/microevent.js"
}
]))
//ignore temp files
.use(ignore([
'**/.*.swp',
'**/.DS_Store',
]))
.use(storeSource())
.use(drafts())
.use(markdown('default', {
html: true,
typographer: true
}).use(md_implicit_figures, {
dataType: true,
figcaption: true
})
)
.use(branch("blog/*/**")
.use(setMetadata({ 'template': 'blog/article.html' }))
.use(title({ remove: true }))
.use(excerpts())
.use(permalinks({
'relative': false
}))
)
//process titles for normal pages after blog, because blog is treated differently
.use(title())
.use(collections({
"travel": {
"sortBy": 'created',
reverse: true
},
//The last collection defines article's previous/next links
"articles": {
"pattern": "blog/*/**",
"sortBy": 'created',
reverse: true
},
}))
.use(stencils({
"engine": "swig",
"directory": "templates",
"autoescape": false,
"extendsPattern": '{% extends "%s" %}',
"blockPattern": [ '{% block %s %}', '{% endblock %}' ]
}))
.use(autoprefixer())
;
if (!buildEnv.isProduction) {
metalsmith
.use(beautify({
"indent_size": 2,
"indent_char": " "
}))
.use(debug())
;
}
if (!buildEnv.isProduction && buildEnv.watch) {
metalsmith
.use(watch({
livereload: false,
paths: {
"${source}/**/*": true,
"templates/**/*": "**/*",
"metadata/**/*": "**/*"
}
}))
;
}
if (!buildEnv.isProduction && buildEnv.serve) {
metalsmith
.use(serve({
port: 8080,
verbose: true
}))
;
}
if (buildEnv.isProduction) {
metalsmith
//TODO: this breaks metalsmith-templates
// .use(minify_html())
//TODO: generates .min.js files, which is not exactly useful without rewriting the source files
// .use(minify_js({}))
.use(minify_css())
;
}
metalsmith
.build(function(err){
if (err) throw err;
console.log("Build complete!");
});
function setMetadata(meta, force) {
force = (typeof force !== 'undefined') ? force : false;
/**
* Set Metadata plugin.
*
* Sets specified metadata on given files. Optionally, force is given to
* force overwriting of existing metadata keys.
*
* @param {Object} files
* @param {Metalsmith} metalsmith
* @param {Function} done
*/
return function(files, metalsmith, done){
Object.keys(files).forEach(function(file) {
Object.keys(meta).forEach(function(key) {
if (force || !files[file][key]) {
files[file][key] = meta[key];
}
})
})
done();
}
}
/*
* Stores the source file in metadata. Optional argument defines the metadata key
* to store the information in, defaults to 'source_file'
*/
function storeSource(key) {
var key = (typeof key !== 'undefined') ? key : 'source_file';
/**
* @param {Object} files
* @param {Metalsmith} metalsmith
* @param {Function} done
*/
return function(files, metalsmith, done){
Object.keys(files).forEach(function(file) {
files[file][key] = file
})
done();
}
}