-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (70 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const nodemailer = require('nodemailer');
const config = require('./config');
const template = require('./template');
const shell = require('shelljs')
if (!config || !config.email || !config.pass || !config.to) {
console.log('oops! touch a config file for your gmail address, password and receiver!');
return;
}
if (!config.repoPath) {
console.log('oops! repo path is NEEDED!');
return;
}
let transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: config.email,
pass: config.pass
}
});
let date = new Date();
let mailOptions = {
from: `${config.from} <${config.email}>`,
to: config.to,
subject: `Daily Report - ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.`,
html: ''
};
var sendMailToBoss = (commits) => {
mailOptions.html = template({
table: commits.replace(/\*/g, '').split('\n'),
footer: {
userName: config.from,
phone: config.phone || '',
email: config.workingEmail || config.email
}
});
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return _sendFailEmail(error);
}
console.log(`Congratuactions!, ${date.toLocaleDateString()} mail succeed!\n`);
console.log(`options: ${JSON.stringify({
from: `${config.from} <${config.email}>`,
to: config.to,
subject: `Daily Report - ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.`,
})}\n\n`);
});
};
shell.cd(config.repoPath);
shell.exec('git log --graph --pretty=format:"%s" --abbrev-commit --since="0am"', {
silent: true
}, (code, stdout, stderr) => {
if (code != 0) {
_sendFailEmail(stderr);
return;
}
if (!stdout) {
return;
}
sendMailToBoss(stdout);
});
function _sendFailEmail(err) {
console.log(`oops!, ${date.toLocaleDateString()} mail filed!\n`);
console.log(err);
transporter.sendMail({
from: mailOptions.from,
to: mailOptions.from,
subject: `Failed! Boss did not get your email in ${date.toLocaleString()}`,
text: `send email failed,mail options: ${JSON.stringify(mailOptions)}`
});
}