-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (146 loc) · 4.91 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require('dotenv').config()
const fs = require('fs')
const ora = require('ora')
const juice = require('juice')
const Handlebars = require('handlebars')
const sendGmail = require('gmail-send')
const batchPromises = require('batch-promises')
const argv = require('yargs').argv
const campaigns = require('./emails')
function exitWithError(errorDescription) {
console.error(`\n${errorDescription}`)
process.exit()
}
function createSpinner() {
const spinner = ora('Loading campaigns').start();
spinner.color = 'yellow'
return spinner
}
function spinnerProgress(current, total, description) {
return `[${current}/${total}] ${description}`
}
function sendEmailsByEmailName(name, emails) {
const emailModule = require(`./emails/${name}`)
const spinner = createSpinner()
const gmailSender = loginToGmail()
const emailsToSend = transformCampaign(emails, emailModule)
const emailCount = emailsToSend.length
batchPromises(2, emailsToSend, async email => {
let index = emailsToSend.findIndex(item => item == email)
spinner.text = spinnerProgress(index + 1, emailCount, `Email with subject ${email.subject}`)
return processEmail(gmailSender, email)
}).then(function () {
spinner.color = 'green'
spinner.text = "Finished processing emails. Wrapping things up.."
setTimeout(() => {
process.exit()
}, 1000);
})
}
function sendEmailsByCampaign(campaign, emails) {
const campaignEmails = campaigns[campaign]
if ([campaign, emails].includes(undefined)) {
exitWithError("One of the parameters passed was invalid")
}
const spinner = createSpinner()
if (campaignEmails == undefined) {
exitWithError("Campaign not found")
}
const gmailSender = loginToGmail()
const emailsToSendOut = campaignEmails.flatMap((campaign, campaignIndex) => transformCampaign(emails, campaign, campaignIndex))
const emailCount = emailsToSendOut.length
batchPromises(2, emailsToSendOut, async email => {
let index = emailsToSendOut.findIndex(item => item == email)
spinner.text = spinnerProgress(index + 1, emailCount, `Email with subject ${email.subject}`)
return processEmail(gmailSender, email)
}).then(function () {
spinner.color = 'green'
spinner.text = "Finished processing emails. Wrapping things up.."
setTimeout(() => {
process.exit()
}, 1000);
})
}
function transformCampaign(emails, campaign) {
const template = generateCampaignTemplate(campaign.info())
return emails.map((email, emailIndex) => {
return transformCampainEmail(template, campaign, email, emailIndex)
})
}
function generateCampaignTemplate({ template, stylesheet = "" }) {
var source = juice(`<style>${stylesheet}</style>${template}`)
return Handlebars.compile(source);
}
function transformCampainEmail(template, campaign, email, emailIndex) {
const { subject, data = {} } = campaign.transformer(emailIndex, email)
const emailBody = template(data)
return {
email,
subject,
body: emailBody
}
}
function loginToGmail() {
return sendGmail({
user: process.env.EMAILER_EMAIL,
pass: process.env.EMAILER_PASSWORD,
})
}
async function processEmail(send, emailDetails) {
try {
await send({
to: emailDetails.email,
subject: emailDetails.subject,
html: emailDetails.body
})
} catch (error) {
console.log(`Failed sending email to: ${emailDetails.email} - ${emailDetails.subject}`)
}
}
function validateArguments() {
var errored = false
switch (argv.method) {
case "campaign": {
if (!argv.campaign) {
console.error(`"--campaign" property missing`)
errored = true
}
if (!argv.emails) {
console.error(`"--emails" property missing`)
errored = true
}
break
}
case "single": {
if (!argv.emailName) {
console.error(`"--emailName" property missing`)
errored = true
}
if (!argv.emails) {
console.error(`"--emails" property missing`)
errored = true
}
break
}
case undefined:
console.error(`"--method" property missing`)
errored = true
break
default: break
}
if (errored) {
process.exit()
}
}
validateArguments()
switch (argv.method) {
case "campaign": {
const emails = require(`./recipients/${argv.emails}.json`)
return sendEmailsByCampaign(argv.campaign, emails)
}
case "single": {
const emails = require(`./recipients/${argv.emails}.json`)
return sendEmailsByEmailName(argv.emailName, emails)
}
default: exitWithError(`Unknown "--method" property provided`)
}