handlebars-email 1.1.1
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.
- package/.gitattributes +2 -0
- package/.github/workflows/node.js.yml +31 -0
- package/.github/workflows/npm-publish.yml +33 -0
- package/LICENSE.md +21 -0
- package/README.md +175 -0
- package/package.json +31 -0
- package/src/__tests__/assets/finalTemplate.html +11 -0
- package/src/__tests__/assets/template.hbs +11 -0
- package/src/__tests__/hbsEmail.test.js +19 -0
- package/src/config/config.js +24 -0
- package/src/main.js +52 -0
- package/src/modules/hbs.js +4 -0
- package/src/modules/templet.js +40 -0
- package/src/modules/validator.js +15 -0
package/.gitattributes
ADDED
@@ -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,175 @@
|
|
1
|
+
# Handlebars Email
|
2
|
+
|
3
|
+
A repack from Karthik gh. LazyFolks/handlebars-email.git - 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
|
+
# Handlebars Email
|
17
|
+
|
18
|
+
A Handlebars template engine for emails.
|
19
|
+
|
20
|
+
Handlebars provides the power necessary to let you build **semantic templates** effectively with no frustration.
|
21
|
+
Checkout the official Handlebars docs site at [handlebarsjs.com](https://handlebarsjs.com) or [Give it a Try](https://handlebarsjs.com/playground.html).
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
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).
|
26
|
+
|
27
|
+
```bash
|
28
|
+
npm i handlebars-email
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
Import Handlebars Email `hbsEmail` method.
|
34
|
+
`hbsEmail( template, context )` take 2 argument a template & a context argument. Which will be used to render the final template.
|
35
|
+
|
36
|
+
- template - Path of the template with extension ( .hbs or .handlebars ) or template name if hbsEmailConfig is set.
|
37
|
+
- context - The actual context Object.
|
38
|
+
|
39
|
+
`hbsEmailConfig()`
|
40
|
+
|
41
|
+
- views - Views Path of the email template folder
|
42
|
+
- extname - template extension ( .hbs or .handlebars )
|
43
|
+
|
44
|
+
```js
|
45
|
+
hbsEmailConfig({
|
46
|
+
views: 'views/email/',
|
47
|
+
extname: '.hbs',
|
48
|
+
});
|
49
|
+
```
|
50
|
+
|
51
|
+
Once you have a template, use the `hbsEmail` method to render the template by passing the template & context.
|
52
|
+
|
53
|
+
## Example
|
54
|
+
|
55
|
+
email.js
|
56
|
+
|
57
|
+
```javascript
|
58
|
+
const { hbsEmail } = require('handlebars-email');
|
59
|
+
const path = require('path');
|
60
|
+
|
61
|
+
const template = path.join('views', 'email', 'template.hbs');
|
62
|
+
const context = { message: 'Hello World!' };
|
63
|
+
const eMailTemplate = hbsEmail(template, context);
|
64
|
+
```
|
65
|
+
|
66
|
+
With hbsEmailConfig
|
67
|
+
|
68
|
+
```javascript
|
69
|
+
const { hbsEmail, hbsEmailConfig } = require('handlebars-email');
|
70
|
+
|
71
|
+
hbsEmailConfig({
|
72
|
+
views: 'views/email/',
|
73
|
+
extname: '.hbs',
|
74
|
+
});
|
75
|
+
|
76
|
+
const context = { message: 'Hello World!' };
|
77
|
+
const eMailTemplate = hbsEmail('template', context);
|
78
|
+
```
|
79
|
+
|
80
|
+
template.hbs
|
81
|
+
|
82
|
+
```hbs
|
83
|
+
<html>
|
84
|
+
<head>
|
85
|
+
<title>
|
86
|
+
Message HTML Title
|
87
|
+
</title>
|
88
|
+
</head>
|
89
|
+
<body>
|
90
|
+
<div>
|
91
|
+
<h2>
|
92
|
+
Message:
|
93
|
+
</h2>
|
94
|
+
<p>
|
95
|
+
{{message}}
|
96
|
+
</p>
|
97
|
+
</div>
|
98
|
+
</body>
|
99
|
+
</html>
|
100
|
+
```
|
101
|
+
|
102
|
+
Would render:
|
103
|
+
|
104
|
+
```html
|
105
|
+
<html>
|
106
|
+
<head>
|
107
|
+
<title>Message HTML Title</title>
|
108
|
+
</head>
|
109
|
+
<body>
|
110
|
+
<div>
|
111
|
+
<h2>Message:</h2>
|
112
|
+
<p>Hello World!</p>
|
113
|
+
</div>
|
114
|
+
</body>
|
115
|
+
</html>
|
116
|
+
```
|
117
|
+
|
118
|
+
---
|
119
|
+
|
120
|
+
## With Nodemailer
|
121
|
+
|
122
|
+
email.js
|
123
|
+
|
124
|
+
```js
|
125
|
+
const { hbsEmail } = require('handlebars-email');
|
126
|
+
const nodemailer = require('nodemailer');
|
127
|
+
const path = require('path');
|
128
|
+
|
129
|
+
const template = path.join(__dirname, '/template.hbs');
|
130
|
+
const context = { message: 'Hello World!' };
|
131
|
+
const eMailTemplate = hbsEmail(template, context);
|
132
|
+
|
133
|
+
const transporter = nodemailer.createTransport({
|
134
|
+
host: process.env.SMTP_HOST,
|
135
|
+
port: process.env.SMTP_PORT || 587,
|
136
|
+
secure: process.env.SMTP_PORT === 465, // true for 465, false for other ports
|
137
|
+
auth: {
|
138
|
+
user: process.env.SMTP_USERNAME,
|
139
|
+
pass: process.env.SMTP_PASSWORD,
|
140
|
+
},
|
141
|
+
});
|
142
|
+
|
143
|
+
const mailOptions = {
|
144
|
+
from: 'sender@example.com', // Sender address
|
145
|
+
to: 'receiver@example.com', // List of recipients
|
146
|
+
subject: 'Node Mailer Handlebars Email', // Subject line
|
147
|
+
html: eMailTemplate, // Handlebars eMail template
|
148
|
+
};
|
149
|
+
|
150
|
+
transporter.sendMail(mailOptions, (error, email) => {
|
151
|
+
if (error) return console.log(error);
|
152
|
+
console.log('Message sent: %s', email.messageId);
|
153
|
+
});
|
154
|
+
```
|
155
|
+
|
156
|
+
<!-- CONTRIBUTING -->
|
157
|
+
|
158
|
+
## Contributing
|
159
|
+
|
160
|
+
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
|
161
|
+
|
162
|
+
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".
|
163
|
+
Don't forget to give the project a star! Thanks again!
|
164
|
+
|
165
|
+
1. Fork the Project
|
166
|
+
2. Create your Feature Branch (` git checkout -b feature/AmazingFeature`)
|
167
|
+
3. Commit your Changes (` git commit -m 'Add some AmazingFeature'`)
|
168
|
+
4. Push to the Branch (` git push origin feature/AmazingFeature`)
|
169
|
+
5. Open a Pull Request
|
170
|
+
|
171
|
+
<!-- LICENSE -->
|
172
|
+
|
173
|
+
## License
|
174
|
+
|
175
|
+
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.1",
|
4
|
+
"description": "A repack from Karthik gh. LazyFolks/handlebars-email.git - 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/fgirolami29/handlebars-email.git"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"hbs",
|
15
|
+
"handlebars",
|
16
|
+
"handlebars-email",
|
17
|
+
"hbs-email"
|
18
|
+
],
|
19
|
+
"author": "fgirolami29",
|
20
|
+
"license": "MIT",
|
21
|
+
"bugs": {
|
22
|
+
"url": "https://github.com/fgirolami29/handlebars-email/issues"
|
23
|
+
},
|
24
|
+
"homepage": "https://github.com/fgirolami29/handlebars-email#readme",
|
25
|
+
"dependencies": {
|
26
|
+
"handlebars": "^4.7.7"
|
27
|
+
},
|
28
|
+
"devDependencies": {
|
29
|
+
"jest": "^29.3.1"
|
30
|
+
}
|
31
|
+
}
|
@@ -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,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
|