handlebars-email 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of handlebars-email might be problematic. Click here for more details.

@@ -0,0 +1,31 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ "main" ]
9
+ pull_request:
10
+ branches: [ "main" ]
11
+
12
+ jobs:
13
+ build:
14
+
15
+ runs-on: ubuntu-latest
16
+
17
+ strategy:
18
+ matrix:
19
+ node-version: [16.x, 18.x]
20
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
+
22
+ steps:
23
+ - uses: actions/checkout@v3
24
+ - name: Use Node.js ${{ matrix.node-version }}
25
+ uses: actions/setup-node@v3
26
+ with:
27
+ node-version: ${{ matrix.node-version }}
28
+ cache: 'npm'
29
+ - run: npm ci
30
+ - run: npm run build --if-present
31
+ - run: npm test
@@ -0,0 +1,33 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Publish Package to NPM
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+ - uses: actions/setup-node@v3
16
+ with:
17
+ node-version: 16
18
+ - run: npm ci
19
+ - run: npm test
20
+
21
+ publish-npm:
22
+ needs: build
23
+ runs-on: ubuntu-latest
24
+ steps:
25
+ - uses: actions/checkout@v3
26
+ - uses: actions/setup-node@v3
27
+ with:
28
+ node-version: 16
29
+ registry-url: https://registry.npmjs.org/
30
+ - run: npm ci
31
+ - run: npm publish
32
+ env:
33
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Karthik V
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # Handlebars Email
2
+
3
+ A Handlebars template engine for emails.
4
+
5
+ Handlebars provides the power necessary to let you build **semantic templates** effectively with no frustration.
6
+ Checkout the official Handlebars docs site at [handlebarsjs.com](https://handlebarsjs.com) or [Give it a Try](https://handlebarsjs.com/playground.html).
7
+
8
+ ## Installation
9
+
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).
11
+
12
+ ```bash
13
+ npm i handlebars-email
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Import Handlebars Email `hbsEmail` method.
19
+ `hbsEmail( template, context )` take 2 argument a template & a context argument. Which will be used to render the final template.
20
+
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
39
+
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
+
53
+ ```javascript
54
+ const { hbsEmail, hbsEmailConfig } = require("handlebars-email");
55
+
56
+ hbsEmailConfig({
57
+ views: "views/email/",
58
+ extname: ".hbs",
59
+ });
60
+
61
+ const context = { message: "Hello World!" };
62
+ const eMailTemplate = hbsEmail("template", context);
63
+ ```
64
+
65
+ template.hbs
66
+
67
+ ```hbs
68
+ <html>
69
+ <head>
70
+ <title>Message HTML Title</title>
71
+ </head>
72
+ <body>
73
+ <div>
74
+ <h2>Message: </h2>
75
+ <p>{{message}}</p>
76
+ </div>
77
+ </body>
78
+ </html>
79
+ ```
80
+
81
+ Would render:
82
+
83
+ ```html
84
+ <html>
85
+ <head>
86
+ <title>Message HTML Title</title>
87
+ </head>
88
+ <body>
89
+ <div>
90
+ <h2>Message:</h2>
91
+ <p>Hello World!</p>
92
+ </div>
93
+ </body>
94
+ </html>
95
+ ```
96
+
97
+ ---
98
+
99
+ ## With Nodemailer
100
+
101
+ email.js
102
+
103
+ ```js
104
+ const { hbsEmail } = require("handlebars-email");
105
+ const nodemailer = require("nodemailer");
106
+ const path = require("path");
107
+
108
+ const template = path.join(__dirname, "/template.hbs");
109
+ const context = { message: "Hello World!" };
110
+ const eMailTemplate = hbsEmail(template, context);
111
+
112
+ const transporter = nodemailer.createTransport({
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
+ });
121
+
122
+ const mailOptions = {
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
+ };
128
+
129
+ transporter.sendMail(mailOptions, (error, email) => {
130
+ if (error) return console.log(error);
131
+ console.log("Message sent: %s", email.messageId);
132
+ });
133
+ ```
134
+
135
+ <!-- CONTRIBUTING -->
136
+
137
+ ## Contributing
138
+
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**.
140
+
141
+ If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
142
+ Don't forget to give the project a star! Thanks again!
143
+
144
+ 1. Fork the Project
145
+ 2. Create your Feature Branch (` git checkout -b feature/AmazingFeature`)
146
+ 3. Commit your Changes (` git commit -m 'Add some AmazingFeature'`)
147
+ 4. Push to the Branch (` git push origin feature/AmazingFeature`)
148
+ 5. Open a Pull Request
149
+
150
+ <!-- LICENSE -->
151
+
152
+ ## License
153
+
154
+ Distributed under the MIT License. See `LICENSE.md` for more information.
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "handlebars-email",
3
+ "version": "1.1.0",
4
+ "description": "A Handlebars template engine for emails.",
5
+ "main": "src/main.js",
6
+ "scripts": {
7
+ "test": "jest"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/LazyFolks/handlebars-email.git"
12
+ },
13
+ "keywords": [
14
+ "hbs",
15
+ "handlebars",
16
+ "handlebars-email",
17
+ "hbs-email"
18
+ ],
19
+ "author": "Karthik",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/LazyFolks/handlebars-email/issues"
23
+ },
24
+ "homepage": "https://github.com/LazyFolks/handlebars-email#readme",
25
+ "dependencies": {
26
+ "handlebars": "^4.7.7"
27
+ },
28
+ "devDependencies": {
29
+ "jest": "^29.3.1"
30
+ }
31
+ }
@@ -0,0 +1,11 @@
1
+ <html>
2
+ <head>
3
+ <title>Message HTML Title</title>
4
+ </head>
5
+ <body>
6
+ <div>
7
+ <span style="font-weight: bold;">Message: </span>
8
+ <span>Hello World!</span>
9
+ </div>
10
+ </body>
11
+ </html>
@@ -0,0 +1,11 @@
1
+ <html>
2
+ <head>
3
+ <title>Message HTML Title</title>
4
+ </head>
5
+ <body>
6
+ <div>
7
+ <span style="font-weight: bold;">Message: </span>
8
+ <span>{{message}}</span>
9
+ </div>
10
+ </body>
11
+ </html>
@@ -0,0 +1,19 @@
1
+ const { hbsEmail, hbsEmailConfig, templet } = require('../main')
2
+
3
+ test('hbsEmail', () => {
4
+ const template = hbsEmail('src/__tests__/assets/template.hbs', { message: "Hello World!" })
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')
18
+ expect(template).toBe(finalTemplate)
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 ADDED
@@ -0,0 +1,52 @@
1
+ const { hbs, hbsCompile } = require('./modules/hbs')
2
+ const { hbsEmailConfig, getConfig } = require('./config/config')
3
+ const path = require('path')
4
+ const templet = require('./modules/templet')
5
+ const validate = require('./modules/validator')
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
+
33
+ const hbsEmail = ( template , context ) => {
34
+
35
+ const templateFile = getTemplateFile(template)
36
+
37
+ const emailTemplateSource = templet.reader(templateFile)
38
+
39
+ const Compiler = hbsCompile(emailTemplateSource)
40
+
41
+ const html = Compiler(context)
42
+
43
+ return html
44
+ }
45
+
46
+ module.exports = {
47
+ hbsEmail:hbsEmail,
48
+ hbsEmailConfig:hbsEmailConfig,
49
+ hbs:hbs,
50
+ hbsCompile:hbsCompile,
51
+ templet:templet
52
+ }
@@ -0,0 +1,4 @@
1
+ const handlebars = require('handlebars')
2
+
3
+ exports.hbs = handlebars
4
+ exports.hbsCompile = handlebars.compile
@@ -0,0 +1,40 @@
1
+ const fs = require('fs')
2
+
3
+ const reader = ( template, options = 'utf8' ) => fs.readFileSync(template, options)
4
+ const writer = ( template, data, options ) => fs.writeFileSync( template, data, options )
5
+ const exists = template => fs.existsSync(template)
6
+
7
+ const canRead = template => {
8
+ try {
9
+ fs.accessSync(template, fs.constants.R_OK)
10
+ return true
11
+ } catch (error) {
12
+ return false
13
+ }
14
+ }
15
+ const canWrite = template => {
16
+ try {
17
+ fs.accessSync(template, fs.constants.W_OK)
18
+ return true
19
+ } catch (error) {
20
+ return false
21
+ }
22
+ }
23
+ const canExecute = template => {
24
+ try {
25
+ fs.accessSync(template, fs.constants.X_OK)
26
+ return true
27
+ } catch (error) {
28
+ return false
29
+ }
30
+ }
31
+
32
+
33
+ module.exports = {
34
+ reader:reader,
35
+ writer:writer,
36
+ exists:exists,
37
+ canRead:canRead,
38
+ canWrite:canWrite,
39
+ canExecute:canExecute
40
+ }
@@ -0,0 +1,15 @@
1
+ const templet = require('./templet')
2
+ const path = require('path')
3
+
4
+ const validateTemplate = template => {
5
+
6
+ const templatePath = path.resolve(template)
7
+
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}`)
10
+
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}`)
13
+ }
14
+
15
+ exports.template = validateTemplate