-
Notifications
You must be signed in to change notification settings - Fork 21
/
es6-renderer.js
executable file
·94 lines (93 loc) · 3.38 KB
/
es6-renderer.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
const fs = require('fs'); // this engine requires the fs module
/* jshint ignore:start */
const compile = (content, $ = '$') => Function($, 'return `' + content + '`;');
const precompile = (content, $ = '$') =>
Function($, 'try { return `' + content + '`;} catch(err) { return err }');
/* jshint ignore:end */
const setPath = (views, ref, ext) => ref.endsWith(ext) ? ref : views + '/' + ref + ext;
const getPartial = (path, cb = 'resolveNeutral') => {
const findFile = function(resolve, reject) {
this.resolveNeutral = (err, content) => err ? reject(err) : resolve(content);
this.resolvePositive = (err, content) => resolve(err || content);
fs.readFile(path, 'utf-8', this[cb]);
};
return new Promise(findFile);
};
module.exports = (path, options, render) => {
if(options === undefined || typeof options === 'string') {
return precompile(path, options);
}
let willResolve;
let willReject;
const fulfillPromise = (resolve, reject) => {
willResolve = resolve;
willReject = reject;
};
const handleRejection = (err) => {
const output = render(err);
return willReject ? willReject(err) : output;
};
const {locals = {}, partials = {}, settings, template} = options;
const assign = (err, content) => {
const send = () => {
if(render) {
try {
const compiled = compile(content, localsKeys)(...localsValues);
const output = render(null, compiled);
return willResolve ? willResolve(compiled) : output;
} catch(err) {
return handleRejection(err);
}
}
try {
return willResolve(compile(content, localsKeys)(...localsValues));
} catch (err) {
return willReject(err);
}
}
if(err) {
return handleRejection(err);
}
const localsKeys = Object.keys(locals);
const localsValues = localsKeys.map(i => locals[i]);
const partialsKeys = Object.keys(partials);
const compilePartials = values => {
const valTempList = localsValues.concat(values);
try {
localsValues.push(...values.map(i => compile(i, localsKeys)(...valTempList)));
} catch (err) {
return render(err);
}
send();
};
if(partialsKeys.length) {
const applySettings = () => {
const ext = '.' + settings['view engine'];
if(typeof settings.views === 'string') {
return i => getPartial(setPath(settings.views, partials[i], ext));
}
return i => {
const getFile = view => getPartial(setPath(view, partials[i], ext), 'resolvePositive');
const getFirst = value => typeof value === 'string';
const searchFile = (resolve, reject) => {
const getContent = values => resolve(values.find(getFirst));
Promise.all(settings.views.map(getFile)).then(getContent);
};
return new Promise(searchFile);
};
};
const setPartial = settings ? applySettings() : i => getPartial(partials[i]);
localsKeys.push(...partialsKeys);
const willGetPartials = Promise.all(partialsKeys.map(setPartial))
.then(compilePartials, handleRejection);
return willResolve ? willGetPartials : new Promise(fulfillPromise);
}
return send();
};
if(template) {
render = render || ((err, content) => err || content);
return assign(null, path);
}
fs.readFile(path, 'utf-8', assign);
return new Promise(fulfillPromise);
};