-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (37 loc) · 1.44 KB
/
index.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
'use strict';
const through = require('through2');
const PluginError = require('plugin-error');
const utils = {
htl: require('./utils/htl.js'),
helpers: require('./utils/helpers.js')
};
module.exports = function (options) {
options = options || {};
return through.obj(function (file, enc, cb) {
const _this = this;
if (file.isNull()) {
cb(null, file);
return;
}
if (file.path.indexOf('.html') <= 0 || file.path.indexOf('.spec.html') >= 1) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-htl', 'Streaming not supported'));
return;
}
const split = file.path.split("/");
const base = `${split.slice(0, split.length - 1).join("/")}/`;
process.chdir(`${base}htlmock/`);
const mockData = utils.htl.buildMockDataObject(`${base}htlmock/mock.json`);
const templateFile = utils.htl.fixSlyInclude(file.path, base, mockData);
const result = new utils.htl.compiler().includeRuntime(true).withRuntimeVar(Object.keys(mockData)).compileToString(templateFile);
Promise.resolve(result).then(function (result) {
utils.htl.render.call(_this, base, result, mockData, file, cb);
}).catch(function () {
console.error(arguments);
cb(new PluginError('gulp-htl', 'get result failed'));
});
});
};