-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
67 lines (52 loc) · 1.64 KB
/
compile.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
#!/usr/bin/env node
require('shelljs/global');
config.fatal = true;
config.silent = true;
var path = require('path');
var nunjucks = require('nunjucks');
/*
Nunjucks settings and custom filters
==========================================
*/
var env = nunjucks.configure(new nunjucks.FileSystemLoader('views'))
env.addFilter('thumbnail', function(image_path) {
var path_ext = image_path.split("."),
path = path_ext[0],
pathParts = path.split("/"),
partsTotal = pathParts.length,
name = pathParts[partsTotal - 1]
pathParts[partsTotal - 1] = ""
return pathParts.join("/")+"/"+name+"."+path_ext[1];
});
/*
Main code
==========================================
*/
rm('-rf', 'build');
mkdir('build');
var a = ['src/assets/fonts', 'src/assets/images', 'src/assets/js'];
a.forEach(dir => cp('-r', dir, 'build'));
cp('-r', 'src/assets/styles/compiled', 'build/styles')
b = ls('-R', 'src/pages').filter(file => file.slice(-5) === '.html')
b.forEach(page => {
mkdir('-p', path.join('build', path.dirname(page)));
const str = cat(path.join('src/pages', page)).toString();
const out = env.renderString(str, getContext(path.basename(page, '.html')));
out.to(path.join('build', page));
});
/*
Helper functions
==========================================
*/
function getContext (pageName) {
const defaultContext = {
__DEV__: (typeof process.env.DEV === 'string') ? process.env.DEV.trim() === '1' : process.env.NODE_ENV !== 'production',
};
const pagesContexts = {
// "index":{
// "software": require('./src/data/software.json'),
// "3d": require('./src/data/3d.json')
// }
}
return Object.assign({}, defaultContext, pagesContexts[pageName]);
}