-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
267 lines (242 loc) · 9.72 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
let email = require("emailjs/email");
/**
* Mailer to aggregate error or log mails. It's a singleton. Create it once by `logmailer.create(..)`, use it then simply like `logmailer.add(..)` or `logmailer.send(..)`
*
* Instructions see https://www.npmjs.com/package/logmailer
*
*/
class Mailer {
constructor() {
if (!Mailer.instance) {
this.appName = `App-${process.pid}`;
this.mailAlias = `logmailer-${process.pid}`
this.mailClient = null;
this.recipients = [];
this.chapters = {};
}
return Mailer.instance;
}
/**
* Create the logmailer instance once
* @param {Object} options
* @param {string} [options.appName] (optional, default is App-#pid) meaningful app name is used in mail subject (e.g. appName [Success]: YourSubject)
* @param {string} [options.mailAlias] (optional, default is logmailer-#pid) your mail alias you want to send from (e.g. "fictive-sender[at]fictive-domain.com")
* @param {Object} options.client mail client configuration
* @param {string} options.client.host email host, e.g. smtp.googlemail.com
* @param {string} options.client.user email user
* @param {string} options.client.password email password
* @param {boolean} options.client.ssl use ssl
* @param {string[]|Recipient[]} options.recipients Array, define the recipients via email string or recipient object
* @param {Object} options.chapters use the StandardChapters or define your own
* @param {Chapter} [options.chapters.customChapter] just an example
*/
create(options) {
this.appName = options.appName;
this.mailAlias = options.mailAlias;
let mailClient = email.server.connect({
host: options.client.host,
user: options.client.user,
password: options.client.password,
ssl: options.client.ssl
});
this.mailClient = mailClient;
this.recipients = options.recipients;
this.chapters = options.chapters;
}
/**
* Send mail finally
* @param {function(Error): void} callback returns an error if there was one, otherwise returns null
*/
sendMail(callback) {
let recipientHTMLs = [];
this.recipients.forEach(recipient => {
let recipientHTML = {
emailAddress: "",
sendEmail: false,
html: "",
subject: `${this.appName}`
}
if (recipient.hasOwnProperty("emailAddress")) {
recipientHTML.emailAddress = recipient.emailAddress; // recipient is a Recipient object
} else {
recipientHTML.emailAddress = recipient; // recipient is an email string only
}
if (recipient.getsEmailOnlyIfChaptersNotEmpty && Array.isArray(recipient.getsEmailOnlyIfChaptersNotEmpty)) {
recipient.getsEmailOnlyIfChaptersNotEmpty.forEach(chapter => {
if (chapter.html) {
recipientHTML.sendEmail = true;
}
})
} else {
recipientHTML.sendEmail = true;
}
let chs = {};
if (recipient.canOnlySeeChapters && Array.isArray(recipient.canOnlySeeChapters)) {
chs = recipient.canOnlySeeChapters;
} else {
chs = this.chapters;
}
Object.values(chs).forEach(chapter => {
if (chapter.html) {
if (chapter.hasOwnProperty("count")) {
if (chapter.count > 0) {
recipientHTML.subject += ` | [${chapter.name}]: ${chapter.count}`;
recipientHTML.html += `<h2><font color="${chapter.color}">${chapter.name}: ${chapter.count}</font></h2>`;
}
} else {
recipientHTML.html += `<h2><font color="${chapter.color}">${chapter.name}</font></h2>`;
}
recipientHTML.html += chapter.html;
}
})
recipientHTMLs.push(recipientHTML);
})
this._mailSender(recipientHTMLs, null, errors => {
callback(errors);
})
}
/**
* @private
*/
_mailSender(recipientHTMLs, errors, callback) {
if (!errors) {
errors = {};
}
if (recipientHTMLs.length > 0) {
let recipientHTML = recipientHTMLs[0];
if (recipientHTML.sendEmail) {
let message = {
text: "Please use an email client, which is able to display HTML!",
subject: recipientHTML.subject,
from: this.mailAlias,
to: recipientHTML.emailAddress,
attachment: [
{ data: `<html><h2>${recipientHTML.subject}</h2><div>${recipientHTML.html}</div></html>`, alternative: true }
]
}
this.mailClient.send(message, (err, message) => {
if (err) {
errors[recipientHTML.emailAddress] = err;
}
recipientHTMLs.shift();
this._mailSender(recipientHTMLs, errors, callback);
})
} else {
recipientHTMLs.shift();
this._mailSender(recipientHTMLs, errors, callback);
}
} else {
if (Object.keys(errors).length > 0) {
callback(errors);
} else {
callback();
}
}
}
/**
* Convert array of JS Objects to HTML Table
* @param {*} array Array of Objects
*/
convertObjectArrayToHTMLTable(array) {
if (Array.isArray(array)) {
let html = "";
for (let i = 0; i < array.length; i++) {
html += this.convertObjectToHTMLTable(array[i]);
}
return html;
}
}
/**
* Convert JS Object to HTML Table
* @param {Object} obj Object
*/
convertObjectToHTMLTable(obj) {
if (obj && typeof (obj) === 'object') {
let keys = Object.keys(obj);
if (keys.length > 0) {
// https://stackoverflow.com/questions/17684201/create-html-table-from-javascript-object
let html = '<table style="border: 1px solid #ddd; border-collapse: collapse;">';
for (let i = 0; i < keys.length; i++) {
let item = obj[keys[i]];
let value = (typeof (item) === 'object') ? this.convertObjectToHTMLTable(item) : item.toString();
html += '<tr style="border: 1px solid grey;">';
html += '<td style="border: 1px solid grey; padding: 5px;">' + keys[i] + '</td>';
html += '<td style="border: 1px solid grey; padding: 5px;">' + value + '</td>';
html += '</tr>';
}
html += '</table>';
return html;
} else {
return "";
}
} else {
return String(obj);
}
}
}
class Recipient {
/**
* A single recipient object
* @constructor
* @param {string} emailAddress
* @param {Chapter[]} [getsEmailOnlyIfChaptersNotEmpty] (optional) array of chapters e.g. [Error], the recipient will get the email only if there is at least 1 logged error
* @param {Chapter[]} [canOnlySeeChapters] (optional) array of chapters e.g. [Summary,Error], the recipient can only see the summary and the logged errors
*/
constructor(emailAddress, getsEmailOnlyIfChaptersNotEmpty, canOnlySeeChapters) {
this.emailAddress = emailAddress;
this.getsEmailOnlyIfChaptersNotEmpty = getsEmailOnlyIfChaptersNotEmpty;
this.canOnlySeeChapters = canOnlySeeChapters;
}
}
class Chapter {
/**
* A single chapter object
* @param {string} name chapters name e.g. "Summary"
* @param {boolean} [hasCount=false] (optional, default is false) set to true if you want to count how often you added content to the chapter (good for errors or warnings)
* @param {string} [color] (optional, default is "black") use colors to colorize headlines (you can use hex, rgb, rgba, color codes etc. but it is **important** that the email client can display the color correctly)
*/
constructor(name, hasCount, color) {
this.name = name;
this.html = "";
this.color = color;
if (hasCount) {
this.count = 0;
}
if (color) {
this.color = color;
} else {
this.color = "black";
}
/**
* Add content to chapter
* @param {String} headline can also be null
* @param {String} contentHTML
*/
this.add = function(headline, contentHTML){
this.html += `${headline ? `<h4><font color="${this.color}">${headline}</font></h4>` : "<br/>"}<span>${contentHTML}</span>`;
if (this.hasOwnProperty("count") && headline) {
this.count++;
}
}
/**
* Reset specific chapter
*/
this.reset = function(){
this.html = "";
if (this.hasOwnProperty("count")) {
this.count = 0;
}
}
}
}
let StandardChapters = {
Summary: new Chapter("Summary", false),
Errors: new Chapter("Errors", true, "red"),
Warnings: new Chapter("Warnings", true, "orange"),
Logs: new Chapter("Logs", false, "limegreen")
};
let instance = new Mailer();
module.exports.logmailer = instance;
module.exports.Recipient = Recipient;
module.exports.Chapter = Chapter;
module.exports.StandardChapters = StandardChapters;