handlebars-email 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -5,11 +5,9 @@ A Handlebars template engine for emails.
5
5
  Handlebars provides the power necessary to let you build **semantic templates** effectively with no frustration.
6
6
  Checkout the official Handlebars docs site at [handlebarsjs.com](https://handlebarsjs.com) or [Give it a Try](https://handlebarsjs.com/playground.html).
7
7
 
8
-
9
-
10
8
  ## Installation
11
9
 
12
- Use the [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) package manager to install [Handlebars Email](https://www.npmjs.com/package/handlebars-email).
10
+ Use the [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) package manager to install [Handlebars Email](https://www.npmjs.com/package/handlebars-email).
13
11
 
14
12
  ```bash
15
13
  npm i handlebars-email
@@ -19,21 +17,53 @@ npm i handlebars-email
19
17
 
20
18
  Import Handlebars Email `hbsEmail` method.
21
19
  `hbsEmail( template, context )` take 2 argument a template & a context argument. Which will be used to render the final template.
22
- * template - Path of the template with extension ( .hbs or .handlebars )
23
- * context - The actual context Object.
24
20
 
25
- Once you have a template, use the `hbsEmail` method to render the template by passing the template & context.
21
+ - template - Path of the template with extension ( .hbs or .handlebars ) or template name if hbsEmailConfig is set.
22
+ - context - The actual context Object.
23
+
24
+ `hbsEmailConfig()`
25
+
26
+ - views - Views Path of the email template folder
27
+ - extname - template extension ( .hbs or .handlebars )
28
+
29
+ ```js
30
+ hbsEmailConfig({
31
+ views: "views/email/",
32
+ extname: ".hbs"
33
+ });
34
+ ```
35
+
36
+ Once you have a template, use the `hbsEmail` method to render the template by passing the template & context.
37
+
38
+ ## Example
26
39
 
27
40
  email.js
41
+
42
+ ```javascript
43
+ const { hbsEmail } = require("handlebars-email");
44
+ const path = require("path");
45
+
46
+ const template = path.join("views", "email", "template.hbs");
47
+ const context = { message: "Hello World!" };
48
+ const eMailTemplate = hbsEmail(template, context);
49
+ ```
50
+
51
+ With hbsEmailConfig
52
+
28
53
  ```javascript
29
- const { hbsEmail } = require('handlebars-email')
30
- const path = require("path")
54
+ const { hbsEmail, hbsEmailConfig } = require("handlebars-email");
31
55
 
32
- const template = path.join(__dirname, '/template.hbs')
33
- const context = { message: "Hello World!" }
34
- const eMailtemplate = hbsEmail( template, context )
56
+ hbsEmailConfig({
57
+ views: "views/email/",
58
+ extname: ".hbs",
59
+ });
60
+
61
+ const context = { message: "Hello World!" };
62
+ const eMailTemplate = hbsEmail("template", context);
35
63
  ```
64
+
36
65
  template.hbs
66
+
37
67
  ```hbs
38
68
  <html>
39
69
  <head>
@@ -47,7 +77,9 @@ template.hbs
47
77
  </body>
48
78
  </html>
49
79
  ```
80
+
50
81
  Would render:
82
+
51
83
  ```html
52
84
  <html>
53
85
  <head>
@@ -55,55 +87,53 @@ Would render:
55
87
  </head>
56
88
  <body>
57
89
  <div>
58
- <h2>Message: </h2>
90
+ <h2>Message:</h2>
59
91
  <p>Hello World!</p>
60
92
  </div>
61
93
  </body>
62
94
  </html>
63
95
  ```
64
96
 
65
- ___
97
+ ---
66
98
 
67
- ## With Nodemailer
99
+ ## With Nodemailer
68
100
 
69
101
  email.js
70
- ```js
71
- const { hbsEmail } = require('handlebars-email')
72
- const nodemailer = require("nodemailer")
73
- const path = require("path")
74
102
 
75
- const template = path.join(__dirname, '/template.hbs')
76
- const context = { message: "Hello World!" }
77
- const eMailtemplate = hbsEmail( template, context )
103
+ ```js
104
+ const { hbsEmail } = require("handlebars-email");
105
+ const nodemailer = require("nodemailer");
106
+ const path = require("path");
78
107
 
108
+ const template = path.join(__dirname, "/template.hbs");
109
+ const context = { message: "Hello World!" };
110
+ const eMailTemplate = hbsEmail(template, context);
79
111
 
80
112
  const transporter = nodemailer.createTransport({
81
- host: process.env.SMTP_HOST,
82
- port: process.env.SMTP_PORT || 587,
83
- secure: process.env.SMTP_PORT === 465, // true for 465, false for other ports
84
- auth: {
85
- user: process.env.SMTP_USERNAME,
86
- pass: process.env.SMTP_PASSWORD
87
- },
88
- })
113
+ host: process.env.SMTP_HOST,
114
+ port: process.env.SMTP_PORT || 587,
115
+ secure: process.env.SMTP_PORT === 465, // true for 465, false for other ports
116
+ auth: {
117
+ user: process.env.SMTP_USERNAME,
118
+ pass: process.env.SMTP_PASSWORD,
119
+ },
120
+ });
89
121
 
90
122
  const mailOptions = {
91
- from: 'sender@example.com', // Sender address
92
- to: 'receiver@example.com', // List of recipients
93
- subject: 'Node Mailer Handlebars Email', // Subject line
94
- html: eMailtemplate, // Handlebars eMail template
95
- }
123
+ from: "sender@example.com", // Sender address
124
+ to: "receiver@example.com", // List of recipients
125
+ subject: "Node Mailer Handlebars Email", // Subject line
126
+ html: eMailTemplate, // Handlebars eMail template
127
+ };
96
128
 
97
129
  transporter.sendMail(mailOptions, (error, email) => {
98
- if (error) return console.log(error)
99
- console.log('Message sent: %s', email.messageId)
130
+ if (error) return console.log(error);
131
+ console.log("Message sent: %s", email.messageId);
100
132
  });
101
-
102
133
  ```
103
134
 
104
-
105
-
106
135
  <!-- CONTRIBUTING -->
136
+
107
137
  ## Contributing
108
138
 
109
139
  Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
@@ -117,8 +147,8 @@ Don't forget to give the project a star! Thanks again!
117
147
  4. Push to the Branch (` git push origin feature/AmazingFeature`)
118
148
  5. Open a Pull Request
119
149
 
120
-
121
150
  <!-- LICENSE -->
151
+
122
152
  ## License
123
153
 
124
- Distributed under the MIT License. See `LICENSE.md` for more information.
154
+ Distributed under the MIT License. See `LICENSE.md` for more information.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "handlebars-email",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "A Handlebars template engine for emails.",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -1,7 +1,19 @@
1
- const { hbsEmail, Templet } = require('../main')
1
+ const { hbsEmail, hbsEmailConfig, templet } = require('../main')
2
2
 
3
3
  test('hbsEmail', () => {
4
4
  const template = hbsEmail('src/__tests__/assets/template.hbs', { message: "Hello World!" })
5
- const finalTemplate = Templet.Reader('src/__tests__/assets/finalTemplate.html')
5
+ const finalTemplate = templet.reader('src/__tests__/assets/finalTemplate.html')
6
+ expect(template).toBe(finalTemplate)
7
+ })
8
+
9
+ test('hbsEmailConfig', () => {
10
+
11
+ hbsEmailConfig({
12
+ views: 'src/__tests__/assets/',
13
+ extname: '.hbs'
14
+ })
15
+
16
+ const template = hbsEmail('template', { message: "Hello World!" })
17
+ const finalTemplate = templet.reader('src/__tests__/assets/finalTemplate.html')
6
18
  expect(template).toBe(finalTemplate)
7
19
  })
@@ -0,0 +1,24 @@
1
+ const fs = require("fs")
2
+ const path = require('path')
3
+
4
+ const hbsEmailConfig = ( config = {} ) => {
5
+ const configFile = path.join(__dirname, "config.json")
6
+ fs.writeFileSync(configFile, JSON.stringify(config))
7
+ }
8
+
9
+ const getConfig = () => {
10
+
11
+ const configFile = path.join(__dirname, "config.json")
12
+
13
+ if(!fs.existsSync(configFile)) return {}
14
+
15
+ const rawData = fs.readFileSync(configFile)
16
+ const config = JSON.parse(rawData)
17
+ return config
18
+ }
19
+
20
+
21
+ module.exports = {
22
+ hbsEmailConfig: hbsEmailConfig,
23
+ getConfig: getConfig
24
+ }
package/src/main.js CHANGED
@@ -1,13 +1,40 @@
1
1
  const { hbs, hbsCompile } = require('./modules/hbs')
2
- const Templet = require('./modules/templet')
2
+ const { hbsEmailConfig, getConfig } = require('./config/config')
3
+ const path = require('path')
4
+ const templet = require('./modules/templet')
3
5
  const validate = require('./modules/validator')
4
6
 
7
+ const getTemplateFile = template => {
8
+
9
+ const config = getConfig()
10
+
11
+ const viewsPath = config.views
12
+ const extName = config.extname
13
+
14
+ if(!viewsPath && !extName) {
15
+ //validate template path
16
+ validate.template(template)
17
+ return template
18
+ }
19
+
20
+ // if viewsPath doesn't exist but extName ( extension ) exist in config
21
+ if(!viewsPath){
22
+ const templateFile = template.concat(extName)
23
+ validate.template(templateFile)
24
+ return templateFile
25
+ }
26
+
27
+ const templateFile = path.join(viewsPath,template.concat(extName))
28
+ validate.template(templateFile)
29
+ return templateFile
30
+
31
+ }
32
+
5
33
  const hbsEmail = ( template , context ) => {
6
34
 
7
- //validate template path
8
- validate.template(template)
35
+ const templateFile = getTemplateFile(template)
9
36
 
10
- const emailTemplateSource = Templet.Reader(template)
37
+ const emailTemplateSource = templet.reader(templateFile)
11
38
 
12
39
  const Compiler = hbsCompile(emailTemplateSource)
13
40
 
@@ -18,7 +45,8 @@ const hbsEmail = ( template , context ) => {
18
45
 
19
46
  module.exports = {
20
47
  hbsEmail:hbsEmail,
48
+ hbsEmailConfig:hbsEmailConfig,
21
49
  hbs:hbs,
22
50
  hbsCompile:hbsCompile,
23
- Templet:Templet
51
+ templet:templet
24
52
  }
@@ -1,7 +1,7 @@
1
1
  const fs = require('fs')
2
2
 
3
- const Reader = ( template, options = 'utf8' ) => fs.readFileSync(template, options)
4
- const Writer = ( template, data, options ) => fs.writeFileSync( template, data, options )
3
+ const reader = ( template, options = 'utf8' ) => fs.readFileSync(template, options)
4
+ const writer = ( template, data, options ) => fs.writeFileSync( template, data, options )
5
5
  const exists = template => fs.existsSync(template)
6
6
 
7
7
  const canRead = template => {
@@ -31,8 +31,8 @@ const canExecute = template => {
31
31
 
32
32
 
33
33
  module.exports = {
34
- Reader:Reader,
35
- Writer:Writer,
34
+ reader:reader,
35
+ writer:writer,
36
36
  exists:exists,
37
37
  canRead:canRead,
38
38
  canWrite:canWrite,
@@ -1,4 +1,4 @@
1
- const Templet = require('./templet')
1
+ const templet = require('./templet')
2
2
  const path = require('path')
3
3
 
4
4
  const validateTemplate = template => {
@@ -6,10 +6,10 @@ const validateTemplate = template => {
6
6
  const templatePath = path.resolve(template)
7
7
 
8
8
  // Check if template exist.
9
- if(!Templet.exists(template)) throw new Error(`ENOENT: template file doesn't exist. Please choose the correct template path. \n PATH: ${templatePath}`)
9
+ if(!templet.exists(template)) throw new Error(`ENOENT: template file doesn't exist. Please choose the correct template path. \n PATH: ${templatePath}`)
10
10
 
11
11
  // Check read permission.
12
- if(!Templet.canRead(template)) throw new Error(`EACCES: permission denied. Can't read template file. Please Check the template file permission. \n PATH: ${templatePath}`)
12
+ if(!templet.canRead(template)) throw new Error(`EACCES: permission denied. Can't read template file. Please Check the template file permission. \n PATH: ${templatePath}`)
13
13
  }
14
14
 
15
15
  exports.template = validateTemplate