Skip to content

Commit

Permalink
fix: load partials and set global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoleal committed Feb 5, 2020
1 parent ec44f5d commit ee62f27
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
36 changes: 23 additions & 13 deletions lib/adapters/handlebars.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import { TemplateAdapter } from '../interfaces/template-adapter.interface';
export class HandlebarsAdapter implements TemplateAdapter {
private precompiledTemplates: { [name: string]: handlebars.TemplateDelegate } = {};

constructor() {
handlebars.registerHelper('concat', (...args) => {
args.pop();
return args.join('');
});
}

public compile(mail: any, callback: any, mailerOptions: MailerOptions): void {
const precompile = (template, callback, options) => {
const templateExt = path.extname(template) || '.hbs';
Expand All @@ -25,36 +32,39 @@ export class HandlebarsAdapter implements TemplateAdapter {
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);
}
}

return {
templateExt,
templateName,
templateDir,
templatePath,
};
};

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

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

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

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

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@veeklabs/nestjs-modules-mailer",
"version": "1.3.0",
"name": "@veek/nestjs-modules-mailer",
"version": "1.3.1",
"description": "A mailer module for Nest framework (node.js)",
"main": "dist/index.js",
"types": "dist/index",
Expand Down Expand Up @@ -29,7 +29,8 @@
"contributors": [
"Paweł Partyka <[email protected]>",
"Cristiam Díaz <[email protected]>",
"Juan Echeverry <[email protected]>"
"Juan Echeverry <[email protected]>",
"Eduardo Leal <[email protected]>"
],
"license": "MIT",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion publish.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

npm version $1
npm publish
npm publish --access public

0 comments on commit ee62f27

Please sign in to comment.