Created
January 5, 2019 20:06
-
-
Save cannap/d712d10786b16b53a75aeb37ec291657 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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