From 3cfe1183c514b40b559b758ced4b07b8dabb31e8 Mon Sep 17 00:00:00 2001 From: Nitin Jain Date: Thu, 30 Dec 2021 09:49:17 +0530 Subject: [PATCH] Loading multiple template directory --- lib/emailjs.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/emailjs.js b/lib/emailjs.js index e3059a1..483abf0 100644 --- a/lib/emailjs.js +++ b/lib/emailjs.js @@ -20,6 +20,7 @@ exportable = (function () { config = null, transport = null, templates = null, + loadedTemplateDirs = [], ruleSettings = [], defaultFrom = null, retryInterval = null, @@ -35,13 +36,28 @@ exportable = (function () { instance = new email(); function init (_templatesDir, _config, _rules, cb) { - if (initialized) { - return handleExcp(cb, new Error('Already initialized')); - } templatesDir = _templatesDir; config = _config; rules = _rules; + if (initialized) { + if (loadedTemplateDirs.includes(templatesDir)) { + return handleExcp(cb, new Error('Already initialized')); + } else { + return Q.fcall(validateInitialization) + .then(loadTemplates) + .then(function () { + if (cb) { + cb(null, {}); + } + }) + .catch(function (err) { + handleExcp(cb, err); + }); + } + + } + Q.fcall(validateInitialization) .then(loadTemplates) .then(intializeMailService) @@ -107,7 +123,7 @@ exportable = (function () { function loadTemplates () { var deferred = Q.defer(); - templates = {}; + templates = templates || {}; fs.readdir(templatesDir, function (err, dirs) { if (err) { @@ -125,7 +141,7 @@ exportable = (function () { logWarn('Ignoring: '+dirPath); } }); - + loadedTemplateDirs.push(templatesDir); deferred.resolve(); } });