diff --git a/lib/mailer.js b/lib/mailer.js index 07e3802..ec6d8ec 100644 --- a/lib/mailer.js +++ b/lib/mailer.js @@ -187,7 +187,7 @@ const factory = (options) => { // `preview` prop in the data context to apply to the layout. if (tmpl.__helpers.has('preview')) { preview = tmpl.__helpers.get('preview'); - } else if (data.preview) { + } else if (data && data.preview) { // data is optional preview = data.preview; } diff --git a/lib/utils.js b/lib/utils.js index 71fdd8f..b0bf80d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -22,6 +22,21 @@ const sass = (function() { return null; })(); +/** + * Check if file exists + * @param {String} filePath + * @return {Boolean} + */ +const fileExists = function (filePath) { + try { + return fs.statSync(filePath).isFile(); + } + catch (err) + { + return false; + } +} + const TAG = 'mailer-utils'; // This package assumes that assets (templates, SCSS, CSS ..) are @@ -37,7 +52,6 @@ const TAG = 'mailer-utils'; // /var/www/app/bundle // // For Modulus, you need to use the `APP_DIR` variable, which you do NOT need to set. - const developmentPrivateDir = () => { if (!isDevEnv) { return ''; @@ -154,7 +168,21 @@ Utils = { }, readFile(relativePathFromApp) { - const file = Path.join(ROOT, relativePathFromApp); + let file + if (Meteor.isAppTest + || Meteor.isTest) { + // note: + // * this DOES WORK with Meteor.isTest (=unit test mode) + // * Meteor.isAppTest is NOT TESTED yet (=in-app test mode) + // + // background-info: we had NO luck with "Assets.absoluteFilePath(relativePathFromApp)", + // so lets build the path ourselves. + // see discussion https://github.com/lookback/meteor-emails/issues/76 + file = Path.join(process.cwd(), 'assets', 'app', relativePathFromApp); + } else { + // = standard mode (development || production) + file = Path.join(ROOT, relativePathFromApp); + } try { return fs.readFileSync(file, { @@ -178,17 +206,29 @@ ${packageToRecommend}`, TAG); return Utils.readFile(scss); } - const file = Path.join(ROOT, scss); + let file + if (Meteor.isAppTest + || Meteor.isTest) { + // note: + // * this DOES WORK with Meteor.isTest (=unit test mode) + // * Meteor.isAppTest is NOT TESTED yet (=in-app test mode) + file = Path.join(process.cwd(), 'assets', 'app', scss); + } else { + // = standard mode (development || production) + file = Path.join(ROOT, scss); + } - try { - return sass.renderSync({ - file: file, - sourceMap: false - }).css.toString(); - } catch (ex) { - console.error(`Sass failed to compile: ${ex.message}`); - console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`); - return ''; + if (fileExists(file)) { + try { + return sass.renderSync({ + file: file, + sourceMap: false + }).css.toString(); + } catch (ex) { + console.error(`Sass failed to compile: ${ex.message}`); + console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`); + } } + return ''; // fallback: on error, or if file does NOT exist } };