-
Notifications
You must be signed in to change notification settings - Fork 339
/
email-template.js
149 lines (130 loc) · 3.74 KB
/
email-template.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
import P from 'bluebird'
import Debug from 'debug'
import {basename} from 'path'
import juice from 'juice'
import {isFunction} from 'lodash'
import {ensureDirectory, readContents, renderFile} from './util'
const debug = Debug('email-templates:email-template')
export default class EmailTemplate {
constructor (path, options = {}) {
this.files = {}
this.path = path
this.dirname = basename(path)
this.options = options
debug('Creating Email template for path %s', basename(path))
}
_init () {
if (this.isInited) return P.resolve()
debug('Initializing templates')
return ensureDirectory(this.path)
.then(() => this._loadTemplates())
.then(() => {
this.isInited = true
debug('Finished initializing templates')
})
}
_loadTemplates () {
return P.map(['html', 'text', 'style', 'subject'], (type) => {
return readContents(this.path, type)
})
.then((files) => {
let [html, text, style, subject] = files
if (!html && !text) {
let err = new Error(`Neither html nor text template files found or are both empty in path ${this.dirname}`)
err.code = 'ENOENT'
throw err
}
if (html) {
debug('Found HTML file %s in %s', basename(html.filename), this.dirname)
}
this.files.html = html
if (text) {
debug('Found text %s file in %s', basename(text.filename), this.dirname)
}
this.files.text = text
if (style) {
debug('Found stylesheet %s in %s', basename(style.filename), this.dirname)
}
this.files.style = style
if (subject) {
debug('Found subject %s in %s', basename(subject.filename), this.dirname)
}
this.files.subject = subject
debug('Finished loading template')
})
}
renderText (locals, callback) {
debug('Rendering text')
return this._init()
.then(() => {
if (!this.files.text) return null
return renderFile(this.files.text, locals)
})
.tap(() => debug('Finished rendering text'))
.nodeify(callback)
}
renderSubject (locals, callback) {
debug('Rendering subject')
return this._init()
.then(() => {
if (!this.files.subject) return null
return renderFile(this.files.subject, locals)
})
.tap(() => debug('Finished rendering subject'))
.nodeify(callback)
}
renderHtml (locals, callback) {
debug('Rendering HTML')
return this._init()
.then(() => {
return P.all([
renderFile(this.files.html, locals),
this._renderStyle(locals)
])
})
.then((results) => {
let [html, style] = results
if (!style) return html
if (this.options.juiceOptions) {
debug('Using juice options ', this.options.juiceOptions)
}
return juice.inlineContent(html, style, this.options.juiceOptions || {})
})
.tap(() => debug('Finished rendering HTML'))
.nodeify(callback)
}
render (locals, callback) {
if (isFunction(locals)) {
callback = locals
locals = {}
}
debug('Rendering template with locals %j', locals)
return P.all([
this.renderHtml(locals),
this.renderText(locals),
this.renderSubject(locals)
])
.then((rendered) => {
let [html, text, subject] = rendered
return {
html, text, subject
}
})
.nodeify(callback)
}
_renderStyle (locals) {
return new P((resolve) => {
// cached
if (this.style !== undefined) return resolve(this.style)
// no style
if (!this.files.style) return resolve(null)
debug('Rendering stylesheet')
renderFile(this.files.style, locals)
.then((style) => {
this.style = style
debug('Finished rendering stylesheet')
resolve(style)
})
})
}
}