Skip to content

Instantly share code, notes, and snippets.

@cannap
Created January 5, 2019 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cannap/d712d10786b16b53a75aeb37ec291657 to your computer and use it in GitHub Desktop.
Save cannap/d712d10786b16b53a75aeb37ec291657 to your computer and use it in GitHub Desktop.
const path = require('path')
const { readFile, writeFile } = require('fs').promises
const glob = require('fast-glob')
const mjml2html = require('mjml')
const outputExtension = 'hbs'
async function start () {
// Todo: clear emails before
const templates = await glob('./src/emails/**/*.mjml')
try {
const compiledTemplates = await Promise.all(
templates.map(template => {
return readFile(template, { encoding: 'utf-8' }).then(html => {
const extension = path.extname(template)
return {
name: path.basename(template, extension),
html: mjml2html(html, { beautify: true }).html
}
})
})
)
await Promise.all(
compiledTemplates.map(template => {
const outputPath = path.join(
__dirname,
'src',
'server',
'emails',
`${template.name}.${outputExtension}`
)
return writeFile(outputPath, template.html)
})
)
console.log('Building Emails done')
} catch (error) {
console.log('Building Failed!', error)
}
}
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment