Skip to content

Commit

Permalink
build: 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosCarmona committed Jul 23, 2020
1 parent 1adf8cc commit b4dd491
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 512 deletions.
9 changes: 7 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#IDE
.vscode
#
api
node_modules
samples
*.md
*.json
.DS_Store
.npmignore
LICENSE
5 changes: 2 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"browser": true,
"node": true
},
"extends": "eslint:recommended",
"extends": ["plugin:prettier/recommended"],
"globals": {
"Promise": true,
"global": true
},
"parserOptions": {
"ecmaVersion": 2017,
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
Expand All @@ -19,7 +19,6 @@
"indent": ["error", 2, { "SwitchCase": 1 }],
"default-case": "error",
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double"],
"semi": ["error", "always"]
}
}
Empty file modified .gitignore
100644 → 100755
Empty file.
14 changes: 14 additions & 0 deletions .prettierignore
100755 → 100644
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules
.all-contributorsrc
.eslintignore
.gitignore
.prettierignore
.DS_Store
LICENSE
*.png
*.txt
TODO
.npmignore
docs/functions.md
*.md
test.log
6 changes: 4 additions & 2 deletions .prettierrc.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"printWidth": 120,
"singleQuote": false,
"singleQuote": true,
"useTabs": false,
"tabWidth": 2,
"semi": true,
"bracketSpacing": true
"bracketSpacing": true,
"trailingComma": "none",
"arrowParens": "avoid"
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Coderty S.L.
Copyright (c) 2020 Coderty S.L.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
63 changes: 60 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
<p align="center">
<a href="http://runnerty.io">
<img height="257" src="https://runnerty.io/assets/header/logo-stroked.png">
</a>
<p align="center">A new way for processes managing</p>
</p>

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Dependency Status][david-badge]][david-badge-url]
<a href="#badge">
<img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg">
</a>


# Mail notifier for [Runnerty]:
Email notification module with [ejs] template support.

### Configuration sample:
### Installation:
```bash
npm i @runnerty/notifier-mail
```

### Configuration sample:
Add in [config.json]:
```json
{
"id": "mail_default",
Expand All @@ -15,9 +34,39 @@
"ejsRender": true
}
```
```json
{
"id": "mail_default",
"type": "@runnerty-notifier-mail",
"disable": false,
"from": "Runnerty <[email protected]>",
"to": ["NAME <[email protected]>"],
"transport": {
"host": "smtp.mailhost.com",
"port": 465,
"secure": true,
"auth": {
"user": "USER_SAMPLE",
"pass": "PASS_SAMPLE"
}
},
"templateDir": "templates",
"template": "template_one",
"attachments": [
{
"filename": "runnerty.png",
"path": "templates/imgs/runnerty.png",
"cid": "[email protected]"
}
],
"ejsRender": true,
"maxConcurrents": 2,
"minInterval": 25
}
```

### Plan sample:

Add in [plan.json]:
```json
{
"id": "mail_default",
Expand All @@ -26,4 +75,12 @@
}
```

[runnerty]: http://www.runnerty.io
[Runnerty]: http://www.runnerty.io
[downloads-image]: https://img.shields.io/npm/dm/@runnerty/notifier-mail.svg
[npm-url]: https://www.npmjs.com/package/@runnerty/notifier-mail
[npm-image]: https://img.shields.io/npm/v/@runnerty/notifier-mail.svg
[david-badge]: https://david-dm.org/runnerty/notifier-mail.svg
[david-badge-url]: https://david-dm.org/runnerty/notifier-mail
[config.json]: http://docs.runnerty.io/config/
[plan.json]: http://docs.runnerty.io/plan/
[ejs]: https://ejs.co
133 changes: 63 additions & 70 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,95 @@
"use strict";
'use strict';

const Notification = global.NotificationClass;
const nodemailer = require("nodemailer");
const path = require("path");
const fs = require("fs");
const nodemailer = require('nodemailer');
const path = require('path');
const fs = require('fs');

