handlebars-email 0.0.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.github/workflows/node.js.yml +31 -0
- package/.github/workflows/npm-publish.yml +33 -0
- package/LICENSE.md +21 -0
- package/README.md +124 -0
- package/package.json +16 -7
- package/src/__tests__/assets/finalTemplate.html +11 -0
- package/src/__tests__/assets/template.hbs +11 -0
- package/src/__tests__/hbsEmail.test.js +7 -0
- package/src/main.js +24 -0
- package/src/modules/hbs.js +4 -0
- package/src/modules/templet.js +40 -0
- package/src/modules/validator.js +15 -0
@@ -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,124 @@
|
|
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
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Use the [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) package manager to install Handlebars Email.
|
13
|
+
|
14
|
+
```bash
|
15
|
+
npm i handlebars-email
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Import Handlebars Email `hbsEmail` method.
|
21
|
+
`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
|
+
|
25
|
+
Once you have a template, use the `hbsEmail` method to render the template by passing the template & context.
|
26
|
+
|
27
|
+
email.js
|
28
|
+
```javascript
|
29
|
+
const { hbsEmail } = require('handlebars-email')
|
30
|
+
const path = require("path")
|
31
|
+
|
32
|
+
const template = path.join(__dirname, '/template.hbs')
|
33
|
+
const context = { message: "Hello World!" }
|
34
|
+
const eMailtemplate = hbsEmail( template, context )
|
35
|
+
```
|
36
|
+
template.hbs
|
37
|
+
```hbs
|
38
|
+
<html>
|
39
|
+
<head>
|
40
|
+
<title>Message HTML Title</title>
|
41
|
+
</head>
|
42
|
+
<body>
|
43
|
+
<div>
|
44
|
+
<h2>Message: </h2>
|
45
|
+
<p>{{message}}</p>
|
46
|
+
</div>
|
47
|
+
</body>
|
48
|
+
</html>
|
49
|
+
```
|
50
|
+
Would render:
|
51
|
+
```html
|
52
|
+
<html>
|
53
|
+
<head>
|
54
|
+
<title>Message HTML Title</title>
|
55
|
+
</head>
|
56
|
+
<body>
|
57
|
+
<div>
|
58
|
+
<h2>Message: </h2>
|
59
|
+
<p>Hello World!</p>
|
60
|
+
</div>
|
61
|
+
</body>
|
62
|
+
</html>
|
63
|
+
```
|
64
|
+
|
65
|
+
___
|
66
|
+
|
67
|
+
## With Nodemailer
|
68
|
+
|
69
|
+
email.js
|
70
|
+
```js
|
71
|
+
const { hbsEmail } = require('handlebars-email')
|
72
|
+
const nodemailer = require("nodemailer")
|
73
|
+
const path = require("path")
|
74
|
+
|
75
|
+
const template = path.join(__dirname, '/template.hbs')
|
76
|
+
const context = { message: "Hello World!" }
|
77
|
+
const eMailtemplate = hbsEmail( template, context )
|
78
|
+
|
79
|
+
|
80
|
+
let 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
|
+
})
|
89
|
+
|
90
|
+
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
|
+
}
|
96
|
+
|
97
|
+
transport.sendMail(mailOptions, (error, email) => {
|
98
|
+
if (error) return console.log(error)
|
99
|
+
console.log('Message sent: %s', email.messageId)
|
100
|
+
});
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
<!-- CONTRIBUTING -->
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
|
110
|
+
|
111
|
+
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".
|
112
|
+
Don't forget to give the project a star! Thanks again!
|
113
|
+
|
114
|
+
1. Fork the Project
|
115
|
+
2. Create your Feature Branch (` git checkout -b feature/AmazingFeature`)
|
116
|
+
3. Commit your Changes (` git commit -m 'Add some AmazingFeature'`)
|
117
|
+
4. Push to the Branch (` git push origin feature/AmazingFeature`)
|
118
|
+
5. Open a Pull Request
|
119
|
+
|
120
|
+
|
121
|
+
<!-- LICENSE -->
|
122
|
+
## License
|
123
|
+
|
124
|
+
Distributed under the MIT License. See `LICENSE.md` for more information.
|
package/package.json
CHANGED
@@ -1,22 +1,31 @@
|
|
1
1
|
{
|
2
2
|
"name": "handlebars-email",
|
3
|
-
"version": "
|
4
|
-
"description": "Handlebars
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A Handlebars template engine for emails.",
|
5
5
|
"main": "src/main.js",
|
6
6
|
"scripts": {
|
7
|
-
"test": "
|
7
|
+
"test": "jest"
|
8
8
|
},
|
9
9
|
"repository": {
|
10
10
|
"type": "git",
|
11
11
|
"url": "git+https://github.com/LazyFolks/handlebars-email.git"
|
12
12
|
},
|
13
13
|
"keywords": [
|
14
|
-
"hbs"
|
14
|
+
"hbs",
|
15
|
+
"handlebars",
|
16
|
+
"handlebars-email",
|
17
|
+
"hbs-email"
|
15
18
|
],
|
16
19
|
"author": "Karthik",
|
17
|
-
"license": "
|
20
|
+
"license": "MIT",
|
18
21
|
"bugs": {
|
19
22
|
"url": "https://github.com/LazyFolks/handlebars-email/issues"
|
20
23
|
},
|
21
|
-
"homepage": "https://github.com/LazyFolks/handlebars-email#readme"
|
22
|
-
|
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,7 @@
|
|
1
|
+
const { hbsEmail, 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
|
+
})
|
package/src/main.js
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
const { hbs, hbsCompile } = require('./modules/hbs')
|
2
|
+
const Templet = require('./modules/templet')
|
3
|
+
const validate = require('./modules/validator')
|
4
|
+
|
5
|
+
const hbsEmail = ( template , context ) => {
|
6
|
+
|
7
|
+
//validate template path
|
8
|
+
validate.template(template)
|
9
|
+
|
10
|
+
const emailTemplateSource = Templet.Reader(template)
|
11
|
+
|
12
|
+
const Compiler = hbsCompile(emailTemplateSource)
|
13
|
+
|
14
|
+
const html = Compiler(context)
|
15
|
+
|
16
|
+
return html
|
17
|
+
}
|
18
|
+
|
19
|
+
module.exports = {
|
20
|
+
hbsEmail:hbsEmail,
|
21
|
+
hbs:hbs,
|
22
|
+
hbsCompile:hbsCompile,
|
23
|
+
Templet:Templet
|
24
|
+
}
|
@@ -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
|