-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (147 loc) · 4.52 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
169
const fs = require('fs')
const path = require('path')
const ENGINE = {
config: {
paths: {
doc: 'Paths required by Mustache',
format: Object,
default: {
helpers: 'workspace/utils/helpers'
}
}
},
extensions: ['.mustache'],
handle: 'mustache'
}
module.exports = () => {
const debug = require('debug')('web:templates:mustache')
const libHelpers = require(path.join(__dirname, 'lib/helpers'))
const mustache = require('mustache')
const requireDir = require('require-dir')
const EngineMustache = function (options) {
debug('Starting Mustache.js engine...')
// additionalTemplates is passed by DADI Web: it is an array of absolute
// paths to any templates found with an extension supported by this engine
// that haven't already been loaded due to not having a JSON schema
// file (i.e. they are not pages)
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.
*/
EngineMustache.prototype.getCore = function () {
return mustache
}
/**
* Returns information about the engine.
*
* @return {object} An object containing the engine name and version.
*/
EngineMustache.prototype.getInfo = function () {
return {
engine: ENGINE.handle,
version: undefined
}
}
/**
* Initialises the engine.
*
* @return {Promise} A Promise that resolves when the engine is fully loaded.
*/
EngineMustache.prototype.initialise = function () {
const paths = this.config.get('engines.mustache.paths')
const helpersPath = path.resolve(paths.helpers)
if (paths && paths.helpers) {
const helperPath = path.join(process.cwd(), paths.helpers)
this.helperFunctions = requireDir(helperPath, { recurse: true, camelcase: true })
}
return this._requireDirectory(helpersPath).then(helpers => {
debug('helpers loaded %o', helpers)
return this._loadPartials()
}).then(partials => {
debug('partials loaded %o', partials)
debug('Mustache initialised')
})
}
/**
* Registers the template with markup.
*
* @return {Promise} A Promise that resolves with the loaded data.
*/
EngineMustache.prototype.register = function (name, data, path) {
this.templates[name] = data
}
/**
* Registers the template with markup.
*
* @return {Promise} A Promise that resolves with the loaded data.
*/
EngineMustache.prototype.registerPartial = function (name, data, path) {
this.partials = this.partials || {}
this.partials[name] = 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.
*/
EngineMustache.prototype.render = function (name, data, locals, options) {
if (this.helperFunctions) {
Object.assign(locals, this.helperFunctions)
}
return Promise.resolve(mustache.render(this.templates[name], locals, this.partials))
}
/**
* Loads any additional templates.
*
* @return {Promise} The names of the partials loaded.
*/
EngineMustache.prototype._loadPartials = function () {
return libHelpers.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)
this.registerPartial(templateName, data)
resolve(templateName)
})
})
}
})
}
/**
* Requires all JS files within a directory.
*
* @param {string} directory The full path to the directory.
*/
EngineMustache.prototype._requireDirectory = function (directory) {
if (!directory) {
return Promise.resolve([])
}
return libHelpers.readDirectory(directory, {
extensions: ['.js'],
recursive: true
}).then(files => {
files.forEach(file => {
require(path.resolve(file))
})
return files
})
}
return EngineMustache
}
module.exports.metadata = ENGINE