155 lines
5.2 KiB
JavaScript
155 lines
5.2 KiB
JavaScript
var fs = require('fs');
|
|
var mustache = require('mustache');
|
|
var locale = require('./locale.js');
|
|
var path = require('path');
|
|
var inlineCss = require('inline-css');
|
|
|
|
module.exports = {
|
|
/**
|
|
* Get template.
|
|
* @param {String} template - Template name
|
|
* @param {Object} countryCode - Language code
|
|
* @param {Object} params - Params
|
|
* @param {Object} cb - Callback
|
|
*/
|
|
get: function(template, params, cb) {
|
|
var templatePath = path.join(__dirname, 'template', `${template}`, `index.html`);
|
|
var classPath = path.join(__dirname, 'template', `${template}`, `${template}.js`);
|
|
var stylePath = path.join(__dirname, 'template', `default`, `style.css`);
|
|
|
|
fs.stat(templatePath, (error, stat) => {
|
|
if (error)
|
|
return cb(new Error('Template ' + template + ' not found'));
|
|
|
|
let TemplateClass = require(classPath);
|
|
let instance = new TemplateClass();
|
|
|
|
let getRenderedStyles = body => {
|
|
this.renderStyles(stylePath, body, (error, body) => {
|
|
params.subject = params.subject || instance.subject;
|
|
|
|
if (params.subject == undefined)
|
|
params.subject = body.match(new RegExp('<title>(.*?)</title>', 'i'))[1];
|
|
|
|
this.getAttachments(template, body, (error, attachments) => {
|
|
if (error)
|
|
return cb(error);
|
|
|
|
cb(null, {recipient: instance.recipient, subject: params.subject, body: body, attachments: attachments});
|
|
});
|
|
});
|
|
};
|
|
|
|
let getDataCb = () => {
|
|
this.render(templatePath, instance, (error, result) => getRenderedStyles(result));
|
|
};
|
|
|
|
instance.getData(params, (error, result) => {
|
|
if (error)
|
|
return cb(error);
|
|
|
|
locale.load(template, instance.countryCode, (error, result) => {
|
|
if (error)
|
|
return cb(error);
|
|
|
|
instance._ = result.locale;
|
|
getDataCb(null, result);
|
|
});
|
|
});
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Render template
|
|
* @param {String} path - Template path
|
|
* @param {Object} data - Params
|
|
* @param {Object} cb - Callback
|
|
*/
|
|
render: function(path, data, cb) {
|
|
fs.readFile(path, 'utf8', function(error, body) {
|
|
mustache.parse(body);
|
|
cb(null, mustache.render(body, data));
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Render template style.
|
|
* @param {String} path - Stylesheet path
|
|
* @param {String} body - Rendered html
|
|
* @param {Object} cb - Callback
|
|
*/
|
|
renderStyles: function(path, html, cb) {
|
|
fs.stat(path, error => {
|
|
if (error) return cb(new Error('Template stylesheet not found'));
|
|
fs.readFile(path, 'utf8', (error, css) => {
|
|
let style = '<style>' + css + '</style>';
|
|
let body = style + html;
|
|
let options = {url: ' '};
|
|
|
|
inlineCss(body, options)
|
|
.then(function(body) {
|
|
cb(null, body);
|
|
});
|
|
});
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Get template attachments
|
|
* @param {String} template - Template name
|
|
* @param {String} body - template body
|
|
* @param {Object} cb - Callback
|
|
*/
|
|
getAttachments: function(template, body, cb) {
|
|
let attachments = [];
|
|
let tplAttachments = body.match(new RegExp('src="cid:(.*?)"', 'ig'));
|
|
|
|
// Template default attachments
|
|
for (var i = 0; i < tplAttachments.length; i++) {
|
|
let name = tplAttachments[i].replace('src="cid:', '').replace('"', '');
|
|
let attachmentPath = path.join(__dirname, 'template/default/image', name);
|
|
|
|
attachments.push({filename: name, path: attachmentPath, cid: name});
|
|
}
|
|
|
|
// Template attachment files
|
|
let attachmentsPath = path.join(__dirname, 'template', `${template}`, 'attachment.json');
|
|
|
|
fs.stat(attachmentsPath, (error, stats) => {
|
|
if (error)
|
|
return cb(new Error(`Could not load attachments.js from template ${template}`));
|
|
|
|
let attachObj = require(attachmentsPath);
|
|
|
|
for (var i = 0; i < attachObj.length; i++) {
|
|
let filename = attachObj[i];
|
|
let attachmentPath = path.join(__dirname, 'template', `${template}`, 'attachment', filename);
|
|
|
|
attachments.push({filename: filename, path: attachmentPath, cid: filename});
|
|
}
|
|
|
|
this.checkAttachments(attachments, error => {
|
|
if (error)
|
|
return cb(error);
|
|
cb(null, attachments);
|
|
});
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Check all template attachments
|
|
* @param {Object} attachments - Attachments object
|
|
* @param {Object} cb - Callback
|
|
*/
|
|
checkAttachments: function(attachments, cb) {
|
|
for (var i = 0; i < attachments.length; i++) {
|
|
var attachment = attachments[i];
|
|
fs.stat(attachment.path, error => {
|
|
if (error)
|
|
return cb(new Error(`Could not load attachment file ${attachment.path}`));
|
|
});
|
|
}
|
|
cb();
|
|
}
|
|
};
|