Skip to content

Commit

Permalink
feat: enable handlebars partials
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoleal committed Feb 5, 2020
1 parent f093d87 commit da4eb86
Show file tree
Hide file tree
Showing 4 changed files with 5,382 additions and 14 deletions.
55 changes: 41 additions & 14 deletions lib/adapters/handlebars.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as handlebars from 'handlebars';
import * as CSSInliner from 'css-inliner';
import * as glob from 'glob';
import { get } from 'lodash';

/** Interfaces **/
Expand All @@ -13,22 +14,49 @@ export class HandlebarsAdapter implements TemplateAdapter {
private precompiledTemplates: { [name: string]: handlebars.TemplateDelegate } = {};

public compile(mail: any, callback: any, mailerOptions: MailerOptions): void {
const templateExt = path.extname(mail.data.template) || '.hbs';
const templateName = path.basename(mail.data.template, path.extname(mail.data.template));
const templateDir = path.dirname(mail.data.template) !== '.' ? path.dirname(mail.data.template) : get(mailerOptions, 'template.dir', '');
const templatePath = path.join(templateDir, templateName + templateExt);

if (!this.precompiledTemplates[templateName]) {
try {
const template = fs.readFileSync(templatePath, 'UTF-8');

this.precompiledTemplates[templateName] = handlebars.compile(template, get(mailerOptions, 'template.options', {}));
} catch (err) {
return callback(err);
const precompile = (template, callback, options) => {
const templateExt = path.extname(template) || '.hbs';
const templateName = path.basename(template, path.extname(template));
const templateDir = path.dirname(template) !== '.' ? path.dirname(template) : get(options, 'dir', '');
const templatePath = path.join(templateDir, templateName + templateExt);

if (!this.precompiledTemplates[templateName]) {
try {
const template = fs.readFileSync(templatePath, 'UTF-8');

this.precompiledTemplates[templateName] = handlebars.compile(template, get(options, 'options', {}));

return {
templateExt,
templateName,
templateDir,
templatePath,
};
} catch (err) {
return callback(err);
}
}
};

const {
templateName,
templatePath,
} = precompile(mail.data.template, callback, mailerOptions.template);

const { partials } = get(mailerOptions, 'template.options', {
partials: false,
})

if (partials) {
const files = glob.sync(path.join(partials.dir, '*.hbs'));
files.forEach((file: string) => {
precompile(file, () => {}, partials);
});
}

const rendered = this.precompiledTemplates[templateName](mail.data.context);
const rendered = this.precompiledTemplates[templateName](mail.data.context, {
partials: this.precompiledTemplates
});

const { dir } = path.parse(templatePath);
const inliner = new CSSInliner({ directory: dir });
Expand All @@ -37,6 +65,5 @@ export class HandlebarsAdapter implements TemplateAdapter {
mail.data.html = html;
return callback();
});

}
}
1 change: 1 addition & 0 deletions lib/interfaces/mailer-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface MailerOptions {
adapter?: TemplateAdapter;
options?: { [name: string]: any; };
};
options?: { [name: string]: any; };
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@nestjs/common": "^6.10.10",
"@nestjs/core": "^6.10.10",
"css-inliner": "^1.1.4",
"glob": "^7.1.6",
"handlebars": "^4.5.3",
"lodash": "^4.17.15",
"nodemailer": "^6.4.2",
Expand All @@ -48,6 +49,7 @@
},
"devDependencies": {
"@nestjs/testing": "^6.10.10",
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.23",
"@types/lodash": "^4.14.149",
"@types/nodemailer": "^6.2.2",
Expand Down
Loading

0 comments on commit da4eb86

Please sign in to comment.