-
Notifications
You must be signed in to change notification settings - Fork 256
/
gulpfile.js
188 lines (160 loc) · 5.71 KB
/
gulpfile.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
var _gulp = require('gulp'),
_handlebars = require('handlebars'),
_fs = require('fs-extra'),
_path = require('path'),
_connect = require('gulp-connect'),
_shared = { "variables": {} };
//--------------- Constants ----------------
const _contentRootPath = "./src/content";
const _outputRootPath = "./build";
//--------------- End ----------------
//--------------- Helpers ----------------
var parseJsonFromFile = function (path) {
var jsonStr = _fs.readFileSync(path, { encoding: 'utf8' });
var jsonObj = JSON.parse(jsonStr);
return jsonObj;
};
var getPageConfig = function (rootPath, pageConfig) {
var res = {};
if (pageConfig && pageConfig.root) {
var configPath = _path.join(rootPath, pageConfig.root, 'config.json');
var config = parseJsonFromFile(configPath);
res = Object.assign(pageConfig, config);
res.absoluteRoot = _path.join(rootPath, pageConfig.root);
}
else {
res = pageConfig;
}
return res;
};
var getTemplate = function (templatePath) {
var source = _fs.readFileSync(templatePath, { encoding: 'utf8' });
var template = _handlebars.compile(source);
return template;
}
var renderPage = function (pageConfig, globalConfig) {
var template = getTemplate("./src/templates/index-template.html");
var context = pageConfig;
context.menu = globalConfig.menu;
context.footerMenu = globalConfig.footerMenu;
context.socials = globalConfig.socials;
context.downloadLink = globalConfig.downloadLink;
var html = template(context);
var dir = _path.join(_path.resolve(), _outputRootPath, pageConfig.absoluteRoot.replace("src\\content\\", "").replace("src/content/", ""));
_fs.ensureDirSync(dir);
//copy assets to build assets directory
try {
_fs.copySync(_path.join(pageConfig.absoluteRoot, "assets"), _path.join(dir, "assets"));
} catch (ex) {
//ignore exception because this is optional step
}
_fs.writeFileSync(_path.join(dir, "index.html"), html, { encoding: 'utf8' });
};
//--------------- End ----------------
//--------------- Gulp tasks ----------------
_gulp.task('build', function (done) {
//init handlebars
_handlebars.registerHelper({
block: function (block) {
var template = getTemplate("./src/templates/" + block.templateName + "-template.html");
var html = template(block.content);
return html;
},
json: function (obj) {
return new _handlebars.SafeString(JSON.stringify(obj));
},
var: function (key) {
if (_shared.variables[key])
return new _handlebars.SafeString(_shared.variables[key]);
else
return new _handlebars.SafeString(key);
},
eq: function (v1, v2) {
return v1 === v2;
},
ne: function (v1, v2) {
return v1 !== v2;
},
lt: function (v1, v2) {
return v1 < v2;
},
gt: function (v1, v2) {
return v1 > v2;
},
lte: function (v1, v2) {
return v1 <= v2;
},
gte: function (v1, v2) {
return v1 >= v2;
},
and: function () {
return Array.prototype.slice.call(arguments).every(Boolean);
},
or: function () {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);
}
});
var siteConfig = parseJsonFromFile(_path.join(_contentRootPath, "site.json"));
var globalConfig = {
menu: [],
socials: siteConfig.socials,
footerMenu: siteConfig.footerMenu,
downloadLink: siteConfig.downloadLink
};
_shared.variables = siteConfig.variables;
//load settings for home page
siteConfig.home = getPageConfig(_contentRootPath, siteConfig.home);
//load settings for all children
siteConfig.menu.forEach(function (menuItem) {
var pageConfig = getPageConfig(_contentRootPath, menuItem);
if (pageConfig.children) {
for (var i = 0; i < pageConfig.children.length; i++) {
pageConfig.children[i] = getPageConfig(_path.join(_contentRootPath, menuItem.root), pageConfig.children[i]);
}
}
if (pageConfig.visible) {
console.log(pageConfig.absoluteRoot)
globalConfig.menu.push(pageConfig);
}
});
//clear output dir
//_fs.removeSync(_path.join(_path.resolve(), _outputRootPath));
//copy global assets to build directory
_fs.copySync("./src/assets", _path.join(_path.resolve(), _outputRootPath, "assets"));
//render home page
renderPage(siteConfig.home, globalConfig);
//render all other pages from the menue
siteConfig.menu.forEach(function (menuItem) {
if (menuItem.menuUrl)
return;
renderPage(menuItem, globalConfig);
if (menuItem.children) {
for (var i = 0; i < menuItem.children.length; i++) {
if (!menuItem.children[i].menuUrl)
renderPage(menuItem.children[i], globalConfig);
}
}
});
done();
});
_gulp.task('server', function () {
_connect.server({
root: _outputRootPath,
port: 8080,
host: '0.0.0.0',
keepalive: true,
livereload: true
});
});
_gulp.task('reload', function () {
_connect.reload();
});
_gulp.task('watch-and-reload', function (done) {
_gulp.watch(['src/content/**/*.json', 'src/templates/**/*.html', 'src/assets/**/*.js', 'src/assets/**/*.css'], function () {
}).on('change', function (file) {
_gulp.series('build', 'reload')();
});
done();
});
_gulp.task('run', _gulp.series('build', 'watch-and-reload', 'server'));
//--------------- End ----------------