-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (48 loc) · 1.53 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
'use strict'
const fs = require('fs')
const ejs = require('ejs')
const transformer = {
name: 'ejs',
outputFormat: 'html',
}
transformer.compile = function (source, options) {
return ejs.compile(source, { ...options, async: false, client: false })
}
transformer.compileClient = function (source, options) {
return ejs.compile(source, { ...options, async: false, client: true }).toString()
}
transformer.compileFile = function (path, options) {
options = options || {}
options.filename = options.filename || path
return transformer.compile(fs.readFileSync(path, 'utf8'), options)
}
transformer.compileFileAsync = function (path, options) {
options = options || {}
options.filename = options.filename || path
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (error, source) => {
if (error) {
reject(error)
}
resolve(transformer.compile(source, options))
})
})
}
transformer.render = function (source, options, locals) {
return ejs.render(source, locals, { ...options, async: false })
}
transformer.renderFile = function (path, options, locals = null) {
options.filename = options.filename || path
return ejs.render(fs.readFileSync(path, 'utf8'), locals, { ...options, async: false })
}
transformer.renderFileAsync = function (path, options, locals = null) {
return new Promise((resolve, reject) => {
ejs.renderFile(path, locals, options, (error, result) => {
if (error) {
reject(error)
}
resolve(result)
})
})
}
module.exports = transformer