-
Notifications
You must be signed in to change notification settings - Fork 62
/
generate-thumbnails.js
96 lines (82 loc) · 2.28 KB
/
generate-thumbnails.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
const promisify = require('es6-promisify')
const webshot = require('webshot')
const path = require('path')
const fs = require('fs')
const mjml2html = require('mjml')
const access = promisify(fs.access)
const readDir = promisify(fs.readdir)
const readFile = promisify(fs.readFile)
const mkdir = promisify(fs.mkdir)
const TEMPLATES_FOLDER = path.join(__dirname, 'templates')
const THUMB_FOLDER = path.join(__dirname, 'thumbnails')
const WEBSHOT_OPTIONS = {
siteType: 'html',
screenSize: {
width: 700,
},
shotSize: {
width: 700,
height: 'all',
},
defaultWhiteBackground: true,
}
;(async function () {
try {
await isWritableOrCreate(THUMB_FOLDER)
console.log('>> Reading templates')
const templates = await readDir(TEMPLATES_FOLDER)
const templatesWithContent = await Promise.all(templates.map(readContent))
console.log('>> Generating thumbnails')
await templatesWithContent.reduce((promise, template) => {
return promise.then(() => generateThumbnail(template))
}, Promise.resolve())
} catch (err) { exitErr(err) }
})()
async function isWritableOrCreate (folder) {
try {
await access(THUMB_FOLDER, fs.constants.R_OK | fs.constants.W_OK)
} catch (err) {
if (err.code === 'ENOENT') {
await mkdir(THUMB_FOLDER)
} else {
throw err
}
}
}
async function readContent (templateName) {
const templatePath = path.join(TEMPLATES_FOLDER, templateName)
const mjml = await readFile(templatePath, { encoding: 'utf8' })
return {
name: path.basename(templateName, '.mjml'),
mjml,
}
}
async function generateThumbnail (template) {
console.log(` > treating ${template.name}`)
const thumbnailName = path.join(THUMB_FOLDER, `${template.name}.jpg`)
const html = await getHTML(template.mjml)
await shot(thumbnailName, html)
}
function getHTML (mjml) {
return new Promise((resolve, reject) => {
try {
const res = mjml2html(mjml)
resolve(res.html)
} catch (err) {
reject(err)
}
})
}
function shot (name, html) {
return new Promise((resolve, reject) => {
webshot(html, name, WEBSHOT_OPTIONS, (err) => {
if (err) { return reject(err) }
resolve()
})
})
}
function exitErr (err) {
console.log('> Something went wrong')
console.log(err.message || err)
process.exit(1)
}