class mailNotifier extends Notification {
constructor(notification) {
super(notification);
}

send(notification) {
let _this = this;
let endOptions = {};
notification.to = notification.to ? notification.to.toString() : "";
notification.cc = notification.cc ? notification.cc.toString() : "";
notification.bcc = notification.bcc ? notification.bcc.toString() : "";

function readFilePromise(type, file) {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
let res = {};
if (err) {
res[type] = err;
reject(res);
} else {
res[type] = data;
resolve(res);
}
});
});
}
notification.to = notification.to ? notification.to.toString() : '';
notification.cc = notification.cc ? notification.cc.toString() : '';
notification.bcc = notification.bcc ? notification.bcc.toString() : '';

let transport = nodemailer.createTransport(notification.transport);
let filesReads = [];

let templateDir = path.resolve(notification.templateDir, notification.template);
let htmlTemplate = path.resolve(templateDir, "html.html");
let txtTemplate = path.resolve(templateDir, "text.txt");
let htmlTemplate = path.resolve(templateDir, 'html.html');
let txtTemplate = path.resolve(templateDir, 'text.txt');

filesReads.push(readFilePromise("html", htmlTemplate));
filesReads.push(readFilePromise("text", txtTemplate));
filesReads.push(this.readFilePromise('html', htmlTemplate));
filesReads.push(this.readFilePromise('text', txtTemplate));

Promise.all(filesReads)
.then(res => {
try {
Promise.all(filesReads).then(res => {
let html_data;
let text_data;

if (res[0].hasOwnProperty("html")) {
if (res[0].hasOwnProperty('html')) {
[html_data, text_data] = [res[0].html.toString(), res[1].text.toString()];
} else {
[html_data, text_data] = [res[1].html.toString(), res[0].text.toString()];
}

let textData = [];
textData.push(_this.replaceWith(html_data, notification));
textData.push(_this.replaceWith(text_data, notification));
textData.push(this.recursiveObjectInterpreter(html_data, notification));
textData.push(this.recursiveObjectInterpreter(text_data, notification));

Promise.all(textData)
.then(res => {
let [html, text] = res;
let mailOptions = {
from: notification.from,
to: notification.to,
cc: notification.cc,
bcc: notification.bcc,
subject: notification.subject,
text: text,
html: html,
attachments: notification.attachments
};
Promise.all(textData).then(res => {
let [html, text] = res;
let mailOptions = {
from: notification.from,
to: notification.to,
cc: notification.cc,
bcc: notification.bcc,
subject: notification.subject,
text: text,
html: html,
attachments: notification.attachments
};

if (notification.disable) {
_this.logger("warn", "Mail sender is disable.");
endOptions.messageLog = "Mail sender is disable.";
_this.end(endOptions);
} else {
transport.sendMail(mailOptions, err => {
if (err) {
endOptions.messageLog = "Mail sender:" + JSON.stringify(err);
_this.end(endOptions);
} else {
_this.end();
}
});
}
})
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = "Mail sender:" + JSON.stringify(err);
_this.end(endOptions);
});
})
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = "Mail sender:" + JSON.stringify(err);
_this.end(endOptions);
if (notification.disable) {
this.logger('warn', 'Mail sender is disable.');
endOptions.messageLog = 'Mail sender is disable.';
this.end(endOptions);
} else {
transport.sendMail(mailOptions, err => {
if (err) {
endOptions.messageLog = 'Mail sender:' + JSON.stringify(err);
this.end(endOptions);
} else {
this.end();
}
});
}
});
});
} catch (err) {
endOptions.end = 'error';
endOptions.messageLog = 'Mail sender:' + JSON.stringify(err);
this.end(endOptions);
}
}

readFilePromise(type, file) {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
let res = {};
if (err) {
res[type] = err;
reject(res);
} else {
res[type] = data;
resolve(res);
}
});
});
}
}

Expand Down
Loading

0 comments on commit b4dd491

Please sign in to comment.