Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Fix to make it work in unit-test mode #78

Merged
merged 3 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
64 changes: 52 additions & 12 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 '';
Expand Down Expand Up @@ -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, {
Expand All @@ -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
}
};