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

Using Mailer inside another package support #51

Closed
wants to merge 9 commits into from
13 changes: 7 additions & 6 deletions lib/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const factory = (options) => {
check(template, Match.ObjectIncluding({
path: String,
name: String,
packageFolderName: Match.Optional(String),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some docs on what to pass as packageFolderName in README?

scss: Match.Optional(String),
css: Match.Optional(String),
layout: Match.Optional(Match.OneOf(Boolean, {
Expand All @@ -120,7 +121,7 @@ const factory = (options) => {
let content = null;

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

if (layout && template.layout !== false) {
const layoutContent = Utils.readFile(layout.path);
const layoutContent = Utils.readFile(layout.path, template.packageFolderName);
SSR.compileTemplate(layout.name, layoutContent, {
language: settings.language
});
Expand Down Expand Up @@ -195,7 +196,7 @@ const factory = (options) => {
// the layout in `<style>` tags. Ideal for media queries.
if (template.extraCSS) {
try {
css = Utils.readFile(template.extraCSS);
css = Utils.readFile(template.extraCSS, template.packageFolderName);
} catch (ex) {
Utils.Logger.error(`Could not add extra CSS when rendering ${templateName}: ${ex.message}`, TAG);
}
Expand Down Expand Up @@ -267,7 +268,7 @@ const factory = (options) => {
}
};

const init = () => {
const initFactory = () => {
if (options.templates) {
_.each(options.templates, (template, name) => {
template.name = name;
Expand All @@ -284,7 +285,7 @@ const factory = (options) => {
precompile: compile,
render: render,
send: sendEmail,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for renaming the public init() interface to initFactory() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I am not renaming the public interface, because Mailer.init is still Mailer.init.

I am renaming the private interface to something different in order _not to _ overwrite this public interface with the extend call, because the way it was before the first call to Mailer.init({...}) overwrote the public Mailer.init interface with the private one.

The goal of this was to enable several init calls to add templates and helpers from several places to the Mailer global. One in main app, and one in the package for example.

But I may have gone a bit too fast: it's not taking into account global helpers name overriding for example...

init
initFactory
};
};

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

obj.init();
obj.initFactory();
};
21 changes: 17 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const developmentPrivateDir = () => {
return path.join(meteorRoot, 'private');
};

const developmentPackagesPrivateDir = () => {
if (!isDevEnv())
return;
const meteorRoot = process.cwd().replace(/(\.meteor.*)/g, '');
return path.join(meteorRoot, '.meteor', 'local', 'build', 'programs', 'server', 'assets', 'packages');
};

const productionPrivateDir = () => {
if (isDevEnv())
return;
Expand All @@ -40,8 +47,10 @@ const productionPrivateDir = () => {
const privateDir = process.env.BUNDLE_PATH || process.env.APP_DIR || productionPrivateDir();

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

ROOT = ROOT || developmentPrivateDir();
PACKAGES_ROOT = PACKAGES_ROOT || developmentPackagesPrivateDir();

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

readFile: function(relativePathFromApp) {
const file = path.join(ROOT, relativePathFromApp);
readFile: function(relativePathFromApp, packageFolderName) {
const file = packageFolderName ?
path.join(PACKAGES_ROOT, packageFolderName, relativePathFromApp)
: path.join(ROOT, relativePathFromApp);

try {
return fs.readFileSync(file, {
Expand All @@ -145,13 +156,15 @@ Utils = {
},

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

const file = path.join(ROOT, scss);
const file = packageFolderName ?
path.join(PACKAGES_ROOT, packageName, relativePathFromApp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like packageName isn't defined here?

: path.join(ROOT, scss);
const sass = Package['chrisbutler:node-sass'].sass;

try {
Expand Down