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

Commit 71b85ef

Browse files
authored
Merge pull request #78 from thebarty/testingsupport
Make package work in unit and app test modes.
2 parents 3371f90 + 764dc2f commit 71b85ef

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

lib/mailer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const factory = (options) => {
187187
// `preview` prop in the data context to apply to the layout.
188188
if (tmpl.__helpers.has('preview')) {
189189
preview = tmpl.__helpers.get('preview');
190-
} else if (data.preview) {
190+
} else if (data && data.preview) { // data is optional
191191
preview = data.preview;
192192
}
193193

lib/utils.js

+52-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ const sass = (function() {
2222
return null;
2323
})();
2424

25+
/**
26+
* Check if file exists
27+
* @param {String} filePath
28+
* @return {Boolean}
29+
*/
30+
const fileExists = function (filePath) {
31+
try {
32+
return fs.statSync(filePath).isFile();
33+
}
34+
catch (err)
35+
{
36+
return false;
37+
}
38+
}
39+
2540
const TAG = 'mailer-utils';
2641

2742
// This package assumes that assets (templates, SCSS, CSS ..) are
@@ -37,7 +52,6 @@ const TAG = 'mailer-utils';
3752
// /var/www/app/bundle
3853
//
3954
// For Modulus, you need to use the `APP_DIR` variable, which you do NOT need to set.
40-
4155
const developmentPrivateDir = () => {
4256
if (!isDevEnv) {
4357
return '';
@@ -154,7 +168,21 @@ Utils = {
154168
},
155169

156170
readFile(relativePathFromApp) {
157-
const file = Path.join(ROOT, relativePathFromApp);
171+
let file
172+
if (Meteor.isAppTest
173+
|| Meteor.isTest) {
174+
// note:
175+
// * this DOES WORK with Meteor.isTest (=unit test mode)
176+
// * Meteor.isAppTest is NOT TESTED yet (=in-app test mode)
177+
//
178+
// background-info: we had NO luck with "Assets.absoluteFilePath(relativePathFromApp)",
179+
// so lets build the path ourselves.
180+
// see discussion https://github.com/lookback/meteor-emails/issues/76
181+
file = Path.join(process.cwd(), 'assets', 'app', relativePathFromApp);
182+
} else {
183+
// = standard mode (development || production)
184+
file = Path.join(ROOT, relativePathFromApp);
185+
}
158186

159187
try {
160188
return fs.readFileSync(file, {
@@ -178,17 +206,29 @@ ${packageToRecommend}`, TAG);
178206
return Utils.readFile(scss);
179207
}
180208

181-
const file = Path.join(ROOT, scss);
209+
let file
210+
if (Meteor.isAppTest
211+
|| Meteor.isTest) {
212+
// note:
213+
// * this DOES WORK with Meteor.isTest (=unit test mode)
214+
// * Meteor.isAppTest is NOT TESTED yet (=in-app test mode)
215+
file = Path.join(process.cwd(), 'assets', 'app', scss);
216+
} else {
217+
// = standard mode (development || production)
218+
file = Path.join(ROOT, scss);
219+
}
182220

183-
try {
184-
return sass.renderSync({
185-
file: file,
186-
sourceMap: false
187-
}).css.toString();
188-
} catch (ex) {
189-
console.error(`Sass failed to compile: ${ex.message}`);
190-
console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`);
191-
return '';
221+
if (fileExists(file)) {
222+
try {
223+
return sass.renderSync({
224+
file: file,
225+
sourceMap: false
226+
}).css.toString();
227+
} catch (ex) {
228+
console.error(`Sass failed to compile: ${ex.message}`);
229+
console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`);
230+
}
192231
}
232+
return ''; // fallback: on error, or if file does NOT exist
193233
}
194234
};

0 commit comments

Comments
 (0)