forked from aragonone/court-backend
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-emails
executable file
·127 lines (106 loc) · 3.33 KB
/
build-emails
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
#!/usr/bin/env node
const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const prettier = require('prettier')
const handlebars = require('handlebars')
const postmark = require('postmark')
const {
MOCK_DATA,
PRINT_DATA_FOR,
SYNC_TEMPLATES,
POSTMARK_SERVER_API_TOKEN
} = require('./env')
const DIST_DIR = path.resolve('dist')
function renderTemplate(filename) {
const name = path.basename(filename, '.js')
const createTemplate = require(path.join(__dirname, 'templates', name))
let { subject, template, templateText, mockData } = createTemplate()
// Prettify templates
template = prettier.format(template, { parser: 'html', printWidth: 999 })
templateText = prettier.format(templateText, {
parser: 'markdown',
proseWrap: 'always',
printWidth: 80,
})
// Remove extra whitespaces in the text template
templateText = templateText.replace(/^ +/gm, '')
if (MOCK_DATA) {
template = handlebars.compile(template)(mockData)
templateText = handlebars.compile(templateText)(mockData)
}
return { subject, name, template, templateText }
}
async function syncPostmark(files) {
if (!POSTMARK_SERVER_API_TOKEN) {
throw 'You must specify a valid POSTMARK_SERVER_API_TOKEN environment variable.'
}
const client = new postmark.ServerClient(POSTMARK_SERVER_API_TOKEN)
for (const filename of files) {
const { subject, name, template, templateText } = renderTemplate(filename)
const params = {
Name: name,
Alias: name,
Subject: subject,
HtmlBody: template,
TextBody: templateText
}
try {
await client.getTemplate(name)
await client.editTemplate(name, params)
} catch {
await client.createTemplate(params)
}
}
}
function generateTemplates(files) {
execSync(`rm -rf "${DIST_DIR}"`)
fs.mkdirSync(DIST_DIR, { recursive: true })
files.forEach(filename => {
const { name, template, templateText } = renderTemplate(filename)
fs.writeFileSync(path.join(DIST_DIR, name + '.html'), template, 'utf8')
fs.writeFileSync(path.join(DIST_DIR, name + '.txt'), templateText, 'utf8')
})
execSync(`cp -r "${__dirname}/assets" "${DIST_DIR}/assets"`)
}
function printTemplateData(files, filename) {
// Tolerate .js suffix in the name passed to PRINT_DATA_FOR
const name = path.basename(filename, '.js')
const templateFile = files.find(filename => filename === name + '.js')
if (!templateFile) {
throw `Error: the template ${PRINT_DATA_FOR} couldn’t be found. ` +
`Please check the PRINT_DATA_FOR variable.`
}
const createTemplate = require(path.join(
__dirname,
'templates',
filename + '.js'
))
const template = createTemplate()
console.log('')
console.log(`Data for the template “${name}”:`)
console.log('')
console.log(JSON.stringify(template.mockData, null, 2))
console.log('')
}
async function main() {
const files = fs
.readdirSync(path.join(__dirname, 'templates'))
.filter(filename => filename.endsWith('.js'))
// Print template data
if (PRINT_DATA_FOR) {
printTemplateData(files, PRINT_DATA_FOR)
return
}
// sync templates to postmark
if (SYNC_TEMPLATES) {
await syncPostmark(files)
return
}
// Generate the templates
generateTemplates(files)
}
main().catch((error) => {
console.error(error)
process.exit(1)
})