@gov-cy/dsf-email-templates 2.0.1 → 2.0.2
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/CHANGELOG.md +9 -0
- package/README.md +6 -3
- package/bin/dsf-email-templater.js +12 -2
- package/package.json +1 -1
- package/src/DSFEmailRenderer.mjs +1 -30
- package/test/mailtrap-njk.js +5 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [v2.0.2] - 2024-05-01
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- `dsf-email-templater` use `fs/promises` to load and save files
|
|
12
|
+
- `mailtrap-njk` use `fs/promises` to load files
|
|
13
|
+
|
|
14
|
+
### Removed
|
|
15
|
+
- Removed use of `fs/promises`, `saveFile()` and `renderFromFile()` from `DSFEmailRenderer`
|
|
16
|
+
|
|
8
17
|
## [v2.0.1] - 2024-04-30
|
|
9
18
|
|
|
10
19
|
### Fixed
|
package/README.md
CHANGED
|
@@ -86,10 +86,11 @@ const renderedTemplate = renderer.renderFromString(inputString);
|
|
|
86
86
|
console.log(renderedTemplate); // Output the rendered template
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
You may also use
|
|
89
|
+
You may also use load a template from a file using `fs/promises` and provide a text file with the nunjucks input template as follows:
|
|
90
90
|
|
|
91
91
|
```js
|
|
92
92
|
import DSFEmailRenderer from '@gov-cy/dsf-email-templates';
|
|
93
|
+
import fs from 'fs/promises';
|
|
93
94
|
|
|
94
95
|
// Create an instance of DSFEmailRenderer
|
|
95
96
|
const renderer = new DSFEmailRenderer();
|
|
@@ -97,8 +98,10 @@ const renderer = new DSFEmailRenderer();
|
|
|
97
98
|
// Specify the path of the template file
|
|
98
99
|
const templatePath = 'path/to/template.njk';
|
|
99
100
|
|
|
100
|
-
//
|
|
101
|
-
const
|
|
101
|
+
// Load template file
|
|
102
|
+
const templateContent = await fs.readFile(templatePath, 'utf8');
|
|
103
|
+
// Render template
|
|
104
|
+
const renderedTemplate = renderer.renderFromString(templateContent);
|
|
102
105
|
|
|
103
106
|
console.log(renderedTemplate); // Output the rendered template
|
|
104
107
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { DSFEmailRenderer } from '../src/index.mjs';
|
|
4
|
+
import fs from 'fs/promises';
|
|
4
5
|
|
|
5
6
|
const args = process.argv.slice(2);
|
|
6
7
|
|
|
@@ -15,8 +16,17 @@ const renderer = new DSFEmailRenderer();
|
|
|
15
16
|
|
|
16
17
|
(async () => {
|
|
17
18
|
try {
|
|
18
|
-
|
|
19
|
-
await
|
|
19
|
+
// Load template
|
|
20
|
+
const templateContent = await fs.readFile(inputPath, 'utf8');
|
|
21
|
+
// Render template
|
|
22
|
+
const inputTemplate = renderer.renderFromString(templateContent);
|
|
23
|
+
try {
|
|
24
|
+
// Write rendered content to output file
|
|
25
|
+
await fs.writeFile(outputPath, inputTemplate);
|
|
26
|
+
console.log(`File rendered and saved to ${outputPath}`);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
throw new Error(`Error rendering template: ${error.message}`);
|
|
29
|
+
}
|
|
20
30
|
} catch (error) {
|
|
21
31
|
console.error(error);
|
|
22
32
|
process.exit(1);
|
package/package.json
CHANGED
package/src/DSFEmailRenderer.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import nunjucks from 'nunjucks';
|
|
2
|
-
import fs from 'fs/promises';
|
|
3
2
|
import { dirname, join } from 'path';
|
|
4
3
|
import { fileURLToPath } from 'url';
|
|
5
4
|
|
|
@@ -21,21 +20,7 @@ export default class DSFEmailRenderer {
|
|
|
21
20
|
const templateDirectory = join(__dirname, 'njk');
|
|
22
21
|
nunjucks.configure(templateDirectory);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
* Renders the email html in file, based on an input html string
|
|
26
|
-
*
|
|
27
|
-
* @param {string} inputHTML The input
|
|
28
|
-
* @param {string} outputPath The path of the output html file
|
|
29
|
-
*/
|
|
30
|
-
async saveFile(inputHTML, outputPath) {
|
|
31
|
-
try {
|
|
32
|
-
// Write rendered content to output file
|
|
33
|
-
await fs.writeFile(outputPath, inputHTML);
|
|
34
|
-
console.log(`File rendered and saved to ${outputPath}`);
|
|
35
|
-
} catch (error) {
|
|
36
|
-
throw new Error(`Error rendering template: ${error.message}`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
23
|
+
|
|
39
24
|
/**
|
|
40
25
|
* Returns the rendered email html as string, based on the nunjucks templates
|
|
41
26
|
*
|
|
@@ -50,20 +35,6 @@ export default class DSFEmailRenderer {
|
|
|
50
35
|
return renderedContent;
|
|
51
36
|
}
|
|
52
37
|
|
|
53
|
-
/**
|
|
54
|
-
* Renders the email html, based on an input template
|
|
55
|
-
*
|
|
56
|
-
* @param {string} templatePath The path of the template to used as input
|
|
57
|
-
* @returns {string} Rendered email html as string
|
|
58
|
-
*/
|
|
59
|
-
async renderFromFile(templatePath) {
|
|
60
|
-
// Load template
|
|
61
|
-
const templateContent = await fs.readFile(templatePath, 'utf8');
|
|
62
|
-
// Render template
|
|
63
|
-
const renderedContent = this.renderFromString(templateContent);
|
|
64
|
-
return renderedContent;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
38
|
/**
|
|
68
39
|
* Returns the rendered email html as string, based on the input json object
|
|
69
40
|
*
|
package/test/mailtrap-njk.js
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
*
|
|
33
33
|
*/
|
|
34
34
|
import nodemailer from 'nodemailer';
|
|
35
|
-
import fs from 'fs';
|
|
35
|
+
import fs from 'fs/promises';
|
|
36
36
|
import { DSFEmailRenderer } from '../src/index.mjs';
|
|
37
37
|
import { exit } from 'process';
|
|
38
38
|
|
|
@@ -46,8 +46,10 @@ async function testMailtrap() {
|
|
|
46
46
|
process.exit(1);
|
|
47
47
|
}
|
|
48
48
|
const renderer = new DSFEmailRenderer();
|
|
49
|
-
//
|
|
50
|
-
const
|
|
49
|
+
// Load template
|
|
50
|
+
const templateContent = await fs.readFile(myArgs[0], 'utf8');
|
|
51
|
+
// Render template
|
|
52
|
+
const htmlBody = renderer.renderFromString(templateContent);
|
|
51
53
|
|
|
52
54
|
// Generate test SMTP service account from ethereal.email
|
|
53
55
|
// Only needed if you don't have a real mail account for testing
|