Skip to content

Commit 5c871ee

Browse files
committed
adding template packageFolderName param to allow the use of Mailer inside another package. Also changed the overwriting of Mailer.init function that prevented several init calls, necessary if you use it in a package and in the app
1 parent aed4b7a commit 5c871ee

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Diff for: lib/mailer.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const factory = (options) => {
107107
check(template, Match.ObjectIncluding({
108108
path: String,
109109
name: String,
110+
packageFolderName: Match.Optional(String),
110111
scss: Match.Optional(String),
111112
css: Match.Optional(String),
112113
layout: Match.Optional(Match.OneOf(Boolean, {
@@ -120,7 +121,7 @@ const factory = (options) => {
120121
let content = null;
121122

122123
try {
123-
content = Utils.readFile(template.path);
124+
content = Utils.readFile(template.path, template.packageFolderName);
124125
} catch (ex) {
125126
Utils.Logger.error(`Could not read template file: ${template.path}`, TAG);
126127
return false;
@@ -129,7 +130,7 @@ const factory = (options) => {
129130
const layout = template.layout || options.layout;
130131

131132
if (layout && template.layout !== false) {
132-
const layoutContent = Utils.readFile(layout.path);
133+
const layoutContent = Utils.readFile(layout.path, template.packageFolderName);
133134
SSR.compileTemplate(layout.name, layoutContent, {
134135
language: settings.language
135136
});
@@ -195,7 +196,7 @@ const factory = (options) => {
195196
// the layout in `<style>` tags. Ideal for media queries.
196197
if (template.extraCSS) {
197198
try {
198-
css = Utils.readFile(template.extraCSS);
199+
css = Utils.readFile(template.extraCSS, template.packageFolderName);
199200
} catch (ex) {
200201
Utils.Logger.error(`Could not add extra CSS when rendering ${templateName}: ${ex.message}`, TAG);
201202
}
@@ -267,7 +268,7 @@ const factory = (options) => {
267268
}
268269
};
269270

270-
const init = () => {
271+
const initFactory = () => {
271272
if (options.templates) {
272273
_.each(options.templates, (template, name) => {
273274
template.name = name;
@@ -284,7 +285,7 @@ const factory = (options) => {
284285
precompile: compile,
285286
render: render,
286287
send: sendEmail,
287-
init
288+
initFactory
288289
};
289290
};
290291

@@ -298,5 +299,5 @@ Mailer.init = function(opts) {
298299
obj.use(Routing);
299300
}
300301

301-
obj.init();
302+
obj.initFactory();
302303
};

Diff for: lib/utils.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const developmentPrivateDir = () => {
2929
return path.join(meteorRoot, 'private');
3030
};
3131

32+
const developmentPackagesPrivateDir = () => {
33+
if (!isDevEnv())
34+
return;
35+
const meteorRoot = process.cwd().replace(/(\.meteor.*)/g, '');
36+
return path.join(meteorRoot, '.meteor', 'local', 'build', 'programs', 'server', 'assets', 'packages');
37+
};
38+
3239
const productionPrivateDir = () => {
3340
if (isDevEnv())
3441
return;
@@ -40,8 +47,10 @@ const productionPrivateDir = () => {
4047
const privateDir = process.env.BUNDLE_PATH || process.env.APP_DIR || productionPrivateDir();
4148

4249
let ROOT = privateDir && path.join(privateDir, 'programs', 'server', 'assets', 'app');
50+
let PACKAGES_ROOT = privateDir && path.join(privateDir, 'programs','server', 'assets', 'packages');
4351

4452
ROOT = ROOT || developmentPrivateDir();
53+
PACKAGES_ROOT = PACKAGES_ROOT || developmentPackagesPrivateDir();
4554

4655
Utils = {
4756
// Takes an HTML string and outputs a text version of it. Catches and logs errors.
@@ -131,8 +140,10 @@ Utils = {
131140
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' + html;
132141
},
133142

134-
readFile: function(relativePathFromApp) {
135-
const file = path.join(ROOT, relativePathFromApp);
143+
readFile: function(relativePathFromApp, packageFolderName) {
144+
const file = packageFolderName ?
145+
path.join(PACKAGES_ROOT, packageFolderName, relativePathFromApp)
146+
: path.join(ROOT, relativePathFromApp);
136147

137148
try {
138149
return fs.readFileSync(file, {
@@ -145,13 +156,15 @@ Utils = {
145156
},
146157

147158
// Take a path to a SCSS file and compiles it to CSS with `node-sass`.
148-
toCSS: function(scss) {
159+
toCSS: function(scss, packageFolderName) {
149160
if (!Package['chrisbutler:node-sass']) {
150161
Utils.Logger.warn('Sass support is opt-in since lookback:[email protected]. Please add chrisbutler:node-sass from Atmosphere and try again.', TAG);
151162
return Utils.readFile(scss);
152163
}
153164

154-
const file = path.join(ROOT, scss);
165+
const file = packageFolderName ?
166+
path.join(PACKAGES_ROOT, packageName, relativePathFromApp)
167+
: path.join(ROOT, scss);
155168
const sass = Package['chrisbutler:node-sass'].sass;
156169

157170
try {

0 commit comments

Comments
 (0)