-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathissueEmailGenerator.js
More file actions
226 lines (168 loc) · 5.26 KB
/
Copy pathissueEmailGenerator.js
File metadata and controls
226 lines (168 loc) · 5.26 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* This script generates the email template
* for a particular issue.
*
* Use this script to generate the email template
* via the following command:
*
* > node scripts/issueEmailGenerator.js --issueNumber <issueNumber>
*/
const optionDefinitions = [
{ name: 'issueNumber', alias: 'i', type: Number },
{ name: 'help', alias: 'h', type: Boolean },
];
const fs = require('fs');
const axios = require('axios');
const commandLineArgs = require('command-line-args');
require('dotenv').config({ path: './.env.local' });
const options = commandLineArgs(optionDefinitions);
if (options.help) {
console.log(`
Usage: node scripts/issueEmailGenerator.js [--issueNumber=<issueNumber>]
`);
return;
}
if (typeof options.issueNumber !== 'number') {
console.log('Please provide a valid issue number');
return;
}
(async () => {
const currentIssue = await axios
.get(`${process.env.CMS_API}issues/${options.issueNumber}`)
.then(response => response.data);
const PROFILE_TYPES = {
website: 'Website',
twitter: 'Twitter',
github: 'GitHub',
youtube: 'YouTube',
linkedin: 'LinkedIn',
instagram: 'Instagram',
};
const PROFILE_KEYS = ['webiste', 'twitter', 'github', 'youtube', 'linkedin', 'instagram'];
const convertDate = date => {
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
return new Date(date).toLocaleDateString('en-US', options);
};
const OG_IMAGE_BASE = 'https://og.scriptified.dev/';
const ASSETS_URL = 'https://images.scriptified.dev/';
function getOGImage(title, issueNumber, date) {
const parsedDate = convertDate(date);
return `${OG_IMAGE_BASE}${encodeURIComponent(title)}.png?issue_number=${issueNumber}&date=${encodeURIComponent(
parsedDate
)}`;
}
function isValidHttpUrl(str) {
let url;
try {
url = new URL(str);
} catch (_) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
}
function getAssetURL(issueNumber, assetURL) {
if (isValidHttpUrl(assetURL)) {
return assetURL;
}
return `${ASSETS_URL}issue-${issueNumber}/${assetURL}`;
}
const ogImgURL = getOGImage(currentIssue.title, currentIssue.id, currentIssue.dateOfPublishing);
const emailTemplate = `

<center>[Read issue on web](https://scriptified.dev/issues/${currentIssue.id})</center>
${currentIssue.description}
# Tip of the day
${currentIssue.tipOfTheWeek.description}
${
currentIssue.tipOfTheWeek.codeSnippet &&
`
\`\`\`${currentIssue.tipOfTheWeek.codeSnippet.language}
${currentIssue.tipOfTheWeek.codeSnippet.code}
\`\`\`
`
}
___
# Articles
${currentIssue.articles
.map(
article =>
`[**${article.title}**](${article.url})
${article.description}
*by ${article.authors.map(author => author.Name).join(', ')}*
`
)
.join('\n')}
___
${
currentIssue.devOfTheWeek
? `# Dev of the Week
<img alt="${currentIssue.devOfTheWeek.name}" src="${getAssetURL(
currentIssue.id,
currentIssue.devOfTheWeek.profileImg
)}" style="width:200px;"/>
## ${currentIssue.devOfTheWeek.name}
${currentIssue.devOfTheWeek.bio}
${Object.keys(currentIssue.devOfTheWeek)
.filter(key => PROFILE_KEYS.includes(key) && currentIssue.devOfTheWeek[key] !== null)
.map(profile => `[${PROFILE_TYPES[profile]}](${currentIssue.devOfTheWeek[profile]})`)
.join(' | ')}
___`
: ''
}
# Tools
${currentIssue.tools
.map(
tool =>
`[**${tool.name}**](${tool.url})
${tool.description}
*by ${tool.authors.map(author => author.Name).join(', ')}*
`
)
.join('\n')}
___
# Tech Talks
[**${currentIssue.talks[0].title}**](${currentIssue.talks[0].url})
${currentIssue.talks[0].url}
${currentIssue.talks[0].description}
___
# Quiz
### ${currentIssue.quiz.question}
\`\`\`${currentIssue.quiz.CodeSnippet.language}
${currentIssue.quiz.CodeSnippet.code}
\`\`\`
${currentIssue.quiz.Option.map(
option => `<a href="https://scriptified.dev/issues/${currentIssue.id}?section=quiz&option=${option.option_id}" style="text-decoration:none;">
<div style="margin: 12px 0px; border: 1px solid gray; padding: 16px; background: #F2F3F5;">
${option.text}
</div>
</a>
`
).join('\n')}
___
# This week in GIF
})
<center>${currentIssue.gif.caption}</center>
---
Liked this issue? [Share on Twitter](https://twitter.com/intent/tweet?text=${encodeURIComponent(`Have a look at issue #${currentIssue.id} of Scriptified.
Subscribe to @scriptified_dev for more.`)}&url=${encodeURIComponent(
`https://scriptified.dev/issues/${currentIssue.id}`
)}) or [read previous issues](https://scriptified.dev/issues).
`;
const archiveDirectory = './archives';
const issueFile = `${archiveDirectory}/issue${currentIssue.id}.md`;
// create archives folder if iot doesn't exist
if (!fs.existsSync(archiveDirectory)) {
fs.mkdirSync(archiveDirectory);
}
// const issueFile = `./issue${currentIssue.id}.md`;
// create a issue-{id}.md file in archives
fs.closeSync(fs.openSync(issueFile, 'a'));
// write email template to issue-{id}.md file
fs.writeFileSync(issueFile, emailTemplate);
console.log(`Created the template at - ${issueFile}`);
})();