-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·168 lines (144 loc) · 4.15 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
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
'use strict'
const ENGINE = {
config: {
paths: {
doc: 'Paths required by ES6 templates',
format: Object,
default: {
helpers: 'workspace/utils/helpers'
}
}
},
extensions: ['.js'],
handle: 'es6'
}
module.exports = () => {
const fs = require('fs')
const path = require('path')
const helpers = require(path.join(__dirname, 'lib/helpers'))
const debug = require('debug')('web:templates:es6')
const es6 = require('es6-template-strings')
const compile = require('es6-template-strings/compile')
const resolveToString = require('es6-template-strings/resolve-to-string')
const EngineES6 = function (options) {
debug('Starting ES6 templates engine...')
this.additionalTemplates = options.additionalTemplates
this.config = options.config
this.helpers = options.helpers
this.pagesPath = options.pagesPath
this.templates = {}
}
/**
* Returns the engine core module.
*
* @return {function} The engine core module.
*/
EngineES6.prototype.getCore = function () {
return es6
}
/**
* Returns information about the engine.
*
* @return {object} An object containing the engine name and version.
*/
EngineES6.prototype.getInfo = function () {
return {
engine: ENGINE.handle
}
}
/**
* Requires all JS files within a directory.
*
* @param {string} directory The full path to the directory.
*/
EngineES6.prototype._requireDirectory = function (directory) {
return helpers
.readDirectory(directory, {
extensions: ['.js'],
recursive: true
})
.then(files => {
files.forEach(file => {
require(path.resolve(file))
})
return files
})
}
/**
* Loads any additional templates.
*
* @return {Promise} The names of the partials loaded.
*/
EngineES6.prototype._loadPartials = function () {
return helpers.readFiles(this.additionalTemplates, {
callback: file => {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) return reject(err)
const extension = path.extname(file)
const templateName = path.relative(this.pagesPath, file)
.slice(0, -extension.length).replace(/\//gmi, '_')
this.register(templateName, data)
resolve(templateName)
})
})
}
})
}
/**
* Initialises the engine.
*
* @return {Promise} A Promise that resolves when the engine is fully loaded.
*/
EngineES6.prototype.initialise = function () {
const paths = this.config.get('engines.es6.paths')
const helpersPath = path.resolve(paths.helpers)
return this._requireDirectory(helpersPath)
.then(helpers => {
debug('helpers loaded %o', helpers)
return this._loadPartials()
})
.then(partials => {
debug('partials loaded %o', partials)
})
debug('ES6 templates initialised')
}
/**
* Registers the template with markup.
*
* @return {Promise} A Promise that resolves with the loaded data.
*/
EngineES6.prototype.register = function (name, data) {
this.templates[name] = compile(data)
}
/**
* Renders a template.
*
* @param {string} name The name of the template.
* @param {string} data The template content.
* @param {object} locals The variables to add to the context.
* @param {object} options Additional render options.
*
* @return {Promise} A Promise that resolves with the render result.
*/
EngineES6.prototype.render = function (name, data, locals, options) {
// Look for and resolve partials
this.templates[name].substitutions.map(i => {
if (i in this.templates) locals[i] = resolveToString(this.templates[i], locals)
})
return new Promise((resolve, reject) => {
try {
resolve(resolveToString(this.templates[name], locals))
}
catch (err) {
return reject({
name: 'web-es6-templates',
message: 'Error rendering template: ' + name,
stack: err
})
}
})
}
return EngineES6
}
module.exports.metadata = ENGINE